Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Option::take() #7865

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl<T> Deque<T> for DList<T> {
///
/// O(1)
fn pop_front(&mut self) -> Option<T> {
match util::replace(&mut self.list_head, None) {
match self.list_head.take() {
None => None,
Some(old_head) => {
self.length -= 1;
Expand Down
5 changes: 2 additions & 3 deletions src/libextra/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
//! extra::container::Deque`.

use std::num;
use std::util;
use std::uint;
use std::vec;
use std::iterator::{FromIterator, InvertIterator};
Expand Down Expand Up @@ -72,7 +71,7 @@ impl<T> Deque<T> for RingBuf<T> {

/// Remove and return the first element in the RingBuf, or None if it is empty
fn pop_front(&mut self) -> Option<T> {
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;
Expand All @@ -85,7 +84,7 @@ impl<T> Deque<T> for RingBuf<T> {
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
}
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<V> MutableMap<uint, V> for SmallIntMap<V> {
if *key >= self.v.len() {
return None;
}
replace(&mut self.v[*key], None)
self.v[*key].take()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
}
}
}
return match replace(node, None) {
return match node.take() {
Some(~TreeNode{value, _}) => Some(value), None => fail!()
};
}
Expand Down
5 changes: 2 additions & 3 deletions src/libextra/workcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use std::result;
use std::run;
use std::task;
use std::to_bytes;
use std::util::replace;

/**
*
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -378,7 +377,7 @@ fn unwrap<T:Send +
Decodable<json::Decoder>>( // FIXME(#5121)
w: Work<T>) -> T {
let mut ww = w;
let s = replace(&mut ww.res, None);
let s = ww.res.take();

match s {
None => fail!(),
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use cast::transmute_mut;
use prelude::*;
use util::replace;

/*
A dynamic, mutable location.
Expand Down Expand Up @@ -48,7 +47,7 @@ impl<T> Cell<T> {
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.
Expand Down
19 changes: 7 additions & 12 deletions src/libstd/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ impl<T: Send> GenericChan<T> for SharedChan<T> {
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())
}
}
}
Expand All @@ -259,8 +258,7 @@ impl<T: Send> GenericSmartChan<T> for SharedChan<T> {
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())
}
}
}
Expand Down Expand Up @@ -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<T:Send> {
Expand Down Expand Up @@ -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))
}
}
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -677,7 +672,7 @@ mod pipesy {
fn try_recv(&self) -> Option<T> {
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);
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
};

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,
Expand All @@ -268,7 +268,7 @@ impl<K:Hash + Eq,V> HashMap<K, V> {
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);
}
Expand Down
27 changes: 24 additions & 3 deletions src/libstd/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,15 @@ impl<T> Option<T> {
#[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<T> {
util::replace(self, None)
}

/**
Expand Down Expand Up @@ -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<uint>) {
Expand All @@ -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<uint>) {
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 8 additions & 10 deletions src/libstd/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ fn try_recv_<T:Send>(p: &mut Packet<T>) -> Option<T> {
// 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())
},
Expand Down Expand Up @@ -482,7 +482,7 @@ fn try_recv_<T:Send>(p: &mut Packet<T>) -> Option<T> {
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 {
Expand Down Expand Up @@ -676,8 +676,7 @@ impl<T:Send,Tbuffer:Send> Drop for SendPacketBuffered<T,Tbuffer> {
unsafe {
let this: &mut SendPacketBuffered<T,Tbuffer> = transmute(self);
if this.p != None {
let p = replace(&mut this.p, None);
sender_terminate(p.unwrap())
sender_terminate(this.p.take_unwrap());
}
}
}
Expand All @@ -695,7 +694,7 @@ pub fn SendPacketBuffered<T,Tbuffer>(p: *mut Packet<T>)

impl<T,Tbuffer> SendPacketBuffered<T,Tbuffer> {
pub fn unwrap(&mut self) -> *mut Packet<T> {
replace(&mut self.p, None).unwrap()
self.p.take_unwrap()
}

pub fn header(&mut self) -> *mut PacketHeader {
Expand All @@ -711,7 +710,7 @@ impl<T,Tbuffer> SendPacketBuffered<T,Tbuffer> {

pub fn reuse_buffer(&mut self) -> BufferResource<Tbuffer> {
//error!("send reuse_buffer");
replace(&mut self.buffer, None).unwrap()
self.buffer.take_unwrap()
}
}

Expand All @@ -734,20 +733,19 @@ impl<T:Send,Tbuffer:Send> Drop for RecvPacketBuffered<T,Tbuffer> {
unsafe {
let this: &mut RecvPacketBuffered<T,Tbuffer> = transmute(self);
if this.p != None {
let p = replace(&mut this.p, None);
receiver_terminate(p.unwrap())
receiver_terminate(this.p.take_unwrap())
}
}
}
}

impl<T:Send,Tbuffer:Send> RecvPacketBuffered<T, Tbuffer> {
pub fn unwrap(&mut self) -> *mut Packet<T> {
replace(&mut self.p, None).unwrap()
self.p.take_unwrap()
}

pub fn reuse_buffer(&mut self) -> BufferResource<Tbuffer> {
replace(&mut self.buffer, None).unwrap()
self.buffer.take_unwrap()
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/libstd/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -2022,9 +2021,8 @@ pub mod raw {
pub unsafe fn init_elem<T>(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());
}
}

Expand Down