Skip to content

Commit

Permalink
Add option::get_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
bblum committed Aug 17, 2012
1 parent a076c28 commit 0e3825d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ pure fn get<T: copy>(opt: option<T>) -> T {
}
}

pure fn get_ref<T>(opt: &option<T>) -> &T {
/*!
* Gets an immutable reference to the value inside an option.
*
* # Failure
*
* Fails if the value equals `none`
*/
match *opt {
some(ref x) => x,
none => fail ~"option::get_ref none"
}
}

pure fn expect<T: copy>(opt: option<T>, reason: ~str) -> T {
#[doc = "
Gets the value out of an option, printing a specified message on failure
Expand Down Expand Up @@ -201,6 +215,8 @@ impl<T> &option<T> {
pure fn iter_ref(f: fn(x: &T)) { iter_ref(self, f) }
/// Maps a `some` value from one type to another by reference
pure fn map_ref<U>(f: fn(x: &T) -> U) -> option<U> { map_ref(self, f) }
/// Gets an immutable reference to the value inside a `some`.
pure fn get_ref() -> &self/T { get_ref(self) }
}

impl<T: copy> option<T> {
Expand Down

0 comments on commit 0e3825d

Please sign in to comment.