diff --git a/src/libextra/dlist.rs b/src/libextra/dlist.rs index 35600df4f9dd0..c6dc76adaea56 100644 --- a/src/libextra/dlist.rs +++ b/src/libextra/dlist.rs @@ -208,7 +208,7 @@ impl Deque for DList { /// /// O(1) fn pop_front(&mut self) -> Option { - match util::replace(&mut self.list_head, None) { + match self.list_head.take() { None => None, Some(old_head) => { self.length -= 1; diff --git a/src/libextra/ringbuf.rs b/src/libextra/ringbuf.rs index feec954bc0297..48d89fd662d83 100644 --- a/src/libextra/ringbuf.rs +++ b/src/libextra/ringbuf.rs @@ -14,7 +14,6 @@ //! extra::container::Deque`. use std::num; -use std::util; use std::uint; use std::vec; use std::iterator::{FromIterator, InvertIterator}; @@ -72,7 +71,7 @@ impl Deque for RingBuf { /// Remove and return the first element in the RingBuf, or None if it is empty fn pop_front(&mut self) -> Option { - let result = util::replace(&mut self.elts[self.lo], None); + let result = self.elts[self.lo].take(); if result.is_some() { self.lo = (self.lo + 1u) % self.elts.len(); self.nelts -= 1u; @@ -85,7 +84,7 @@ impl Deque for RingBuf { if self.nelts > 0 { self.nelts -= 1; let hi = self.raw_index(self.nelts); - util::replace(&mut self.elts[hi], None) + self.elts[hi].take() } else { None } diff --git a/src/libextra/smallintmap.rs b/src/libextra/smallintmap.rs index 200e824209450..f71d2ad80d480 100644 --- a/src/libextra/smallintmap.rs +++ b/src/libextra/smallintmap.rs @@ -118,7 +118,7 @@ impl MutableMap for SmallIntMap { if *key >= self.v.len() { return None; } - replace(&mut self.v[*key], None) + self.v[*key].take() } } diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index bd13c8619be1d..49253d800524e 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -695,7 +695,7 @@ fn remove(node: &mut Option<~TreeNode>, } } } - return match replace(node, None) { + return match node.take() { Some(~TreeNode{value, _}) => Some(value), None => fail!() }; } diff --git a/src/libextra/workcache.rs b/src/libextra/workcache.rs index e7bec2fbd8d1c..e1e90a0e0c192 100644 --- a/src/libextra/workcache.rs +++ b/src/libextra/workcache.rs @@ -27,7 +27,6 @@ use std::result; use std::run; use std::task; use std::to_bytes; -use std::util::replace; /** * @@ -346,7 +345,7 @@ impl TPrep for Prep { _ => { let (port, chan) = oneshot(); - let blk = replace(&mut bo, None).unwrap(); + let blk = bo.take_unwrap(); let chan = Cell::new(chan); do task::spawn { @@ -378,7 +377,7 @@ fn unwrap>( // FIXME(#5121) w: Work) -> T { let mut ww = w; - let s = replace(&mut ww.res, None); + let s = ww.res.take(); match s { None => fail!(), diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index 53ea11f2b0592..695ed0749dde0 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -14,7 +14,6 @@ use cast::transmute_mut; use prelude::*; -use util::replace; /* A dynamic, mutable location. @@ -48,7 +47,7 @@ impl Cell { fail!("attempt to take an empty cell"); } - replace(&mut this.value, None).unwrap() + this.value.take_unwrap() } /// Returns the value, failing if the cell is full. diff --git a/src/libstd/comm.rs b/src/libstd/comm.rs index 1bb0ff044fe98..e28f03fae9e0c 100644 --- a/src/libstd/comm.rs +++ b/src/libstd/comm.rs @@ -242,8 +242,7 @@ impl GenericChan for SharedChan { unsafe { let mut xx = Some(x); do chan.with_imm |chan| { - let x = replace(&mut xx, None); - chan.send(x.unwrap()) + chan.send(xx.take_unwrap()) } } } @@ -259,8 +258,7 @@ impl GenericSmartChan for SharedChan { unsafe { let mut xx = Some(x); do chan.with_imm |chan| { - let x = replace(&mut xx, None); - chan.try_send(x.unwrap()) + chan.try_send(xx.take_unwrap()) } } } @@ -372,7 +370,6 @@ mod pipesy { use pipes::{recv, try_recv, peek, PacketHeader}; use super::{GenericChan, GenericSmartChan, GenericPort, Peekable, Selectable}; use cast::transmute_mut; - use util::replace; /*proto! oneshot ( Oneshot:send { @@ -638,8 +635,7 @@ mod pipesy { fn send(&self, x: T) { unsafe { let self_endp = transmute_mut(&self.endp); - let endp = replace(self_endp, None); - *self_endp = Some(streamp::client::data(endp.unwrap(), x)) + *self_endp = Some(streamp::client::data(self_endp.take_unwrap(), x)) } } } @@ -649,8 +645,7 @@ mod pipesy { fn try_send(&self, x: T) -> bool { unsafe { let self_endp = transmute_mut(&self.endp); - let endp = replace(self_endp, None); - match streamp::client::try_data(endp.unwrap(), x) { + match streamp::client::try_data(self_endp.take_unwrap(), x) { Some(next) => { *self_endp = Some(next); true @@ -666,7 +661,7 @@ mod pipesy { fn recv(&self) -> T { unsafe { let self_endp = transmute_mut(&self.endp); - let endp = replace(self_endp, None); + let endp = self_endp.take(); let streamp::data(x, endp) = recv(endp.unwrap()); *self_endp = Some(endp); x @@ -677,7 +672,7 @@ mod pipesy { fn try_recv(&self) -> Option { unsafe { let self_endp = transmute_mut(&self.endp); - let endp = replace(self_endp, None); + let endp = self_endp.take(); match try_recv(endp.unwrap()) { Some(streamp::data(x, endp)) => { *self_endp = Some(endp); @@ -694,7 +689,7 @@ mod pipesy { fn peek(&self) -> bool { unsafe { let self_endp = transmute_mut(&self.endp); - let mut endp = replace(self_endp, None); + let mut endp = self_endp.take(); let peek = match endp { Some(ref mut endp) => peek(endp), None => fail!("peeking empty stream") diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index 79c6c4fb21dcc..d2b349c51b973 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -254,7 +254,7 @@ impl HashMap { }; let len_buckets = self.buckets.len(); - let bucket = replace(&mut self.buckets[idx], None); + let bucket = self.buckets[idx].take(); let value = match bucket { None => None, @@ -268,7 +268,7 @@ impl HashMap { let size = self.size - 1; idx = self.next_bucket(idx, len_buckets); while self.buckets[idx].is_some() { - let bucket = replace(&mut self.buckets[idx], None); + let bucket = self.buckets[idx].take(); self.insert_opt_bucket(bucket); idx = self.next_bucket(idx, len_buckets); } diff --git a/src/libstd/option.rs b/src/libstd/option.rs index b0811674a7bfa..1dac5297335b6 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -295,7 +295,15 @@ impl Option { #[inline] pub fn take_unwrap(&mut self) -> T { if self.is_none() { fail!("option::take_unwrap none") } - util::replace(self, None).unwrap() + self.take().unwrap() + } + + /** + * Return the value, replacing the original value with `None`. + */ + #[inline] + pub fn take(&mut self) -> Option { + util::replace(self, None) } /** @@ -377,7 +385,7 @@ pub struct OptionIterator<'self, A> { impl<'self, A> Iterator<&'self A> for OptionIterator<'self, A> { fn next(&mut self) -> Option<&'self A> { - util::replace(&mut self.opt, None) + self.opt.take() } fn size_hint(&self) -> (uint, Option) { @@ -395,7 +403,7 @@ pub struct OptionMutIterator<'self, A> { impl<'self, A> Iterator<&'self mut A> for OptionMutIterator<'self, A> { fn next(&mut self) -> Option<&'self mut A> { - util::replace(&mut self.opt, None) + self.opt.take() } fn size_hint(&self) -> (uint, Option) { @@ -472,6 +480,19 @@ fn test_option_too_much_dance() { let _y3 = y.take_unwrap(); } +#[test] +fn test_option_take() { + let mut x = Some(()); + let mut y = x.take(); + let z = y.take(); + assert!(x.is_none()); + assert!(y.is_none()); + assert_eq!(z, Some(())); + + let w = x.take(); + assert_eq!(w, None); +} + #[test] fn test_option_while_some() { let mut i = 0; diff --git a/src/libstd/pipes.rs b/src/libstd/pipes.rs index 49713a3a23b9a..501cfb667ed1e 100644 --- a/src/libstd/pipes.rs +++ b/src/libstd/pipes.rs @@ -431,7 +431,7 @@ fn try_recv_(p: &mut Packet) -> Option { // optimistic path match p.header.state { Full => { - let payload = replace(&mut p.payload, None); + let payload = p.payload.take(); p.header.state = Empty; return Some(payload.unwrap()) }, @@ -482,7 +482,7 @@ fn try_recv_(p: &mut Packet) -> Option { fail!("blocking on already blocked packet") }, Full => { - let payload = replace(&mut p.payload, None); + let payload = p.payload.take(); let old_task = swap_task(&mut p.header.blocked_task, ptr::null()); if !old_task.is_null() { unsafe { @@ -676,8 +676,7 @@ impl Drop for SendPacketBuffered { unsafe { let this: &mut SendPacketBuffered = transmute(self); if this.p != None { - let p = replace(&mut this.p, None); - sender_terminate(p.unwrap()) + sender_terminate(this.p.take_unwrap()); } } } @@ -695,7 +694,7 @@ pub fn SendPacketBuffered(p: *mut Packet) impl SendPacketBuffered { pub fn unwrap(&mut self) -> *mut Packet { - replace(&mut self.p, None).unwrap() + self.p.take_unwrap() } pub fn header(&mut self) -> *mut PacketHeader { @@ -711,7 +710,7 @@ impl SendPacketBuffered { pub fn reuse_buffer(&mut self) -> BufferResource { //error!("send reuse_buffer"); - replace(&mut self.buffer, None).unwrap() + self.buffer.take_unwrap() } } @@ -734,8 +733,7 @@ impl Drop for RecvPacketBuffered { unsafe { let this: &mut RecvPacketBuffered = transmute(self); if this.p != None { - let p = replace(&mut this.p, None); - receiver_terminate(p.unwrap()) + receiver_terminate(this.p.take_unwrap()) } } } @@ -743,11 +741,11 @@ impl Drop for RecvPacketBuffered { impl RecvPacketBuffered { pub fn unwrap(&mut self) -> *mut Packet { - replace(&mut self.p, None).unwrap() + self.p.take_unwrap() } pub fn reuse_buffer(&mut self) -> BufferResource { - replace(&mut self.buffer, None).unwrap() + self.buffer.take_unwrap() } } diff --git a/src/libstd/task/mod.rs b/src/libstd/task/mod.rs index b14100991dfcf..5814b7e73261d 100644 --- a/src/libstd/task/mod.rs +++ b/src/libstd/task/mod.rs @@ -45,7 +45,6 @@ use result; use rt::{context, OldTaskContext}; use task::rt::{task_id, sched_id}; use unstable::finally::Finally; -use util::replace; use util; #[cfg(test)] use cast; @@ -212,8 +211,8 @@ impl TaskBuilder { fail!("Cannot copy a task_builder"); // Fake move mode on self } self.consumed = true; - let gen_body = replace(&mut self.gen_body, None); - let notify_chan = replace(&mut self.opts.notify_chan, None); + let gen_body = self.gen_body.take(); + let notify_chan = self.opts.notify_chan.take(); TaskBuilder { opts: TaskOpts { linked: self.opts.linked, @@ -304,7 +303,7 @@ impl TaskBuilder { * existing body generator to the new body generator. */ pub fn add_wrapper(&mut self, wrapper: ~fn(v: ~fn()) -> ~fn()) { - let prev_gen_body = replace(&mut self.gen_body, None); + let prev_gen_body = self.gen_body.take(); let prev_gen_body = match prev_gen_body { Some(gen) => gen, None => { @@ -336,8 +335,8 @@ impl TaskBuilder { * must be greater than zero. */ pub fn spawn(&mut self, f: ~fn()) { - let gen_body = replace(&mut self.gen_body, None); - let notify_chan = replace(&mut self.opts.notify_chan, None); + let gen_body = self.gen_body.take(); + let notify_chan = self.opts.notify_chan.take(); let x = self.consume(); let opts = TaskOpts { linked: x.opts.linked, diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index c728d4a60f15f..99fa907dc2d0e 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -1896,12 +1896,11 @@ pub mod raw { use cast::transmute; use kinds::Copy; use managed; - use option::{None, Some}; + use option::Some; use ptr; use sys; use unstable::intrinsics; use vec::{UnboxedVecRepr, with_capacity, ImmutableVector, MutableVector}; - use util; #[cfg(not(stage0))] use unstable::intrinsics::contains_managed; @@ -2022,9 +2021,8 @@ pub mod raw { pub unsafe fn init_elem(v: &mut [T], i: uint, val: T) { let mut box = Some(val); do v.as_mut_buf |p, _len| { - let box2 = util::replace(&mut box, None); intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), - box2.unwrap()); + box.take_unwrap()); } }