1
Fork 0

Stop using the '<->' operator

This commit is contained in:
Alex Crichton 2013-05-06 00:42:54 -04:00
parent 7d22437ecd
commit 998fececd6
29 changed files with 214 additions and 296 deletions

View file

@ -12,6 +12,7 @@
use cast::transmute_mut; use cast::transmute_mut;
use prelude::*; use prelude::*;
use util::replace;
/* /*
A dynamic, mutable location. A dynamic, mutable location.
@ -48,9 +49,7 @@ pub impl<T> Cell<T> {
fail!(~"attempt to take an empty cell"); fail!(~"attempt to take an empty cell");
} }
let mut value = None; replace(&mut self.value, None).unwrap()
value <-> self.value;
value.unwrap()
} }
/// Returns the value, failing if the cell is full. /// Returns the value, failing if the cell is full.

View file

@ -21,6 +21,7 @@ use uint;
use unstable; use unstable;
use vec; use vec;
use unstable::Exclusive; use unstable::Exclusive;
use util::replace;
use pipes::{recv, try_recv, wait_many, peek, PacketHeader}; use pipes::{recv, try_recv, wait_many, peek, PacketHeader};
@ -149,9 +150,8 @@ impl<T: Owned> GenericChan<T> for Chan<T> {
#[inline(always)] #[inline(always)]
fn send(&self, x: T) { fn send(&self, x: T) {
unsafe { unsafe {
let mut endp = None;
let mut self_endp = transmute_mut(&self.endp); let mut self_endp = transmute_mut(&self.endp);
endp <-> *self_endp; let endp = replace(self_endp, None);
*self_endp = Some(streamp::client::data(endp.unwrap(), x)) *self_endp = Some(streamp::client::data(endp.unwrap(), x))
} }
} }
@ -161,9 +161,8 @@ impl<T: Owned> GenericSmartChan<T> for Chan<T> {
#[inline(always)] #[inline(always)]
fn try_send(&self, x: T) -> bool { fn try_send(&self, x: T) -> bool {
unsafe { unsafe {
let mut endp = None;
let mut self_endp = transmute_mut(&self.endp); let mut self_endp = transmute_mut(&self.endp);
endp <-> *self_endp; let endp = replace(self_endp, None);
match streamp::client::try_data(endp.unwrap(), x) { match streamp::client::try_data(endp.unwrap(), x) {
Some(next) => { Some(next) => {
*self_endp = Some(next); *self_endp = Some(next);
@ -179,9 +178,8 @@ impl<T: Owned> GenericPort<T> for Port<T> {
#[inline(always)] #[inline(always)]
fn recv(&self) -> T { fn recv(&self) -> T {
unsafe { unsafe {
let mut endp = None;
let mut self_endp = transmute_mut(&self.endp); let mut self_endp = transmute_mut(&self.endp);
endp <-> *self_endp; let endp = replace(self_endp, None);
let streamp::data(x, endp) = recv(endp.unwrap()); let streamp::data(x, endp) = recv(endp.unwrap());
*self_endp = Some(endp); *self_endp = Some(endp);
x x
@ -191,9 +189,8 @@ impl<T: Owned> GenericPort<T> for Port<T> {
#[inline(always)] #[inline(always)]
fn try_recv(&self) -> Option<T> { fn try_recv(&self) -> Option<T> {
unsafe { unsafe {
let mut endp = None;
let mut self_endp = transmute_mut(&self.endp); let mut self_endp = transmute_mut(&self.endp);
endp <-> *self_endp; let endp = replace(self_endp, None);
match try_recv(endp.unwrap()) { match try_recv(endp.unwrap()) {
Some(streamp::data(x, endp)) => { Some(streamp::data(x, endp)) => {
*self_endp = Some(endp); *self_endp = Some(endp);
@ -209,14 +206,13 @@ impl<T: Owned> Peekable<T> for Port<T> {
#[inline(always)] #[inline(always)]
fn peek(&self) -> bool { fn peek(&self) -> bool {
unsafe { unsafe {
let mut endp = None;
let mut self_endp = transmute_mut(&self.endp); let mut self_endp = transmute_mut(&self.endp);
endp <-> *self_endp; let mut endp = replace(self_endp, None);
let peek = match endp { let peek = match endp {
Some(ref mut endp) => peek(endp), Some(ref mut endp) => peek(endp),
None => fail!(~"peeking empty stream") None => fail!(~"peeking empty stream")
}; };
*self_endp <-> endp; *self_endp = endp;
peek peek
} }
} }
@ -267,8 +263,7 @@ impl<T:Owned> GenericPort<T> for PortSet<T> {
let mut result = None; let mut result = None;
// we have to swap the ports array so we aren't borrowing // we have to swap the ports array so we aren't borrowing
// aliasable mutable memory. // aliasable mutable memory.
let mut ports = ~[]; let mut ports = replace(self_ports, ~[]);
ports <-> *self_ports;
while result.is_none() && ports.len() > 0 { while result.is_none() && ports.len() > 0 {
let i = wait_many(ports); let i = wait_many(ports);
match ports[i].try_recv() { match ports[i].try_recv() {
@ -281,7 +276,7 @@ impl<T:Owned> GenericPort<T> for PortSet<T> {
} }
} }
} }
ports <-> *self_ports; *self_ports = ports;
result result
} }
} }
@ -320,8 +315,7 @@ impl<T: Owned> GenericChan<T> for SharedChan<T> {
fn send(&self, x: T) { fn send(&self, x: T) {
let mut xx = Some(x); let mut xx = Some(x);
do self.ch.with_imm |chan| { do self.ch.with_imm |chan| {
let mut x = None; let x = replace(&mut xx, None);
x <-> xx;
chan.send(x.unwrap()) chan.send(x.unwrap())
} }
} }
@ -331,8 +325,7 @@ impl<T: Owned> GenericSmartChan<T> for SharedChan<T> {
fn try_send(&self, x: T) -> bool { fn try_send(&self, x: T) -> bool {
let mut xx = Some(x); let mut xx = Some(x);
do self.ch.with_imm |chan| { do self.ch.with_imm |chan| {
let mut x = None; let x = replace(&mut xx, None);
x <-> xx;
chan.try_send(x.unwrap()) chan.try_send(x.unwrap())
} }
} }

View file

@ -176,16 +176,13 @@ priv impl<K:Hash + Eq,V> HashMap<K, V> {
/// Expands the capacity of the array and re-insert each of the /// Expands the capacity of the array and re-insert each of the
/// existing buckets. /// existing buckets.
fn resize(&mut self, new_capacity: uint) { fn resize(&mut self, new_capacity: uint) {
let old_capacity = self.buckets.len();
self.resize_at = resize_at(new_capacity); self.resize_at = resize_at(new_capacity);
let mut old_buckets = vec::from_fn(new_capacity, |_| None); let old_buckets = replace(&mut self.buckets,
self.buckets <-> old_buckets; vec::from_fn(new_capacity, |_| None));
self.size = 0; self.size = 0;
for uint::range(0, old_capacity) |i| { do vec::consume(old_buckets) |_, bucket| {
let mut bucket = None;
bucket <-> old_buckets[i];
self.insert_opt_bucket(bucket); self.insert_opt_bucket(bucket);
} }
} }
@ -265,13 +262,11 @@ priv impl<K:Hash + Eq,V> HashMap<K, V> {
}; };
let len_buckets = self.buckets.len(); let len_buckets = self.buckets.len();
let mut bucket = None; let bucket = replace(&mut self.buckets[idx], None);
self.buckets[idx] <-> bucket;
let value = match bucket { let value = match bucket {
None => None, None => None,
Some(bucket) => { Some(Bucket{value, _}) => {
let Bucket{value: value, _} = bucket;
Some(value) Some(value)
}, },
}; };
@ -281,8 +276,7 @@ priv impl<K:Hash + Eq,V> HashMap<K, V> {
let size = self.size - 1; let size = self.size - 1;
idx = self.next_bucket(idx, len_buckets); idx = self.next_bucket(idx, len_buckets);
while self.buckets[idx].is_some() { while self.buckets[idx].is_some() {
let mut bucket = None; let bucket = replace(&mut self.buckets[idx], None);
bucket <-> self.buckets[idx];
self.insert_opt_bucket(bucket); self.insert_opt_bucket(bucket);
idx = self.next_bucket(idx, len_buckets); idx = self.next_bucket(idx, len_buckets);
} }
@ -613,15 +607,13 @@ pub impl<K: Hash + Eq, V> HashMap<K, V> {
} }
fn consume(&mut self, f: &fn(K, V)) { fn consume(&mut self, f: &fn(K, V)) {
let mut buckets = ~[]; let buckets = replace(&mut self.buckets, ~[]);
self.buckets <-> buckets;
self.size = 0; self.size = 0;
do vec::consume(buckets) |_, bucket| { do vec::consume(buckets) |_, bucket| {
match bucket { match bucket {
None => {}, None => {},
Some(bucket) => { Some(Bucket{key, value, _}) => {
let Bucket{key: key, value: value, _} = bucket;
f(key, value) f(key, value)
} }
} }

View file

@ -93,6 +93,7 @@ use unstable::intrinsics;
use ptr; use ptr;
use task; use task;
use vec; use vec;
use util::replace;
static SPIN_COUNT: uint = 0; static SPIN_COUNT: uint = 0;
@ -428,8 +429,7 @@ fn try_recv_<T:Owned>(p: &mut Packet<T>) -> Option<T> {
// optimistic path // optimistic path
match p.header.state { match p.header.state {
Full => { Full => {
let mut payload = None; let payload = replace(&mut p.payload, None);
payload <-> p.payload;
p.header.state = Empty; p.header.state = Empty;
return Some(payload.unwrap()) return Some(payload.unwrap())
}, },
@ -480,8 +480,7 @@ fn try_recv_<T:Owned>(p: &mut Packet<T>) -> Option<T> {
fail!(~"blocking on already blocked packet") fail!(~"blocking on already blocked packet")
}, },
Full => { Full => {
let mut payload = None; let payload = replace(&mut p.payload, None);
payload <-> p.payload;
let old_task = swap_task(&mut p.header.blocked_task, ptr::null()); let old_task = swap_task(&mut p.header.blocked_task, ptr::null());
if !old_task.is_null() { if !old_task.is_null() {
unsafe { unsafe {
@ -675,8 +674,7 @@ impl<T:Owned,Tbuffer:Owned> Drop for SendPacketBuffered<T,Tbuffer> {
unsafe { unsafe {
let this: &mut SendPacketBuffered<T,Tbuffer> = transmute(self); let this: &mut SendPacketBuffered<T,Tbuffer> = transmute(self);
if this.p != None { if this.p != None {
let mut p = None; let p = replace(&mut this.p, None);
p <-> this.p;
sender_terminate(p.unwrap()) sender_terminate(p.unwrap())
} }
} }
@ -695,9 +693,7 @@ pub fn SendPacketBuffered<T,Tbuffer>(p: *mut Packet<T>)
pub impl<T,Tbuffer> SendPacketBuffered<T,Tbuffer> { pub impl<T,Tbuffer> SendPacketBuffered<T,Tbuffer> {
fn unwrap(&mut self) -> *mut Packet<T> { fn unwrap(&mut self) -> *mut Packet<T> {
let mut p = None; replace(&mut self.p, None).unwrap()
p <-> self.p;
p.unwrap()
} }
fn header(&mut self) -> *mut PacketHeader { fn header(&mut self) -> *mut PacketHeader {
@ -713,9 +709,7 @@ pub impl<T,Tbuffer> SendPacketBuffered<T,Tbuffer> {
fn reuse_buffer(&mut self) -> BufferResource<Tbuffer> { fn reuse_buffer(&mut self) -> BufferResource<Tbuffer> {
//error!("send reuse_buffer"); //error!("send reuse_buffer");
let mut tmp = None; replace(&mut self.buffer, None).unwrap()
tmp <-> self.buffer;
tmp.unwrap()
} }
} }
@ -738,8 +732,7 @@ impl<T:Owned,Tbuffer:Owned> Drop for RecvPacketBuffered<T,Tbuffer> {
unsafe { unsafe {
let this: &mut RecvPacketBuffered<T,Tbuffer> = transmute(self); let this: &mut RecvPacketBuffered<T,Tbuffer> = transmute(self);
if this.p != None { if this.p != None {
let mut p = None; let p = replace(&mut this.p, None);
p <-> this.p;
receiver_terminate(p.unwrap()) receiver_terminate(p.unwrap())
} }
} }
@ -748,15 +741,11 @@ impl<T:Owned,Tbuffer:Owned> Drop for RecvPacketBuffered<T,Tbuffer> {
pub impl<T:Owned,Tbuffer:Owned> RecvPacketBuffered<T, Tbuffer> { pub impl<T:Owned,Tbuffer:Owned> RecvPacketBuffered<T, Tbuffer> {
fn unwrap(&mut self) -> *mut Packet<T> { fn unwrap(&mut self) -> *mut Packet<T> {
let mut p = None; replace(&mut self.p, None).unwrap()
p <-> self.p;
p.unwrap()
} }
fn reuse_buffer(&mut self) -> BufferResource<Tbuffer> { fn reuse_buffer(&mut self) -> BufferResource<Tbuffer> {
let mut tmp = None; replace(&mut self.buffer, None).unwrap()
tmp <-> self.buffer;
tmp.unwrap()
} }
} }

View file

@ -35,12 +35,12 @@ pub fn ignore<T>(_x: T) { }
#[inline(always)] #[inline(always)]
pub fn with<T,R>( pub fn with<T,R>(
ptr: @mut T, ptr: @mut T,
mut value: T, value: T,
op: &fn() -> R) -> R op: &fn() -> R) -> R
{ {
value <-> *ptr; let prev = replace(ptr, value);
let result = op(); let result = op();
*ptr = value; *ptr = prev;
return result; return result;
} }

View file

@ -29,6 +29,7 @@ use sys;
use uint; use uint;
use unstable::intrinsics; use unstable::intrinsics;
use vec; use vec;
use util;
#[cfg(not(test))] use cmp::Equiv; #[cfg(not(test))] use cmp::Equiv;
@ -470,7 +471,7 @@ pub fn shift<T>(v: &mut ~[T]) -> T {
let next_ln = v.len() - 1; let next_ln = v.len() - 1;
// Save the last element. We're going to overwrite its position // Save the last element. We're going to overwrite its position
let mut work_elt = v.pop(); let work_elt = v.pop();
// We still should have room to work where what last element was // We still should have room to work where what last element was
assert!(capacity(v) >= ln); assert!(capacity(v) >= ln);
// Pretend like we have the original length so we can use // Pretend like we have the original length so we can use
@ -501,16 +502,14 @@ pub fn shift<T>(v: &mut ~[T]) -> T {
// Swap out the element we want from the end // Swap out the element we want from the end
let vp = raw::to_mut_ptr(*v); let vp = raw::to_mut_ptr(*v);
let vp = ptr::mut_offset(vp, next_ln - 1); let vp = ptr::mut_offset(vp, next_ln - 1);
*vp <-> work_elt;
work_elt util::replace_ptr(vp, work_elt)
} }
} }
/// Prepend an element to the vector /// Prepend an element to the vector
pub fn unshift<T>(v: &mut ~[T], x: T) { pub fn unshift<T>(v: &mut ~[T], x: T) {
let mut vv = ~[x]; let vv = util::replace(v, ~[x]);
*v <-> vv;
v.push_all_move(vv); v.push_all_move(vv);
} }
@ -523,7 +522,7 @@ pub fn insert<T>(v: &mut ~[T], i: uint, x: T) {
v.push(x); v.push(x);
let mut j = len; let mut j = len;
while j > i { while j > i {
v[j] <-> v[j - 1]; swap(*v, j, j - 1);
j -= 1; j -= 1;
} }
} }
@ -536,7 +535,7 @@ pub fn remove<T>(v: &mut ~[T], i: uint) -> T {
let mut j = i; let mut j = i;
while j < len - 1 { while j < len - 1 {
v[j] <-> v[j + 1]; swap(*v, j, j + 1);
j += 1; j += 1;
} }
v.pop() v.pop()
@ -550,10 +549,9 @@ pub fn consume<T>(mut v: ~[T], f: &fn(uint, v: T)) {
// holes we create in the vector. That ensures that, if the // holes we create in the vector. That ensures that, if the
// iterator fails then we won't try to clean up the consumed // iterator fails then we won't try to clean up the consumed
// elements during unwinding // elements during unwinding
let mut x = intrinsics::init(); let x = intrinsics::init();
let p = ptr::mut_offset(p, i); let p = ptr::mut_offset(p, i);
x <-> *p; f(i, util::replace_ptr(p, x));
f(i, x);
} }
} }
@ -572,10 +570,9 @@ pub fn consume_reverse<T>(mut v: ~[T], f: &fn(uint, v: T)) {
// holes we create in the vector. That ensures that, if the // holes we create in the vector. That ensures that, if the
// iterator fails then we won't try to clean up the consumed // iterator fails then we won't try to clean up the consumed
// elements during unwinding // elements during unwinding
let mut x = intrinsics::init(); let x = intrinsics::init();
let p = ptr::mut_offset(p, i); let p = ptr::mut_offset(p, i);
x <-> *p; f(i, util::replace_ptr(p, x));
f(i, x);
} }
} }
@ -592,8 +589,7 @@ pub fn pop<T>(v: &mut ~[T]) -> T {
} }
let valptr = ptr::to_mut_unsafe_ptr(&mut v[ln - 1u]); let valptr = ptr::to_mut_unsafe_ptr(&mut v[ln - 1u]);
unsafe { unsafe {
let mut val = intrinsics::uninit(); let val = util::replace_ptr(valptr, intrinsics::uninit());
val <-> *valptr;
raw::set_len(v, ln - 1u); raw::set_len(v, ln - 1u);
val val
} }
@ -607,8 +603,7 @@ pub fn pop<T>(v: &mut ~[T]) -> T {
} }
let valptr = ptr::to_mut_unsafe_ptr(&mut v[ln - 1u]); let valptr = ptr::to_mut_unsafe_ptr(&mut v[ln - 1u]);
unsafe { unsafe {
let mut val = intrinsics::init(); let val = util::replace_ptr(valptr, intrinsics::init());
val <-> *valptr;
raw::set_len(v, ln - 1u); raw::set_len(v, ln - 1u);
val val
} }
@ -626,7 +621,7 @@ pub fn swap_remove<T>(v: &mut ~[T], index: uint) -> T {
fail!(fmt!("vec::swap_remove - index %u >= length %u", index, ln)); fail!(fmt!("vec::swap_remove - index %u >= length %u", index, ln));
} }
if index < ln - 1 { if index < ln - 1 {
v[index] <-> v[ln - 1]; swap(*v, index, ln - 1);
} }
v.pop() v.pop()
} }
@ -682,8 +677,8 @@ pub fn push_all_move<T>(v: &mut ~[T], mut rhs: ~[T]) {
unsafe { unsafe {
do as_mut_buf(rhs) |p, len| { do as_mut_buf(rhs) |p, len| {
for uint::range(0, len) |i| { for uint::range(0, len) |i| {
let mut x = intrinsics::uninit(); let x = util::replace_ptr(ptr::mut_offset(p, i),
x <-> *ptr::mut_offset(p, i); intrinsics::uninit());
push(&mut *v, x); push(&mut *v, x);
} }
} }
@ -699,8 +694,8 @@ pub fn push_all_move<T>(v: &mut ~[T], mut rhs: ~[T]) {
unsafe { unsafe {
do as_mut_buf(rhs) |p, len| { do as_mut_buf(rhs) |p, len| {
for uint::range(0, len) |i| { for uint::range(0, len) |i| {
let mut x = intrinsics::init(); let x = util::replace_ptr(ptr::mut_offset(p, i),
x <-> *ptr::mut_offset(p, i); intrinsics::init());
push(&mut *v, x); push(&mut *v, x);
} }
} }
@ -716,8 +711,7 @@ pub fn truncate<T>(v: &mut ~[T], newlen: uint) {
unsafe { unsafe {
// This loop is optimized out for non-drop types. // This loop is optimized out for non-drop types.
for uint::range(newlen, oldlen) |i| { for uint::range(newlen, oldlen) |i| {
let mut dropped = intrinsics::uninit(); util::replace_ptr(ptr::mut_offset(p, i), intrinsics::uninit());
dropped <-> *ptr::mut_offset(p, i);
} }
} }
} }
@ -732,8 +726,7 @@ pub fn truncate<T>(v: &mut ~[T], newlen: uint) {
unsafe { unsafe {
// This loop is optimized out for non-drop types. // This loop is optimized out for non-drop types.
for uint::range(newlen, oldlen) |i| { for uint::range(newlen, oldlen) |i| {
let mut dropped = intrinsics::init(); util::replace_ptr(ptr::mut_offset(p, i), intrinsics::init());
dropped <-> *ptr::mut_offset(p, i);
} }
} }
} }
@ -758,14 +751,14 @@ pub fn dedup<T:Eq>(v: &mut ~[T]) {
// last_written < next_to_read < ln // last_written < next_to_read < ln
if *ptr::mut_offset(p, next_to_read) == if *ptr::mut_offset(p, next_to_read) ==
*ptr::mut_offset(p, last_written) { *ptr::mut_offset(p, last_written) {
let mut dropped = intrinsics::uninit(); util::replace_ptr(ptr::mut_offset(p, next_to_read),
dropped <-> *ptr::mut_offset(p, next_to_read); intrinsics::uninit());
} else { } else {
last_written += 1; last_written += 1;
// last_written <= next_to_read < ln // last_written <= next_to_read < ln
if next_to_read != last_written { if next_to_read != last_written {
*ptr::mut_offset(p, last_written) <-> util::swap_ptr(ptr::mut_offset(p, last_written),
*ptr::mut_offset(p, next_to_read); ptr::mut_offset(p, next_to_read));
} }
} }
// last_written <= next_to_read < ln // last_written <= next_to_read < ln
@ -796,14 +789,14 @@ pub fn dedup<T:Eq>(v: &mut ~[T]) {
// last_written < next_to_read < ln // last_written < next_to_read < ln
if *ptr::mut_offset(p, next_to_read) == if *ptr::mut_offset(p, next_to_read) ==
*ptr::mut_offset(p, last_written) { *ptr::mut_offset(p, last_written) {
let mut dropped = intrinsics::init(); util::replace_ptr(ptr::mut_offset(p, next_to_read),
dropped <-> *ptr::mut_offset(p, next_to_read); intrinsics::init());
} else { } else {
last_written += 1; last_written += 1;
// last_written <= next_to_read < ln // last_written <= next_to_read < ln
if next_to_read != last_written { if next_to_read != last_written {
*ptr::mut_offset(p, last_written) <-> util::swap_ptr(ptr::mut_offset(p, last_written),
*ptr::mut_offset(p, next_to_read); ptr::mut_offset(p, next_to_read));
} }
} }
// last_written <= next_to_read < ln // last_written <= next_to_read < ln
@ -1028,7 +1021,7 @@ pub fn retain<T>(v: &mut ~[T], f: &fn(t: &T) -> bool) {
if !f(&v[i]) { if !f(&v[i]) {
deleted += 1; deleted += 1;
} else if deleted > 0 { } else if deleted > 0 {
v[i - deleted] <-> v[i]; swap(*v, i - deleted, i);
} }
} }
@ -1429,15 +1422,25 @@ pub fn zip<T, U>(mut v: ~[T], mut u: ~[U]) -> ~[(T, U)] {
* * a - The index of the first element * * a - The index of the first element
* * b - The index of the second element * * b - The index of the second element
*/ */
#[inline(always)]
pub fn swap<T>(v: &mut [T], a: uint, b: uint) { pub fn swap<T>(v: &mut [T], a: uint, b: uint) {
v[a] <-> v[b]; unsafe {
// Can't take two mutable loans from one vector, so instead just cast
// them to their raw pointers to do the swap
let pa: *mut T = ptr::to_mut_unsafe_ptr(&mut v[a]);
let pb: *mut T = ptr::to_mut_unsafe_ptr(&mut v[b]);
util::swap_ptr(pa, pb);
}
} }
/// Reverse the order of elements in a vector, in place /// Reverse the order of elements in a vector, in place
pub fn reverse<T>(v: &mut [T]) { pub fn reverse<T>(v: &mut [T]) {
let mut i: uint = 0; let mut i: uint = 0;
let ln = len::<T>(v); let ln = len::<T>(v);
while i < ln / 2 { v[i] <-> v[ln - i - 1]; i += 1; } while i < ln / 2 {
swap(v, i, ln - i - 1);
i += 1;
}
} }
/// Returns a vector with the order of elements reversed /// Returns a vector with the order of elements reversed
@ -2476,6 +2479,7 @@ pub mod raw {
use sys; use sys;
use unstable::intrinsics; use unstable::intrinsics;
use vec::{UnboxedVecRepr, as_const_buf, as_mut_buf, len, with_capacity}; use vec::{UnboxedVecRepr, as_const_buf, as_mut_buf, len, with_capacity};
use util;
/// The internal representation of a (boxed) vector /// The internal representation of a (boxed) vector
pub struct VecRepr { pub struct VecRepr {
@ -2573,8 +2577,7 @@ pub mod raw {
pub unsafe fn init_elem<T>(v: &mut [T], i: uint, val: T) { pub unsafe fn init_elem<T>(v: &mut [T], i: uint, val: T) {
let mut box = Some(val); let mut box = Some(val);
do as_mut_buf(v) |p, _len| { do as_mut_buf(v) |p, _len| {
let mut box2 = None; let box2 = util::replace(&mut box, None);
box2 <-> box;
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)),
box2.unwrap()); box2.unwrap());
} }

View file

@ -10,6 +10,8 @@
//! A double-ended queue implemented as a circular buffer //! A double-ended queue implemented as a circular buffer
use core::util::replace;
static initial_capacity: uint = 32u; // 2^5 static initial_capacity: uint = 32u; // 2^5
pub struct Deque<T> { pub struct Deque<T> {
@ -142,9 +144,7 @@ fn grow<T>(nelts: uint, lo: uint, elts: &mut [Option<T>]) -> ~[Option<T>] {
let mut rv = ~[]; let mut rv = ~[];
do rv.grow_fn(nelts + 1) |i| { do rv.grow_fn(nelts + 1) |i| {
let mut element = None; replace(&mut elts[(lo + i) % nelts], None)
element <-> elts[(lo + i) % nelts];
element
} }
rv rv

View file

@ -26,6 +26,7 @@ use core::cell::Cell;
use core::comm::{PortOne, oneshot, send_one}; use core::comm::{PortOne, oneshot, send_one};
use core::pipes::recv; use core::pipes::recv;
use core::task; use core::task;
use core::util::replace;
#[doc = "The future type"] #[doc = "The future type"]
#[cfg(stage0)] #[cfg(stage0)]
@ -77,8 +78,7 @@ pub impl<A> Future<A> {
} }
} }
{ {
let mut state = Evaluating; let state = replace(&mut self.state, Evaluating);
self.state <-> state;
match state { match state {
Forced(_) | Evaluating => fail!(~"Logic error."), Forced(_) | Evaluating => fail!(~"Logic error."),
Pending(f) => { Pending(f) => {
@ -108,8 +108,7 @@ pub impl<A> Future<A> {
} }
} }
{ {
let mut state = Evaluating; let state = replace(&mut self.state, Evaluating);
self.state <-> state;
match state { match state {
Forced(_) | Evaluating => fail!(~"Logic error."), Forced(_) | Evaluating => fail!(~"Logic error."),
Pending(f) => { Pending(f) => {

View file

@ -11,6 +11,7 @@
//! A priority queue implemented with a binary heap //! A priority queue implemented with a binary heap
use core::old_iter::BaseIter; use core::old_iter::BaseIter;
use core::util::{replace, swap};
#[abi = "rust-intrinsic"] #[abi = "rust-intrinsic"]
extern "rust-intrinsic" mod rusti { extern "rust-intrinsic" mod rusti {
@ -73,7 +74,10 @@ pub impl <T:Ord> PriorityQueue<T> {
/// Pop the greatest item from the queue - fails if empty /// Pop the greatest item from the queue - fails if empty
fn pop(&mut self) -> T { fn pop(&mut self) -> T {
let mut item = self.data.pop(); let mut item = self.data.pop();
if !self.is_empty() { item <-> self.data[0]; self.siftdown(0); } if !self.is_empty() {
swap(&mut item, &mut self.data[0]);
self.siftdown(0);
}
item item
} }
@ -92,7 +96,7 @@ pub impl <T:Ord> PriorityQueue<T> {
/// Optimized version of a push followed by a pop /// Optimized version of a push followed by a pop
fn push_pop(&mut self, mut item: T) -> T { fn push_pop(&mut self, mut item: T) -> T {
if !self.is_empty() && self.data[0] > item { if !self.is_empty() && self.data[0] > item {
item <-> self.data[0]; swap(&mut item, &mut self.data[0]);
self.siftdown(0); self.siftdown(0);
} }
item item
@ -100,7 +104,7 @@ pub impl <T:Ord> PriorityQueue<T> {
/// Optimized version of a pop followed by a push - fails if empty /// Optimized version of a pop followed by a push - fails if empty
fn replace(&mut self, mut item: T) -> T { fn replace(&mut self, mut item: T) -> T {
item <-> self.data[0]; swap(&mut item, &mut self.data[0]);
self.siftdown(0); self.siftdown(0);
item item
} }
@ -115,7 +119,7 @@ pub impl <T:Ord> PriorityQueue<T> {
let mut end = q.len(); let mut end = q.len();
while end > 1 { while end > 1 {
end -= 1; end -= 1;
q.data[end] <-> q.data[0]; vec::swap(q.data, 0, end);
q.siftdown_range(0, end) q.siftdown_range(0, end)
} }
q.to_vec() q.to_vec()
@ -149,8 +153,7 @@ pub impl <T:Ord> PriorityQueue<T> {
while pos > start { while pos > start {
let parent = (pos - 1) >> 1; let parent = (pos - 1) >> 1;
if new > self.data[parent] { if new > self.data[parent] {
let mut x = rusti::uninit(); let x = replace(&mut self.data[parent], rusti::uninit());
x <-> self.data[parent];
rusti::move_val_init(&mut self.data[pos], x); rusti::move_val_init(&mut self.data[pos], x);
pos = parent; pos = parent;
loop loop
@ -169,8 +172,7 @@ pub impl <T:Ord> PriorityQueue<T> {
while pos > start { while pos > start {
let parent = (pos - 1) >> 1; let parent = (pos - 1) >> 1;
if new > self.data[parent] { if new > self.data[parent] {
let mut x = rusti::init(); let x = replace(&mut self.data[parent], rusti::init());
x <-> self.data[parent];
rusti::move_val_init(&mut self.data[pos], x); rusti::move_val_init(&mut self.data[pos], x);
pos = parent; pos = parent;
loop loop
@ -194,8 +196,7 @@ pub impl <T:Ord> PriorityQueue<T> {
if right < end && !(self.data[child] > self.data[right]) { if right < end && !(self.data[child] > self.data[right]) {
child = right; child = right;
} }
let mut x = rusti::uninit(); let x = replace(&mut self.data[child], rusti::uninit());
x <-> self.data[child];
rusti::move_val_init(&mut self.data[pos], x); rusti::move_val_init(&mut self.data[pos], x);
pos = child; pos = child;
child = 2 * pos + 1; child = 2 * pos + 1;
@ -218,8 +219,7 @@ pub impl <T:Ord> PriorityQueue<T> {
if right < end && !(self.data[child] > self.data[right]) { if right < end && !(self.data[child] > self.data[right]) {
child = right; child = right;
} }
let mut x = rusti::init(); let x = replace(&mut self.data[child], rusti::init());
x <-> self.data[child];
rusti::move_val_init(&mut self.data[pos], x); rusti::move_val_init(&mut self.data[pos], x);
pos = child; pos = child;
child = 2 * pos + 1; child = 2 * pos + 1;

View file

@ -17,6 +17,7 @@ destruction. They are restricted to containing `Owned` types in order to prevent
use core::libc::{c_void, size_t, malloc, free}; use core::libc::{c_void, size_t, malloc, free};
use core::unstable::intrinsics; use core::unstable::intrinsics;
use core::util;
struct RcBox<T> { struct RcBox<T> {
value: T, value: T,
@ -52,8 +53,7 @@ impl<T: Owned> Drop for Rc<T> {
unsafe { unsafe {
(*self.ptr).count -= 1; (*self.ptr).count -= 1;
if (*self.ptr).count == 0 { if (*self.ptr).count == 0 {
let mut x = intrinsics::uninit(); util::replace_ptr(self.ptr, intrinsics::uninit());
x <-> *self.ptr;
free(self.ptr as *c_void) free(self.ptr as *c_void)
} }
} }
@ -67,8 +67,7 @@ impl<T: Owned> Drop for Rc<T> {
unsafe { unsafe {
(*self.ptr).count -= 1; (*self.ptr).count -= 1;
if (*self.ptr).count == 0 { if (*self.ptr).count == 0 {
let mut x = intrinsics::init(); util::replace_ptr(self.ptr, intrinsics::init());
x <-> *self.ptr;
free(self.ptr as *c_void) free(self.ptr as *c_void)
} }
} }
@ -111,13 +110,6 @@ mod test_rc {
} }
} }
#[abi = "rust-intrinsic"]
extern "rust-intrinsic" mod rusti {
fn init<T>() -> T;
#[cfg(not(stage0))]
fn uninit<T>() -> T;
}
#[deriving(Eq)] #[deriving(Eq)]
enum Borrow { enum Borrow {
Mutable, Mutable,
@ -179,8 +171,7 @@ impl<T: Owned> Drop for RcMut<T> {
unsafe { unsafe {
(*self.ptr).count -= 1; (*self.ptr).count -= 1;
if (*self.ptr).count == 0 { if (*self.ptr).count == 0 {
let mut x = rusti::uninit(); util::replace_ptr(self.ptr, intrinsics::uninit());
x <-> *self.ptr;
free(self.ptr as *c_void) free(self.ptr as *c_void)
} }
} }
@ -194,8 +185,7 @@ impl<T: Owned> Drop for RcMut<T> {
unsafe { unsafe {
(*self.ptr).count -= 1; (*self.ptr).count -= 1;
if (*self.ptr).count == 0 { if (*self.ptr).count == 0 {
let mut x = rusti::init(); util::replace_ptr(self.ptr, intrinsics::init());
x <-> *self.ptr;
free(self.ptr as *c_void) free(self.ptr as *c_void)
} }
} }

View file

@ -13,6 +13,7 @@
use core::cmp::{Eq, Ord}; use core::cmp::{Eq, Ord};
use core::vec::len; use core::vec::len;
use core::vec; use core::vec;
use core::util::swap;
type Le<'self, T> = &'self fn(v1: &T, v2: &T) -> bool; type Le<'self, T> = &'self fn(v1: &T, v2: &T) -> bool;
@ -63,36 +64,36 @@ pub fn merge_sort<T:Copy>(v: &[T], le: Le<T>) -> ~[T] {
#[cfg(stage0)] #[cfg(stage0)]
fn part<T>(arr: &mut [T], left: uint, fn part<T>(arr: &mut [T], left: uint,
right: uint, pivot: uint, compare_func: Le<T>) -> uint { right: uint, pivot: uint, compare_func: Le<T>) -> uint {
arr[pivot] <-> arr[right]; swap(&mut arr[pivot], &mut arr[right]);
let mut storage_index: uint = left; let mut storage_index: uint = left;
let mut i: uint = left; let mut i: uint = left;
while i < right { while i < right {
let a: &mut T = &mut arr[i]; let a: &mut T = &mut arr[i];
let b: &mut T = &mut arr[right]; let b: &mut T = &mut arr[right];
if compare_func(a, b) { if compare_func(a, b) {
arr[i] <-> arr[storage_index]; swap(&mut arr[i], &mut arr[storage_index]);
storage_index += 1; storage_index += 1;
} }
i += 1; i += 1;
} }
arr[storage_index] <-> arr[right]; swap(&mut arr[storage_index], &mut arr[right]);
return storage_index; return storage_index;
} }
#[cfg(not(stage0))] #[cfg(not(stage0))]
fn part<T>(arr: &mut [T], left: uint, fn part<T>(arr: &mut [T], left: uint,
right: uint, pivot: uint, compare_func: Le<T>) -> uint { right: uint, pivot: uint, compare_func: Le<T>) -> uint {
arr[pivot] <-> arr[right]; vec::swap(arr, pivot, right);
let mut storage_index: uint = left; let mut storage_index: uint = left;
let mut i: uint = left; let mut i: uint = left;
while i < right { while i < right {
if compare_func(&arr[i], &arr[right]) { if compare_func(&arr[i], &arr[right]) {
arr[i] <-> arr[storage_index]; vec::swap(arr, i, storage_index);
storage_index += 1; storage_index += 1;
} }
i += 1; i += 1;
} }
arr[storage_index] <-> arr[right]; vec::swap(arr, storage_index, right);
return storage_index; return storage_index;
} }
@ -136,29 +137,29 @@ fn qsort3<T:Copy + Ord + Eq>(arr: &mut [T], left: int, right: int) {
j -= 1; j -= 1;
} }
if i >= j { break; } if i >= j { break; }
arr[i] <-> arr[j]; vec::swap(arr, i as uint, j as uint);
if arr[i] == v { if arr[i] == v {
p += 1; p += 1;
arr[p] <-> arr[i]; vec::swap(arr, p as uint, i as uint);
} }
if v == arr[j] { if v == arr[j] {
q -= 1; q -= 1;
arr[j] <-> arr[q]; vec::swap(arr, j as uint, q as uint);
} }
} }
arr[i] <-> arr[right]; vec::swap(arr, i as uint, right as uint);
j = i - 1; j = i - 1;
i += 1; i += 1;
let mut k: int = left; let mut k: int = left;
while k < p { while k < p {
arr[k] <-> arr[j]; vec::swap(arr, k as uint, j as uint);
k += 1; k += 1;
j -= 1; j -= 1;
if k == len::<T>(arr) as int { break; } if k == len::<T>(arr) as int { break; }
} }
k = right - 1; k = right - 1;
while k > q { while k > q {
arr[i] <-> arr[k]; vec::swap(arr, i as uint, k as uint);
k -= 1; k -= 1;
i += 1; i += 1;
if k == 0 { break; } if k == 0 { break; }
@ -273,7 +274,7 @@ fn binarysort<T:Copy + Ord>(array: &mut [T], start: uint) {
fn reverse_slice<T>(v: &mut [T], start: uint, end:uint) { fn reverse_slice<T>(v: &mut [T], start: uint, end:uint) {
let mut i = start; let mut i = start;
while i < end / 2 { while i < end / 2 {
v[i] <-> v[end - i - 1]; vec::swap(v, i, end - i - 1);
i += 1; i += 1;
} }
} }
@ -493,7 +494,7 @@ impl<T:Copy + Ord> MergeState<T> {
let mut len1 = len1; let mut len1 = len1;
let mut len2 = len2; let mut len2 = len2;
array[dest] <-> array[c2]; vec::swap(array, dest, c2);
dest += 1; c2 += 1; len2 -= 1; dest += 1; c2 += 1; len2 -= 1;
if len2 == 0 { if len2 == 0 {
@ -502,7 +503,7 @@ impl<T:Copy + Ord> MergeState<T> {
} }
if len1 == 1 { if len1 == 1 {
shift_vec(array, dest, c2, len2); shift_vec(array, dest, c2, len2);
array[dest+len2] <-> tmp[c1]; swap(&mut tmp[c1], &mut array[dest+len2]);
return; return;
} }
@ -515,14 +516,14 @@ impl<T:Copy + Ord> MergeState<T> {
loop { loop {
assert!(len1 > 1 && len2 != 0); assert!(len1 > 1 && len2 != 0);
if array[c2] < tmp[c1] { if array[c2] < tmp[c1] {
array[dest] <-> array[c2]; vec::swap(array, dest, c2);
dest += 1; c2 += 1; len2 -= 1; dest += 1; c2 += 1; len2 -= 1;
count2 += 1; count1 = 0; count2 += 1; count1 = 0;
if len2 == 0 { if len2 == 0 {
break_outer = true; break_outer = true;
} }
} else { } else {
array[dest] <-> tmp[c1]; swap(&mut array[dest], &mut tmp[c1]);
dest += 1; c1 += 1; len1 -= 1; dest += 1; c1 += 1; len1 -= 1;
count1 += 1; count2 = 0; count1 += 1; count2 = 0;
if len1 == 1 { if len1 == 1 {
@ -548,7 +549,7 @@ impl<T:Copy + Ord> MergeState<T> {
dest += count1; c1 += count1; len1 -= count1; dest += count1; c1 += count1; len1 -= count1;
if len1 <= 1 { break_outer = true; break; } if len1 <= 1 { break_outer = true; break; }
} }
array[dest] <-> array[c2]; vec::swap(array, dest, c2);
dest += 1; c2 += 1; len2 -= 1; dest += 1; c2 += 1; len2 -= 1;
if len2 == 0 { break_outer = true; break; } if len2 == 0 { break_outer = true; break; }
@ -561,7 +562,7 @@ impl<T:Copy + Ord> MergeState<T> {
dest += count2; c2 += count2; len2 -= count2; dest += count2; c2 += count2; len2 -= count2;
if len2 == 0 { break_outer = true; break; } if len2 == 0 { break_outer = true; break; }
} }
array[dest] <-> tmp[c1]; swap(&mut array[dest], &mut tmp[c1]);
dest += 1; c1 += 1; len1 -= 1; dest += 1; c1 += 1; len1 -= 1;
if len1 == 1 { break_outer = true; break; } if len1 == 1 { break_outer = true; break; }
min_gallop -= 1; min_gallop -= 1;
@ -578,7 +579,7 @@ impl<T:Copy + Ord> MergeState<T> {
if len1 == 1 { if len1 == 1 {
assert!(len2 > 0); assert!(len2 > 0);
shift_vec(array, dest, c2, len2); shift_vec(array, dest, c2, len2);
array[dest+len2] <-> tmp[c1]; swap(&mut array[dest+len2], &mut tmp[c1]);
} else if len1 == 0 { } else if len1 == 0 {
fail!(~"Comparison violates its contract!"); fail!(~"Comparison violates its contract!");
} else { } else {
@ -603,7 +604,7 @@ impl<T:Copy + Ord> MergeState<T> {
let mut len1 = len1; let mut len1 = len1;
let mut len2 = len2; let mut len2 = len2;
array[dest] <-> array[c1]; vec::swap(array, dest, c1);
dest -= 1; c1 -= 1; len1 -= 1; dest -= 1; c1 -= 1; len1 -= 1;
if len1 == 0 { if len1 == 0 {
@ -614,7 +615,7 @@ impl<T:Copy + Ord> MergeState<T> {
dest -= len1; dest -= len1;
c1 -= len1; c1 -= len1;
shift_vec(array, dest+1, c1+1, len1); shift_vec(array, dest+1, c1+1, len1);
array[dest] <-> tmp[c2]; swap(&mut array[dest], &mut tmp[c2]);
return; return;
} }
@ -627,14 +628,14 @@ impl<T:Copy + Ord> MergeState<T> {
loop { loop {
assert!(len1 != 0 && len2 > 1); assert!(len1 != 0 && len2 > 1);
if tmp[c2] < array[c1] { if tmp[c2] < array[c1] {
array[dest] <-> array[c1]; vec::swap(array, dest, c1);
dest -= 1; c1 -= 1; len1 -= 1; dest -= 1; c1 -= 1; len1 -= 1;
count1 += 1; count2 = 0; count1 += 1; count2 = 0;
if len1 == 0 { if len1 == 0 {
break_outer = true; break_outer = true;
} }
} else { } else {
array[dest] <-> tmp[c2]; swap(&mut array[dest], &mut tmp[c2]);
dest -= 1; c2 -= 1; len2 -= 1; dest -= 1; c2 -= 1; len2 -= 1;
count2 += 1; count1 = 0; count2 += 1; count1 = 0;
if len2 == 1 { if len2 == 1 {
@ -663,7 +664,7 @@ impl<T:Copy + Ord> MergeState<T> {
if len1 == 0 { break_outer = true; break; } if len1 == 0 { break_outer = true; break; }
} }
array[dest] <-> tmp[c2]; swap(&mut array[dest], &mut tmp[c2]);
dest -= 1; c2 -= 1; len2 -= 1; dest -= 1; c2 -= 1; len2 -= 1;
if len2 == 1 { break_outer = true; break; } if len2 == 1 { break_outer = true; break; }
@ -680,7 +681,7 @@ impl<T:Copy + Ord> MergeState<T> {
copy_vec(array, dest+1, tmp.slice(c2+1, c2+1+count2)); copy_vec(array, dest+1, tmp.slice(c2+1, c2+1+count2));
if len2 <= 1 { break_outer = true; break; } if len2 <= 1 { break_outer = true; break; }
} }
array[dest] <-> array[c1]; vec::swap(array, dest, c1);
dest -= 1; c1 -= 1; len1 -= 1; dest -= 1; c1 -= 1; len1 -= 1;
if len1 == 0 { break_outer = true; break; } if len1 == 0 { break_outer = true; break; }
min_gallop -= 1; min_gallop -= 1;
@ -700,7 +701,7 @@ impl<T:Copy + Ord> MergeState<T> {
dest -= len1; dest -= len1;
c1 -= len1; c1 -= len1;
shift_vec(array, dest+1, c1+1, len1); shift_vec(array, dest+1, c1+1, len1);
array[dest] <-> tmp[c2]; swap(&mut array[dest], &mut tmp[c2]);
} else if len2 == 0 { } else if len2 == 0 {
fail!(~"Comparison violates its contract!"); fail!(~"Comparison violates its contract!");
} else { } else {
@ -1090,7 +1091,7 @@ mod big_tests {
for 3.times { for 3.times {
let i1 = rng.gen_uint_range(0, n); let i1 = rng.gen_uint_range(0, n);
let i2 = rng.gen_uint_range(0, n); let i2 = rng.gen_uint_range(0, n);
arr[i1] <-> arr[i2]; vec::swap(arr, i1, i2);
} }
tim_sort(arr); // 3sort tim_sort(arr); // 3sort
isSorted(arr); isSorted(arr);
@ -1162,7 +1163,7 @@ mod big_tests {
for 3.times { for 3.times {
let i1 = rng.gen_uint_range(0, n); let i1 = rng.gen_uint_range(0, n);
let i2 = rng.gen_uint_range(0, n); let i2 = rng.gen_uint_range(0, n);
arr[i1] <-> arr[i2]; vec::swap(arr, i1, i2);
} }
tim_sort(arr); // 3sort tim_sort(arr); // 3sort
isSorted(arr); isSorted(arr);

View file

@ -13,6 +13,7 @@
use core::cmp::{Eq, Ord}; use core::cmp::{Eq, Ord};
use core::vec::len; use core::vec::len;
use core::vec; use core::vec;
use core::util;
type Le<'self, T> = &'self fn(v1: &T, v2: &T) -> bool; type Le<'self, T> = &'self fn(v1: &T, v2: &T) -> bool;
@ -63,36 +64,36 @@ pub fn merge_sort<T:Copy>(v: &const [T], le: Le<T>) -> ~[T] {
#[cfg(stage0)] #[cfg(stage0)]
fn part<T>(arr: &mut [T], left: uint, fn part<T>(arr: &mut [T], left: uint,
right: uint, pivot: uint, compare_func: Le<T>) -> uint { right: uint, pivot: uint, compare_func: Le<T>) -> uint {
arr[pivot] <-> arr[right]; vec::swap(arr, pivot, right);
let mut storage_index: uint = left; let mut storage_index: uint = left;
let mut i: uint = left; let mut i: uint = left;
while i < right { while i < right {
let a: &mut T = &mut arr[i]; let a: &mut T = &mut arr[i];
let b: &mut T = &mut arr[right]; let b: &mut T = &mut arr[right];
if compare_func(a, b) { if compare_func(a, b) {
arr[i] <-> arr[storage_index]; vec::swap(arr, i, storage_index);
storage_index += 1; storage_index += 1;
} }
i += 1; i += 1;
} }
arr[storage_index] <-> arr[right]; vec::swap(arr, storage_index, right);
return storage_index; return storage_index;
} }
#[cfg(not(stage0))] #[cfg(not(stage0))]
fn part<T>(arr: &mut [T], left: uint, fn part<T>(arr: &mut [T], left: uint,
right: uint, pivot: uint, compare_func: Le<T>) -> uint { right: uint, pivot: uint, compare_func: Le<T>) -> uint {
arr[pivot] <-> arr[right]; vec::swap(arr, pivot, right);
let mut storage_index: uint = left; let mut storage_index: uint = left;
let mut i: uint = left; let mut i: uint = left;
while i < right { while i < right {
if compare_func(&arr[i], &arr[right]) { if compare_func(&arr[i], &arr[right]) {
arr[i] <-> arr[storage_index]; vec::swap(arr, i, storage_index);
storage_index += 1; storage_index += 1;
} }
i += 1; i += 1;
} }
arr[storage_index] <-> arr[right]; vec::swap(arr, storage_index, right);
return storage_index; return storage_index;
} }
@ -136,29 +137,29 @@ fn qsort3<T:Copy + Ord + Eq>(arr: &mut [T], left: int, right: int) {
j -= 1; j -= 1;
} }
if i >= j { break; } if i >= j { break; }
arr[i] <-> arr[j]; vec::swap(arr, i as uint, j as uint);
if arr[i] == v { if arr[i] == v {
p += 1; p += 1;
arr[p] <-> arr[i]; vec::swap(arr, p as uint, i as uint);
} }
if v == arr[j] { if v == arr[j] {
q -= 1; q -= 1;
arr[j] <-> arr[q]; vec::swap(arr, j as uint, q as uint);
} }
} }
arr[i] <-> arr[right]; vec::swap(arr, i as uint, right as uint);
j = i - 1; j = i - 1;
i += 1; i += 1;
let mut k: int = left; let mut k: int = left;
while k < p { while k < p {
arr[k] <-> arr[j]; vec::swap(arr, k as uint, j as uint);
k += 1; k += 1;
j -= 1; j -= 1;
if k == len::<T>(arr) as int { break; } if k == len::<T>(arr) as int { break; }
} }
k = right - 1; k = right - 1;
while k > q { while k > q {
arr[i] <-> arr[k]; vec::swap(arr, i as uint, k as uint);
k -= 1; k -= 1;
i += 1; i += 1;
if k == 0 { break; } if k == 0 { break; }
@ -273,7 +274,7 @@ fn binarysort<T:Copy + Ord>(array: &mut [T], start: uint) {
fn reverse_slice<T>(v: &mut [T], start: uint, end:uint) { fn reverse_slice<T>(v: &mut [T], start: uint, end:uint) {
let mut i = start; let mut i = start;
while i < end / 2 { while i < end / 2 {
v[i] <-> v[end - i - 1]; vec::swap(v, i, end - i - 1);
i += 1; i += 1;
} }
} }
@ -493,7 +494,7 @@ impl<T:Copy + Ord> MergeState<T> {
let mut len1 = len1; let mut len1 = len1;
let mut len2 = len2; let mut len2 = len2;
array[dest] <-> array[c2]; vec::swap(array, dest, c2);
dest += 1; c2 += 1; len2 -= 1; dest += 1; c2 += 1; len2 -= 1;
if len2 == 0 { if len2 == 0 {
@ -502,7 +503,7 @@ impl<T:Copy + Ord> MergeState<T> {
} }
if len1 == 1 { if len1 == 1 {
copy_vec(array, dest, array, c2, len2); copy_vec(array, dest, array, c2, len2);
array[dest+len2] <-> tmp[c1]; util::swap(&mut array[dest+len2], &mut tmp[c1]);
return; return;
} }
@ -515,14 +516,14 @@ impl<T:Copy + Ord> MergeState<T> {
loop { loop {
assert!(len1 > 1 && len2 != 0); assert!(len1 > 1 && len2 != 0);
if array[c2] < tmp[c1] { if array[c2] < tmp[c1] {
array[dest] <-> array[c2]; vec::swap(array, dest, c2);
dest += 1; c2 += 1; len2 -= 1; dest += 1; c2 += 1; len2 -= 1;
count2 += 1; count1 = 0; count2 += 1; count1 = 0;
if len2 == 0 { if len2 == 0 {
break_outer = true; break_outer = true;
} }
} else { } else {
array[dest] <-> tmp[c1]; util::swap(&mut array[dest], &mut tmp[c1]);
dest += 1; c1 += 1; len1 -= 1; dest += 1; c1 += 1; len1 -= 1;
count1 += 1; count2 = 0; count1 += 1; count2 = 0;
if len1 == 1 { if len1 == 1 {
@ -546,7 +547,7 @@ impl<T:Copy + Ord> MergeState<T> {
dest += count1; c1 += count1; len1 -= count1; dest += count1; c1 += count1; len1 -= count1;
if len1 <= 1 { break_outer = true; break; } if len1 <= 1 { break_outer = true; break; }
} }
array[dest] <-> array[c2]; vec::swap(array, dest, c2);
dest += 1; c2 += 1; len2 -= 1; dest += 1; c2 += 1; len2 -= 1;
if len2 == 0 { break_outer = true; break; } if len2 == 0 { break_outer = true; break; }
@ -557,7 +558,7 @@ impl<T:Copy + Ord> MergeState<T> {
dest += count2; c2 += count2; len2 -= count2; dest += count2; c2 += count2; len2 -= count2;
if len2 == 0 { break_outer = true; break; } if len2 == 0 { break_outer = true; break; }
} }
array[dest] <-> tmp[c1]; util::swap(&mut array[dest], &mut tmp[c1]);
dest += 1; c1 += 1; len1 -= 1; dest += 1; c1 += 1; len1 -= 1;
if len1 == 1 { break_outer = true; break; } if len1 == 1 { break_outer = true; break; }
min_gallop -= 1; min_gallop -= 1;
@ -574,7 +575,7 @@ impl<T:Copy + Ord> MergeState<T> {
if len1 == 1 { if len1 == 1 {
assert!(len2 > 0); assert!(len2 > 0);
copy_vec(array, dest, array, c2, len2); copy_vec(array, dest, array, c2, len2);
array[dest+len2] <-> tmp[c1]; util::swap(&mut array[dest+len2], &mut tmp[c1]);
} else if len1 == 0 { } else if len1 == 0 {
fail!(~"Comparison violates its contract!"); fail!(~"Comparison violates its contract!");
} else { } else {
@ -599,7 +600,7 @@ impl<T:Copy + Ord> MergeState<T> {
let mut len1 = len1; let mut len1 = len1;
let mut len2 = len2; let mut len2 = len2;
array[dest] <-> array[c1]; vec::swap(array, dest, c1);
dest -= 1; c1 -= 1; len1 -= 1; dest -= 1; c1 -= 1; len1 -= 1;
if len1 == 0 { if len1 == 0 {
@ -610,7 +611,7 @@ impl<T:Copy + Ord> MergeState<T> {
dest -= len1; dest -= len1;
c1 -= len1; c1 -= len1;
copy_vec(array, dest+1, array, c1+1, len1); copy_vec(array, dest+1, array, c1+1, len1);
array[dest] <-> tmp[c2]; util::swap(&mut array[dest], &mut tmp[c2]);
return; return;
} }
@ -623,14 +624,14 @@ impl<T:Copy + Ord> MergeState<T> {
loop { loop {
assert!(len1 != 0 && len2 > 1); assert!(len1 != 0 && len2 > 1);
if tmp[c2] < array[c1] { if tmp[c2] < array[c1] {
array[dest] <-> array[c1]; vec::swap(array, dest, c1);
dest -= 1; c1 -= 1; len1 -= 1; dest -= 1; c1 -= 1; len1 -= 1;
count1 += 1; count2 = 0; count1 += 1; count2 = 0;
if len1 == 0 { if len1 == 0 {
break_outer = true; break_outer = true;
} }
} else { } else {
array[dest] <-> tmp[c2]; util::swap(&mut array[dest], &mut tmp[c2]);
dest -= 1; c2 -= 1; len2 -= 1; dest -= 1; c2 -= 1; len2 -= 1;
count2 += 1; count1 = 0; count2 += 1; count1 = 0;
if len2 == 1 { if len2 == 1 {
@ -659,7 +660,7 @@ impl<T:Copy + Ord> MergeState<T> {
if len1 == 0 { break_outer = true; break; } if len1 == 0 { break_outer = true; break; }
} }
array[dest] <-> tmp[c2]; util::swap(&mut array[dest], &mut tmp[c2]);
dest -= 1; c2 -= 1; len2 -= 1; dest -= 1; c2 -= 1; len2 -= 1;
if len2 == 1 { break_outer = true; break; } if len2 == 1 { break_outer = true; break; }
@ -676,7 +677,7 @@ impl<T:Copy + Ord> MergeState<T> {
copy_vec(array, dest+1, tmp, c2+1, count2); copy_vec(array, dest+1, tmp, c2+1, count2);
if len2 <= 1 { break_outer = true; break; } if len2 <= 1 { break_outer = true; break; }
} }
array[dest] <-> array[c1]; vec::swap(array, dest, c1);
dest -= 1; c1 -= 1; len1 -= 1; dest -= 1; c1 -= 1; len1 -= 1;
if len1 == 0 { break_outer = true; break; } if len1 == 0 { break_outer = true; break; }
min_gallop -= 1; min_gallop -= 1;
@ -696,7 +697,7 @@ impl<T:Copy + Ord> MergeState<T> {
dest -= len1; dest -= len1;
c1 -= len1; c1 -= len1;
copy_vec(array, dest+1, array, c1+1, len1); copy_vec(array, dest+1, array, c1+1, len1);
array[dest] <-> tmp[c2]; util::swap(&mut array[dest], &mut tmp[c2]);
} else if len2 == 0 { } else if len2 == 0 {
fail!(~"Comparison violates its contract!"); fail!(~"Comparison violates its contract!");
} else { } else {
@ -1081,7 +1082,7 @@ mod big_tests {
for 3.times { for 3.times {
let i1 = rng.gen_uint_range(0, n); let i1 = rng.gen_uint_range(0, n);
let i2 = rng.gen_uint_range(0, n); let i2 = rng.gen_uint_range(0, n);
arr[i1] <-> arr[i2]; vec::swap(arr, i1, i2);
} }
tim_sort(arr); // 3sort tim_sort(arr); // 3sort
isSorted(arr); isSorted(arr);
@ -1153,7 +1154,7 @@ mod big_tests {
for 3.times { for 3.times {
let i1 = rng.gen_uint_range(0, n); let i1 = rng.gen_uint_range(0, n);
let i2 = rng.gen_uint_range(0, n); let i2 = rng.gen_uint_range(0, n);
arr[i1] <-> arr[i2]; vec::swap(arr, i1, i2);
} }
tim_sort(arr); // 3sort tim_sort(arr); // 3sort
isSorted(arr); isSorted(arr);

View file

@ -13,7 +13,7 @@
//! `TotalOrd`. //! `TotalOrd`.
use core::iterator::*; use core::iterator::*;
use core::util::replace; use core::util::{swap, replace};
// This is implemented as an AA tree, which is a simplified variation of // This is implemented as an AA tree, which is a simplified variation of
// a red-black tree where where red (horizontal) nodes can only be added // a red-black tree where where red (horizontal) nodes can only be added
@ -756,8 +756,8 @@ fn mutate_values<'r, K: TotalOrd, V>(node: &'r mut Option<~TreeNode<K, V>>,
fn skew<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>) { fn skew<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>) {
if node.left.map_default(false, |x| x.level == node.level) { if node.left.map_default(false, |x| x.level == node.level) {
let mut save = node.left.swap_unwrap(); let mut save = node.left.swap_unwrap();
node.left <-> save.right; // save.right now None swap(&mut node.left, &mut save.right); // save.right now None
*node <-> save; swap(node, &mut save);
node.right = Some(save); node.right = Some(save);
} }
} }
@ -768,9 +768,9 @@ fn split<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>) {
if node.right.map_default(false, if node.right.map_default(false,
|x| x.right.map_default(false, |y| y.level == node.level)) { |x| x.right.map_default(false, |y| y.level == node.level)) {
let mut save = node.right.swap_unwrap(); let mut save = node.right.swap_unwrap();
node.right <-> save.left; // save.left now None swap(&mut node.right, &mut save.left); // save.left now None
save.level += 1; save.level += 1;
*node <-> save; swap(node, &mut save);
node.left = Some(save); node.left = Some(save);
} }
} }
@ -823,14 +823,14 @@ fn insert<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>, fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
key: &K) -> Option<V> { key: &K) -> Option<V> {
fn heir_swap<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>, fn heir_swap<K: TotalOrd, V>(node: &mut ~TreeNode<K, V>,
child: &mut Option<~TreeNode<K, V>>) { child: &mut Option<~TreeNode<K, V>>) {
// *could* be done without recursion, but it won't borrow check // *could* be done without recursion, but it won't borrow check
for child.each_mut |x| { for child.each_mut |x| {
if x.right.is_some() { if x.right.is_some() {
heir_swap(node, &mut x.right); heir_swap(node, &mut x.right);
} else { } else {
node.key <-> x.key; swap(&mut node.key, &mut x.key);
node.value <-> x.value; swap(&mut node.value, &mut x.value);
} }
} }
} }
@ -850,8 +850,8 @@ fn remove<K: TotalOrd, V>(node: &mut Option<~TreeNode<K, V>>,
if left.right.is_some() { if left.right.is_some() {
heir_swap(save, &mut left.right); heir_swap(save, &mut left.right);
} else { } else {
save.key <-> left.key; swap(&mut save.key, &mut left.key);
save.value <-> left.value; swap(&mut save.value, &mut left.value);
} }
save.left = Some(left); save.left = Some(left);
(remove(&mut save.left, key), true) (remove(&mut save.left, key), true)

View file

@ -22,6 +22,7 @@ use core::io;
use core::pipes::recv; use core::pipes::recv;
use core::run; use core::run;
use core::to_bytes; use core::to_bytes;
use core::util::replace;
/** /**
* *
@ -352,9 +353,7 @@ impl TPrep for Prep {
_ => { _ => {
let (port, chan) = oneshot(); let (port, chan) = oneshot();
let mut blk = None; let blk = replace(&mut bo, None).unwrap();
blk <-> bo;
let blk = blk.unwrap();
let chan = Cell(chan); let chan = Cell(chan);
do task::spawn { do task::spawn {
@ -386,9 +385,7 @@ fn unwrap<T:Owned +
Decodable<json::Decoder>>( // FIXME(#5121) Decodable<json::Decoder>>( // FIXME(#5121)
w: Work<T>) -> T { w: Work<T>) -> T {
let mut ww = w; let mut ww = w;
let mut s = None; let s = replace(&mut ww.res, None);
ww.res <-> s;
match s { match s {
None => fail!(), None => fail!(),

View file

@ -14,6 +14,7 @@ extern mod std;
use std::time::precise_time_s; use std::time::precise_time_s;
use core::rand::RngUtil; use core::rand::RngUtil;
use core::util;
macro_rules! bench ( macro_rules! bench (
($id:ident) => (maybe_run_test(argv, stringify!($id).to_owned(), $id)) ($id:ident) => (maybe_run_test(argv, stringify!($id).to_owned(), $id))
@ -115,7 +116,7 @@ fn vec_push_all() {
v.push_all(rv); v.push_all(rv);
} }
else { else {
v <-> rv; util::swap(&mut v, &mut rv);
v.push_all(rv); v.push_all(rv);
} }
} }

View file

@ -20,6 +20,7 @@ extern mod std;
use core::cell::Cell; use core::cell::Cell;
use core::pipes::recv; use core::pipes::recv;
use core::util;
use std::time; use std::time;
use std::future; use std::future;
@ -42,10 +43,8 @@ fn thread_ring(i: uint,
// Send/Receive lots of messages. // Send/Receive lots of messages.
for uint::range(0, count) |j| { for uint::range(0, count) |j| {
//error!("task %?, iter %?", i, j); //error!("task %?, iter %?", i, j);
let mut num_chan2 = None; let num_chan2 = replace(&mut num_chan, None);
let mut num_port2 = None; let num_port2 = replace(&mut num_port, None);
num_chan2 <-> num_chan;
num_port2 <-> num_port;
num_chan = Some(ring::client::num(num_chan2.unwrap(), i * j)); num_chan = Some(ring::client::num(num_chan2.unwrap(), i * j));
let port = num_port2.unwrap(); let port = num_port2.unwrap();
match recv(port) { match recv(port) {

View file

@ -17,6 +17,7 @@ use core::hashmap::HashMap;
use core::io::ReaderUtil; use core::io::ReaderUtil;
use core::comm::{stream, Port, Chan}; use core::comm::{stream, Port, Chan};
use core::cmp::Ord; use core::cmp::Ord;
use core::util;
// given a map, print a sorted version of it // given a map, print a sorted version of it
fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str { fn sort_and_fmt(mm: &HashMap<~[u8], uint>, total: uint) -> ~str {
@ -159,8 +160,7 @@ fn main() {
let mut from_child = ~[]; let mut from_child = ~[];
let to_child = vec::mapi(sizes, |ii, sz| { let to_child = vec::mapi(sizes, |ii, sz| {
let sz = *sz; let sz = *sz;
let mut stream = None; let stream = util::replace(&mut streams[ii], None);
stream <-> streams[ii];
let (from_child_, to_parent_) = stream.unwrap(); let (from_child_, to_parent_) = stream.unwrap();
from_child.push(from_child_); from_child.push(from_child_);

View file

@ -1,28 +0,0 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn test1() {
let v: int;
let mut w: int;
v = 1; //~ NOTE prior assignment occurs here
w = 2;
v <-> w; //~ ERROR re-assignment of immutable variable
}
fn test2() {
let v: int;
let mut w: int;
v = 1; //~ NOTE prior assignment occurs here
w = 2;
w <-> v; //~ ERROR re-assignment of immutable variable
}
fn main() {
}

View file

@ -1,16 +0,0 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn main() {
let mut x = 3;
let y;
x <-> y; //~ ERROR use of possibly uninitialized variable: `y`
copy x;
}

View file

@ -87,7 +87,7 @@ fn f110() {
fn f120() { fn f120() {
let x = ~[~"hi", ~"ho"]; let x = ~[~"hi", ~"ho"];
x[0] <-> x[1]; vec::swap(x, 0, 1);
touch(&x[0]); touch(&x[0]);
touch(&x[1]); touch(&x[1]);
} }

View file

@ -1,15 +0,0 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn main() {
5 <-> 3;
//~^ ERROR cannot assign
//~^^ ERROR cannot assign
}

View file

@ -8,14 +8,16 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use core::util;
struct Ints {sum: ~int, values: ~[int]} struct Ints {sum: ~int, values: ~[int]}
fn add_int(x: &mut Ints, v: int) { fn add_int(x: &mut Ints, v: int) {
*x.sum += v; *x.sum += v;
let mut values = ~[]; let mut values = ~[];
x.values <-> values; util::swap(&mut values, &mut x.values);
values.push(v); values.push(v);
x.values <-> values; util::swap(&mut values, &mut x.values);
} }
fn iter_ints(x: &Ints, f: &fn(x: &int) -> bool) -> bool { fn iter_ints(x: &Ints, f: &fn(x: &int) -> bool) -> bool {

View file

@ -10,8 +10,11 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use core::util;
// tjc: I don't know why // tjc: I don't know why
pub mod pipes { pub mod pipes {
use core::util;
use core::cast::{forget, transmute}; use core::cast::{forget, transmute};
pub struct Stuff<T> { pub struct Stuff<T> {
@ -104,8 +107,7 @@ pub mod pipes {
match old_state { match old_state {
empty | blocked => { task::yield(); } empty | blocked => { task::yield(); }
full => { full => {
let mut payload = None; let payload = util::replace(&mut p.payload, None);
payload <-> (*p).payload;
return Some(payload.unwrap()) return Some(payload.unwrap())
} }
terminated => { terminated => {
@ -159,10 +161,9 @@ pub mod pipes {
fn finalize(&self) { fn finalize(&self) {
unsafe { unsafe {
if self.p != None { if self.p != None {
let mut p = None;
let self_p: &mut Option<*packet<T>> = let self_p: &mut Option<*packet<T>> =
cast::transmute(&self.p); cast::transmute(&self.p);
p <-> *self_p; let p = util::replace(self_p, None);
sender_terminate(p.unwrap()) sender_terminate(p.unwrap())
} }
} }
@ -171,9 +172,7 @@ pub mod pipes {
pub impl<T:Owned> send_packet<T> { pub impl<T:Owned> send_packet<T> {
fn unwrap(&mut self) -> *packet<T> { fn unwrap(&mut self) -> *packet<T> {
let mut p = None; util::replace(&mut self.p, None).unwrap()
p <-> self.p;
p.unwrap()
} }
} }
@ -192,10 +191,9 @@ pub mod pipes {
fn finalize(&self) { fn finalize(&self) {
unsafe { unsafe {
if self.p != None { if self.p != None {
let mut p = None;
let self_p: &mut Option<*packet<T>> = let self_p: &mut Option<*packet<T>> =
cast::transmute(&self.p); cast::transmute(&self.p);
p <-> *self_p; let p = util::replace(self_p, None);
receiver_terminate(p.unwrap()) receiver_terminate(p.unwrap())
} }
} }
@ -204,9 +202,7 @@ pub mod pipes {
pub impl<T:Owned> recv_packet<T> { pub impl<T:Owned> recv_packet<T> {
fn unwrap(&mut self) -> *packet<T> { fn unwrap(&mut self) -> *packet<T> {
let mut p = None; util::replace(&mut self.p, None).unwrap()
p <-> self.p;
p.unwrap()
} }
} }
@ -225,6 +221,7 @@ pub mod pipes {
pub mod pingpong { pub mod pingpong {
use core::cast; use core::cast;
use core::ptr; use core::ptr;
use core::util;
pub struct ping(::pipes::send_packet<pong>); pub struct ping(::pipes::send_packet<pong>);
pub struct pong(::pipes::send_packet<ping>); pub struct pong(::pipes::send_packet<ping>);

View file

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use core::util;
pub fn main() { pub fn main() {
let mut x = 4; let mut x = 4;
@ -24,6 +26,6 @@ pub fn main() {
} }
} }
let mut y = 4; let mut y = 4;
y <-> x; util::swap(&mut y, &mut x);
} }
} }

View file

@ -8,7 +8,10 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use core::util;
pub fn main() { pub fn main() {
let mut x = 3; let mut y = 7; let mut x = 3; let mut y = 7;
x <-> y; assert!((x == 7)); assert!((y == 3)); util::swap(&mut x, &mut y);
assert!((x == 7)); assert!((y == 3));
} }

View file

@ -8,15 +8,15 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
fn swap<T>(v: &mut [T], i: int, j: int) { v[i] <-> v[j]; } use core::util;
pub fn main() { pub fn main() {
let mut a: ~[int] = ~[0, 1, 2, 3, 4, 5, 6]; let mut a: ~[int] = ~[0, 1, 2, 3, 4, 5, 6];
swap(a, 2, 4); vec::swap(a, 2, 4);
assert!((a[2] == 4)); assert!((a[2] == 4));
assert!((a[4] == 2)); assert!((a[4] == 2));
let mut n = 42; let mut n = 42;
n <-> a[0]; util::swap(&mut n, &mut a[0]);
assert!((a[0] == 42)); assert!((a[0] == 42));
assert!((n == 0)); assert!((n == 0));
} }

View file

@ -10,6 +10,8 @@
// Issue #5041 - avoid overlapping memcpy when src and dest of a swap are the same // Issue #5041 - avoid overlapping memcpy when src and dest of a swap are the same
use core::util;
pub fn main() { pub fn main() {
let mut test = TestDescAndFn { let mut test = TestDescAndFn {
desc: TestDesc { desc: TestDesc {
@ -22,7 +24,10 @@ pub fn main() {
} }
fn do_swap(test: &mut TestDescAndFn) { fn do_swap(test: &mut TestDescAndFn) {
*test <-> *test; unsafe {
util::swap_ptr(ptr::to_mut_unsafe_ptr(test),
ptr::to_mut_unsafe_ptr(test));
}
} }
pub enum TestName { pub enum TestName {

View file

@ -8,10 +8,12 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use core::util;
pub fn main() { pub fn main() {
let mut i = ~100; let mut i = ~100;
let mut j = ~200; let mut j = ~200;
i <-> j; util::swap(&mut i, &mut j);
assert!(i == ~200); assert!(i == ~200);
assert!(j == ~100); assert!(j == ~100);
} }

View file

@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use core::util;
// Just a grab bag of stuff that you wouldn't want to actually write. // Just a grab bag of stuff that you wouldn't want to actually write.
fn strange() -> bool { let _x: bool = return true; } fn strange() -> bool { let _x: bool = return true; }
@ -52,7 +54,7 @@ fn notsure() {
let mut _y = (_x = 0) == (_x = 0); let mut _y = (_x = 0) == (_x = 0);
let mut _z = (_x = 0) < (_x = 0); let mut _z = (_x = 0) < (_x = 0);
let _a = (_x += 0) == (_x = 0); let _a = (_x += 0) == (_x = 0);
let _b = (_y <-> _z) == (_y <-> _z); let _b = util::swap(&mut _y, &mut _z) == util::swap(&mut _y, &mut _z);
} }
fn canttouchthis() -> uint { fn canttouchthis() -> uint {