diff options
Diffstat (limited to 'src/libcore/option.rs')
| -rw-r--r-- | src/libcore/option.rs | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/src/libcore/option.rs b/src/libcore/option.rs index cadafc81013..94e824d9ad9 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -8,6 +8,10 @@ * type. */ +// NB: transitionary, de-mode-ing. +#[forbid(deprecated_mode)]; +#[forbid(deprecated_pattern)]; + use cmp::Eq; /// The option type @@ -16,7 +20,7 @@ enum Option<T> { Some(T), } -pure fn get<T: Copy>(opt: Option<T>) -> T { +pure fn get<T: Copy>(opt: &Option<T>) -> T { /*! * Gets the value out of an option * @@ -25,7 +29,7 @@ pure fn get<T: Copy>(opt: Option<T>) -> T { * Fails if the value equals `none` */ - match opt { + match *opt { Some(x) => return x, None => fail ~"option::get none" } @@ -45,7 +49,7 @@ pure fn get_ref<T>(opt: &r/Option<T>) -> &r/T { } } -pure fn expect<T: Copy>(opt: Option<T>, reason: ~str) -> T { +pure fn expect<T: Copy>(opt: &Option<T>, +reason: ~str) -> T { /*! * Gets the value out of an option, printing a specified message on * failure @@ -54,13 +58,13 @@ pure fn expect<T: Copy>(opt: Option<T>, reason: ~str) -> T { * * Fails if the value equals `none` */ - match opt { Some(x) => x, None => fail reason } + match *opt { Some(x) => x, None => fail reason } } -pure fn map<T, U>(opt: Option<T>, f: fn(T) -> U) -> Option<U> { +pure fn map<T, U>(opt: &Option<T>, f: fn(T) -> U) -> Option<U> { //! Maps a `some` value from one type to another - match opt { Some(x) => Some(f(x)), None => None } + match *opt { Some(x) => Some(f(x)), None => None } } pure fn map_ref<T, U>(opt: &Option<T>, f: fn(x: &T) -> U) -> Option<U> { @@ -77,13 +81,13 @@ pure fn map_consume<T, U>(+opt: Option<T>, f: fn(+v: T) -> U) -> Option<U> { if opt.is_some() { Some(f(option::unwrap(move opt))) } else { None } } -pure fn chain<T, U>(opt: Option<T>, f: fn(T) -> Option<U>) -> Option<U> { +pure fn chain<T, U>(opt: &Option<T>, f: fn(T) -> Option<U>) -> Option<U> { /*! * Update an optional value by optionally running its content through a * function that returns an option. */ - match opt { Some(x) => f(x), None => None } + match *opt { Some(x) => f(x), None => None } } pure fn chain_ref<T, U>(opt: &Option<T>, @@ -116,28 +120,28 @@ pure fn while_some<T>(+x: Option<T>, blk: fn(+v: T) -> Option<T>) { } } -pure fn is_none<T>(opt: Option<T>) -> bool { +pure fn is_none<T>(opt: &Option<T>) -> bool { //! Returns true if the option equals `none` - match opt { None => true, Some(_) => false } + match *opt { None => true, Some(_) => false } } -pure fn is_some<T>(opt: Option<T>) -> bool { +pure fn is_some<T>(opt: &Option<T>) -> bool { //! Returns true if the option contains some value !is_none(opt) } -pure fn get_default<T: Copy>(opt: Option<T>, def: T) -> T { +pure fn get_default<T: Copy>(opt: &Option<T>, +def: T) -> T { //! Returns the contained value or a default - match opt { Some(x) => x, None => def } + match *opt { Some(x) => x, None => def } } -pure fn map_default<T, U>(opt: Option<T>, +def: U, f: fn(T) -> U) -> U { +pure fn map_default<T, U>(opt: &Option<T>, +def: U, f: fn(T) -> U) -> U { //! Applies a function to the contained value or returns a default - match opt { None => move def, Some(t) => f(t) } + match *opt { None => move def, Some(t) => f(t) } } // This should replace map_default. @@ -149,10 +153,10 @@ pure fn map_default_ref<T, U>(opt: &Option<T>, +def: U, } // This should change to by-copy mode; use iter_ref below for by reference -pure fn iter<T>(opt: Option<T>, f: fn(T)) { +pure fn iter<T>(opt: &Option<T>, f: fn(T)) { //! Performs an operation on the contained value or does nothing - match opt { None => (), Some(t) => f(t) } + match *opt { None => (), Some(t) => f(t) } } pure fn iter_ref<T>(opt: &Option<T>, f: fn(x: &T)) { @@ -163,7 +167,7 @@ pure fn iter_ref<T>(opt: &Option<T>, f: fn(x: &T)) { // tjc: shouldn't this be - instead of +? // then could get rid of some superfluous moves #[inline(always)] -pure fn unwrap<T>(-opt: Option<T>) -> T { +pure fn unwrap<T>(+opt: Option<T>) -> T { /*! * Moves a value out of an option type and returns it. * @@ -195,18 +199,18 @@ impl<T> Option<T> { * Update an optional value by optionally running its content through a * function that returns an option. */ - pure fn chain<U>(f: fn(T) -> Option<U>) -> Option<U> { chain(self, f) } + pure fn chain<U>(f: fn(T) -> Option<U>) -> Option<U> { chain(&self, f) } /// Applies a function to the contained value or returns a default pure fn map_default<U>(+def: U, f: fn(T) -> U) -> U - { map_default(self, move def, f) } + { map_default(&self, move def, f) } /// Performs an operation on the contained value or does nothing - pure fn iter(f: fn(T)) { iter(self, f) } + pure fn iter(f: fn(T)) { iter(&self, f) } /// Returns true if the option equals `none` - pure fn is_none() -> bool { is_none(self) } + pure fn is_none() -> bool { is_none(&self) } /// Returns true if the option contains some value - pure fn is_some() -> bool { is_some(self) } + pure fn is_some() -> bool { is_some(&self) } /// Maps a `some` value from one type to another - pure fn map<U>(f: fn(T) -> U) -> Option<U> { map(self, f) } + pure fn map<U>(f: fn(T) -> U) -> Option<U> { map(&self, f) } } impl<T> &Option<T> { @@ -236,8 +240,8 @@ impl<T: Copy> Option<T> { * * Fails if the value equals `none` */ - pure fn get() -> T { get(self) } - pure fn get_default(def: T) -> T { get_default(self, def) } + pure fn get() -> T { get(&self) } + pure fn get_default(+def: T) -> T { get_default(&self, def) } /** * Gets the value out of an option, printing a specified message on * failure @@ -246,7 +250,7 @@ impl<T: Copy> Option<T> { * * Fails if the value equals `none` */ - pure fn expect(reason: ~str) -> T { expect(self, reason) } + pure fn expect(+reason: ~str) -> T { expect(&self, reason) } /// Applies a function zero or more times until the result is none. pure fn while_some(blk: fn(+v: T) -> Option<T>) { while_some(self, blk) } } |
