std: Add move_val_init to mem. Replace direct intrinsic usage

This commit is contained in:
Brian Anderson 2014-02-08 22:16:42 -08:00
parent d433b80e02
commit c7710cdf45
7 changed files with 24 additions and 18 deletions

View File

@ -178,7 +178,6 @@ use std::cast;
use std::libc::{c_void, size_t, malloc, free};
use std::mem;
use std::ptr;
use std::unstable::intrinsics;
// Define a wrapper around the handle returned by the foreign code.
// Unique<T> has the same semantics as ~T
@ -200,7 +199,7 @@ impl<T: Send> Unique<T> {
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
// move_val_init moves a value into this memory without
// attempting to drop the original value.
intrinsics::move_val_init(&mut *ptr, value);
mem::move_val_init(&mut *ptr, value);
Unique{ptr: ptr}
}
}

View File

@ -32,10 +32,10 @@ use collections::list;
use std::cast::{transmute, transmute_mut, transmute_mut_region};
use std::cast;
use std::cell::{Cell, RefCell};
use std::mem;
use std::num;
use std::ptr;
use std::kinds::marker;
use std::mem;
use std::rc::Rc;
use std::rt::global_heap;
use std::unstable::intrinsics::{TyDesc, get_tydesc};
@ -216,7 +216,7 @@ impl Arena {
unsafe {
let ptr = self.alloc_pod_inner(mem::size_of::<T>(), mem::min_align_of::<T>());
let ptr: *mut T = transmute(ptr);
intrinsics::move_val_init(&mut (*ptr), op());
mem::move_val_init(&mut (*ptr), op());
return transmute(ptr);
}
}
@ -278,7 +278,7 @@ impl Arena {
// has *not* been initialized yet.
*ty_ptr = transmute(tydesc);
// Actually initialize it
intrinsics::move_val_init(&mut(*ptr), op());
mem::move_val_init(&mut(*ptr), op());
// Now that we are done, update the tydesc to indicate that
// the object is there.
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
@ -379,7 +379,7 @@ impl TypedArenaChunk {
let mut chunk = unsafe {
let chunk = global_heap::exchange_malloc(size);
let mut chunk: ~TypedArenaChunk = cast::transmute(chunk);
intrinsics::move_val_init(&mut chunk.next, next);
mem::move_val_init(&mut chunk.next, next);
chunk
};
@ -466,7 +466,7 @@ impl<T> TypedArena<T> {
}
let ptr: &'a mut T = cast::transmute(this.ptr);
intrinsics::move_val_init(ptr, object);
mem::move_val_init(ptr, object);
this.ptr = this.ptr.offset(1);
let ptr: &'a T = ptr;
ptr

View File

@ -13,7 +13,7 @@
#[allow(missing_doc)];
use std::clone::Clone;
use std::unstable::intrinsics::{move_val_init, init};
use std::mem::{move_val_init, init};
use std::util::{replace, swap};
use std::vec;

View File

@ -83,6 +83,15 @@ pub unsafe fn uninit<T>() -> T {
intrinsics::uninit()
}
/// Move a value to an uninitialized memory location.
///
/// Drop glue is not run on the destination.
#[inline]
pub unsafe fn move_val_init<T>(dst: &mut T, src: T) {
intrinsics::move_val_init(dst, src)
}
#[cfg(test)]
mod tests {
use mem::*;

View File

@ -120,7 +120,6 @@ use mem::size_of;
use kinds::marker;
use uint;
use unstable::finally::Finally;
use unstable::intrinsics;
use unstable::raw::{Repr, Slice, Vec};
use util;
@ -137,7 +136,7 @@ pub fn from_fn<T>(n_elts: uint, op: |uint| -> T) -> ~[T] {
let mut i: uint = 0u;
(|| {
while i < n_elts {
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i as int)), op(i));
mem::move_val_init(&mut(*ptr::mut_offset(p, i as int)), op(i));
i += 1u;
}
}).finally(|| {
@ -164,7 +163,7 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> ~[T] {
let mut i = 0u;
(|| {
while i < n_elts {
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i as int)), t.clone());
mem::move_val_init(&mut(*ptr::mut_offset(p, i as int)), t.clone());
i += 1u;
}
}).finally(|| {
@ -1495,7 +1494,7 @@ impl<T> OwnedVector<T> for ~[T] {
(**repr).fill += mem::nonzero_size_of::<T>();
let p = to_unsafe_ptr(&((**repr).data));
let p = ptr::offset(p, fill as int) as *mut T;
intrinsics::move_val_init(&mut(*p), t);
mem::move_val_init(&mut(*p), t);
}
}
@ -1552,7 +1551,7 @@ impl<T> OwnedVector<T> for ~[T] {
ptr::copy_memory(p.offset(1), p, len - i);
// Write it in, overwriting the first copy of the `i`th
// element.
intrinsics::move_val_init(&mut *p, x);
mem::move_val_init(&mut *p, x);
self.set_len(len + 1);
}
}
@ -2397,7 +2396,7 @@ impl<'a,T> MutableVector<'a, T> for &'a mut [T] {
#[inline]
unsafe fn init_elem(self, i: uint, val: T) {
intrinsics::move_val_init(&mut (*self.as_mut_ptr().offset(i as int)), val);
mem::move_val_init(&mut (*self.as_mut_ptr().offset(i as int)), val);
}
#[inline]

View File

@ -17,14 +17,13 @@ use clone::Clone;
use iter::{DoubleEndedIterator, Iterator};
use num::CheckedMul;
use container::Container;
use mem::size_of;
use mem::{size_of, move_val_init};
use cast::{forget, transmute};
use rt::global_heap::{malloc_raw, realloc_raw};
use vec::{ImmutableVector, Items, MutableVector};
use unstable::raw::Slice;
use ptr::{offset, read_ptr};
use libc::{free, c_void};
use unstable::intrinsics::move_val_init;
pub struct Vec<T> {
priv len: uint,

View File

@ -8,12 +8,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use std::unstable;
use std::mem;
pub fn main() {
unsafe {
let mut x: bool = false;
// this line breaks it
unstable::intrinsics::move_val_init(&mut x, false);
mem::move_val_init(&mut x, false);
}
}