diff options
| author | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-08-04 14:59:36 -0700 |
|---|---|---|
| committer | Erick Tryzelaar <erick.tryzelaar@gmail.com> | 2013-08-07 08:52:09 -0700 |
| commit | 1e490813b017f99cb4385fe846d645efe5d62b62 (patch) | |
| tree | 3496b13798c77122fe694387cb10b9ea905279ee /src/libstd | |
| parent | 9218aaa00ed0883d3a082f8753a206fb6d03dac9 (diff) | |
| download | rust-1e490813b017f99cb4385fe846d645efe5d62b62.tar.gz rust-1e490813b017f99cb4385fe846d645efe5d62b62.zip | |
core: option.map_consume -> option.map_move
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/hashmap.rs | 4 | ||||
| -rw-r--r-- | src/libstd/iterator.rs | 8 | ||||
| -rw-r--r-- | src/libstd/option.rs | 28 | ||||
| -rw-r--r-- | src/libstd/os.rs | 4 | ||||
| -rw-r--r-- | src/libstd/result.rs | 8 | ||||
| -rw-r--r-- | src/libstd/rt/comm.rs | 4 | ||||
| -rw-r--r-- | src/libstd/rt/kill.rs | 4 | ||||
| -rw-r--r-- | src/libstd/rt/sched.rs | 4 | ||||
| -rw-r--r-- | src/libstd/str.rs | 2 | ||||
| -rw-r--r-- | src/libstd/task/spawn.rs | 6 |
10 files changed, 35 insertions, 37 deletions
diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index fbc471c0ae0..3484a5e7d6e 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -238,7 +238,7 @@ impl<K:Hash + Eq,V> HashMap<K, V> { let len_buckets = self.buckets.len(); let bucket = self.buckets[idx].take(); - let value = do bucket.map_consume |bucket| { + let value = do bucket.map_move |bucket| { bucket.value }; @@ -479,7 +479,7 @@ impl<K: Hash + Eq, V> HashMap<K, V> { impl<K: Hash + Eq, V: Clone> HashMap<K, V> { /// Like `find`, but returns a copy of the value. pub fn find_copy(&self, k: &K) -> Option<V> { - self.find(k).map_consume(|v| (*v).clone()) + self.find(k).map_move(|v| (*v).clone()) } /// Like `get`, but returns a copy of the value. diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index e86f4d7a85e..47e8eac27f0 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -674,7 +674,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T { Some((y, y_val)) } } - }).map_consume(|(x, _)| x) + }).map_move(|(x, _)| x) } #[inline] @@ -689,7 +689,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T { Some((y, y_val)) } } - }).map_consume(|(x, _)| x) + }).map_move(|(x, _)| x) } } @@ -1383,7 +1383,7 @@ impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for return Some(x) } } - match self.iter.next().map_consume(|x| (self.f)(x)) { + match self.iter.next().map_move(|x| (self.f)(x)) { None => return self.backiter.chain_mut_ref(|it| it.next()), next => self.frontiter = next, } @@ -1415,7 +1415,7 @@ impl<'self, y => return y } } - match self.iter.next_back().map_consume(|x| (self.f)(x)) { + match self.iter.next_back().map_move(|x| (self.f)(x)) { None => return self.frontiter.chain_mut_ref(|it| it.next_back()), next => self.backiter = next, } diff --git a/src/libstd/option.rs b/src/libstd/option.rs index ea1bddcdb4b..e43ff65da5e 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -208,6 +208,12 @@ impl<T> Option<T> { match *self { Some(ref mut x) => Some(f(x)), None => None } } + /// Applies a function to the contained value or returns a default + #[inline] + pub fn map_default<'a, U>(&'a self, def: U, f: &fn(&'a T) -> U) -> U { + match *self { None => def, Some(ref t) => f(t) } + } + /// Maps a `Some` value from one type to another by a mutable reference, /// or returns a default value. #[inline] @@ -218,21 +224,15 @@ impl<T> Option<T> { /// As `map`, but consumes the option and gives `f` ownership to avoid /// copying. #[inline] - pub fn map_consume<U>(self, f: &fn(v: T) -> U) -> Option<U> { - match self { None => None, Some(v) => Some(f(v)) } - } - - /// Applies a function to the contained value or returns a default - #[inline] - pub fn map_default<'a, U>(&'a self, def: U, f: &fn(&'a T) -> U) -> U { - match *self { None => def, Some(ref t) => f(t) } + pub fn map_move<U>(self, f: &fn(T) -> U) -> Option<U> { + match self { Some(x) => Some(f(x)), None => None } } /// As `map_default`, but consumes the option and gives `f` /// ownership to avoid copying. #[inline] - pub fn map_consume_default<U>(self, def: U, f: &fn(v: T) -> U) -> U { - match self { None => def, Some(v) => f(v) } + pub fn map_move_default<U>(self, def: U, f: &fn(T) -> U) -> U { + match self { None => def, Some(t) => f(t) } } /// Take the value out of the option, leaving a `None` in its place. @@ -241,18 +241,18 @@ impl<T> Option<T> { util::replace(self, None) } - /// As `map_consume`, but swaps a None into the original option rather + /// As `map_move`, but swaps a None into the original option rather /// than consuming it by-value. #[inline] pub fn take_map<U>(&mut self, blk: &fn(T) -> U) -> Option<U> { - self.take().map_consume(blk) + self.take().map_move(blk) } - /// As `map_consume_default`, but swaps a None into the original option + /// As `map_move_default`, but swaps a None into the original option /// rather than consuming it by-value. #[inline] pub fn take_map_default<U> (&mut self, def: U, blk: &fn(T) -> U) -> U { - self.take().map_consume_default(def, blk) + self.take().map_move_default(def, blk) } /// Apply a function to the contained value or do nothing. diff --git a/src/libstd/os.rs b/src/libstd/os.rs index b0e1f35b4a0..f246a61a4d5 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -498,9 +498,7 @@ pub fn self_exe_path() -> Option<Path> { } } - do load_self().map |pth| { - Path(*pth).dir_path() - } + load_self().map_move(|path| Path(path).dir_path()) } diff --git a/src/libstd/result.rs b/src/libstd/result.rs index e62ae3885eb..3e429c6116d 100644 --- a/src/libstd/result.rs +++ b/src/libstd/result.rs @@ -407,14 +407,14 @@ mod tests { #[test] pub fn test_impl_map_move() { - assert_eq!(Ok::<~str, ~str>(~"a").map_move(|x| x + ~"b"), Ok(~"ab")); - assert_eq!(Err::<~str, ~str>(~"a").map_move(|x| x + ~"b"), Err(~"a")); + assert_eq!(Ok::<~str, ~str>(~"a").map_move(|x| x + "b"), Ok(~"ab")); + assert_eq!(Err::<~str, ~str>(~"a").map_move(|x| x + "b"), Err(~"a")); } #[test] pub fn test_impl_map_err_move() { - assert_eq!(Ok::<~str, ~str>(~"a").map_err_move(|x| x + ~"b"), Ok(~"a")); - assert_eq!(Err::<~str, ~str>(~"a").map_err_move(|x| x + ~"b"), Err(~"ab")); + assert_eq!(Ok::<~str, ~str>(~"a").map_err_move(|x| x + "b"), Ok(~"a")); + assert_eq!(Err::<~str, ~str>(~"a").map_err_move(|x| x + "b"), Err(~"ab")); } #[test] diff --git a/src/libstd/rt/comm.rs b/src/libstd/rt/comm.rs index a060059f5fc..0cf223f3029 100644 --- a/src/libstd/rt/comm.rs +++ b/src/libstd/rt/comm.rs @@ -159,7 +159,7 @@ impl<T> ChanOne<T> { // Port is blocked. Wake it up. let recvr = BlockedTask::cast_from_uint(task_as_state); if do_resched { - do recvr.wake().map_consume |woken_task| { + do recvr.wake().map_move |woken_task| { Scheduler::run_task(woken_task); }; } else { @@ -381,7 +381,7 @@ impl<T> Drop for ChanOne<T> { // The port is blocked waiting for a message we will never send. Wake it. assert!((*this.packet()).payload.is_none()); let recvr = BlockedTask::cast_from_uint(task_as_state); - do recvr.wake().map_consume |woken_task| { + do recvr.wake().map_move |woken_task| { Scheduler::run_task(woken_task); }; } diff --git a/src/libstd/rt/kill.rs b/src/libstd/rt/kill.rs index 3372c13b877..d90ad07650d 100644 --- a/src/libstd/rt/kill.rs +++ b/src/libstd/rt/kill.rs @@ -402,7 +402,7 @@ impl KillHandle { || { // Prefer to check tombstones that were there first, // being "more fair" at the expense of tail-recursion. - others.take().map_consume_default(true, |f| f()) && { + others.take().map_move_default(true, |f| f()) && { let mut inner = this.take().unwrap(); (!inner.any_child_failed) && inner.child_tombstones.take_map_default(true, |f| f()) @@ -424,7 +424,7 @@ impl KillHandle { let others = Cell::new(other_tombstones); // :( || { // Prefer fairness to tail-recursion, as in above case. - others.take().map_consume_default(true, |f| f()) && + others.take().map_move_default(true, |f| f()) && f.take()() } } diff --git a/src/libstd/rt/sched.rs b/src/libstd/rt/sched.rs index 1a75f2569b5..c2c12c6e3c0 100644 --- a/src/libstd/rt/sched.rs +++ b/src/libstd/rt/sched.rs @@ -325,7 +325,7 @@ impl Scheduler { /// As enqueue_task, but with the possibility for the blocked task to /// already have been killed. pub fn enqueue_blocked_task(&mut self, blocked_task: BlockedTask) { - do blocked_task.wake().map_consume |task| { + do blocked_task.wake().map_move |task| { self.enqueue_task(task); }; } @@ -533,7 +533,7 @@ impl Scheduler { sched.enqueue_blocked_task(last_task); } }; - opt.map_consume(Local::put); + opt.map_move(Local::put); } // The primary function for changing contexts. In the current diff --git a/src/libstd/str.rs b/src/libstd/str.rs index b4057b85cbf..a327b687a75 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1849,7 +1849,7 @@ impl<'self> StrSlice<'self> for &'self str { } else { self.matches_index_iter(needle) .next() - .map_consume(|(start, _end)| start) + .map_move(|(start, _end)| start) } } diff --git a/src/libstd/task/spawn.rs b/src/libstd/task/spawn.rs index 527b20b0e90..7486a78837c 100644 --- a/src/libstd/task/spawn.rs +++ b/src/libstd/task/spawn.rs @@ -500,7 +500,7 @@ impl RuntimeGlue { OldTask(ptr) => rt::rust_task_kill_other(ptr), NewTask(handle) => { let mut handle = handle; - do handle.kill().map_consume |killed_task| { + do handle.kill().map_move |killed_task| { let killed_task = Cell::new(killed_task); do Local::borrow::<Scheduler, ()> |sched| { sched.enqueue_task(killed_task.take()); @@ -682,7 +682,7 @@ fn spawn_raw_newsched(mut opts: TaskOpts, f: ~fn()) { // Child task runs this code. // If child data is 'None', the enlist is vacuously successful. - let enlist_success = do child_data.take().map_consume_default(true) |child_data| { + let enlist_success = do child_data.take().map_move_default(true) |child_data| { let child_data = Cell::new(child_data); // :( do Local::borrow::<Task, bool> |me| { let (child_tg, ancestors, is_main) = child_data.take(); @@ -854,7 +854,7 @@ fn spawn_raw_oldsched(mut opts: TaskOpts, f: ~fn()) { // Even if the below code fails to kick the child off, we must // send Something on the notify channel. - let notifier = notify_chan.map_consume(|c| AutoNotify(c)); + let notifier = notify_chan.map_move(|c| AutoNotify(c)); if enlist_many(OldTask(child), &child_arc, &mut ancestors) { let group = @@mut Taskgroup(child_arc, ancestors, is_main, notifier); |
