Skip to content

Commit

Permalink
Use Option .take() or .take_unwrap() instead of util::replace where p…
Browse files Browse the repository at this point in the history
…ossible
  • Loading branch information
blake2-ppc authored and bblum committed Jul 20, 2013
1 parent 621bc79 commit 980646a
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 45 deletions.
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 @@ -697,7 +697,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 @@ -353,7 +352,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 @@ -385,7 +384,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 @@ -253,7 +253,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 @@ -267,7 +267,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
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 @@ -46,7 +46,6 @@ use rt::{context, OldTaskContext, TaskContext};
use rt::local::Local;
use task::rt::{task_id, sched_id};
use unstable::finally::Finally;
use util::replace;
use util;

#[cfg(test)] use cast;
Expand Down Expand Up @@ -224,8 +223,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 @@ -340,7 +339,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 @@ -372,8 +371,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 clone::Clone;
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

5 comments on commit 980646a

@bors
Copy link
Contributor

@bors bors commented on 980646a Jul 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at bblum@980646a

@bors
Copy link
Contributor

@bors bors commented on 980646a Jul 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging bblum/rust/kill = 980646a into auto

@bors
Copy link
Contributor

@bors bors commented on 980646a Jul 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bblum/rust/kill = 980646a merged ok, testing candidate = e3142c5

@bors
Copy link
Contributor

@bors bors commented on 980646a Jul 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = e3142c5

Please sign in to comment.