~~~~
use std::cast;
use std::libc::{c_void, size_t, malloc, free};
+use std::mem;
use std::ptr;
use std::unstable::intrinsics;
impl<T: Send> Drop for Unique<T> {
fn drop(&mut self) {
unsafe {
- let x = intrinsics::uninit(); // dummy value to swap in
+ let x = mem::uninit(); // dummy value to swap in
// We need to move the object out of the box, so that
// the destructor is called (at the end of this scope.)
ptr::replace_ptr(self.ptr, x);
use std::io;
use std::libc::{c_int, c_void};
use std::libc;
+use std::mem;
use std::os;
use std::rt::rtio;
-use std::unstable::intrinsics;
use std::vec;
use io::{IoResult, retry};
#[cfg(windows)]
fn os_pread(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<int> {
unsafe {
- let mut overlap: libc::OVERLAPPED = intrinsics::init();
+ let mut overlap: libc::OVERLAPPED = mem::init();
let handle = libc::get_osfhandle(fd) as libc::HANDLE;
let mut bytes_read = 0;
overlap.Offset = offset as libc::DWORD;
#[cfg(windows)]
fn os_pwrite(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<()> {
unsafe {
- let mut overlap: libc::OVERLAPPED = intrinsics::init();
+ let mut overlap: libc::OVERLAPPED = mem::init();
let handle = libc::get_osfhandle(fd) as libc::HANDLE;
overlap.Offset = offset as libc::DWORD;
overlap.OffsetHigh = (offset >> 32) as libc::DWORD;
#[cfg(windows)]
fn os_stat(p: &CString) -> IoResult<io::FileStat> {
- let mut stat: libc::stat = unsafe { intrinsics::uninit() };
+ let mut stat: libc::stat = unsafe { mem::uninit() };
as_utf16_p(p.as_str().unwrap(), |up| {
match retry(|| unsafe { libc::wstat(up, &mut stat) }) {
0 => Ok(mkstat(&stat, p)),
#[cfg(unix)]
fn os_stat(p: &CString) -> IoResult<io::FileStat> {
- let mut stat: libc::stat = unsafe { intrinsics::uninit() };
+ let mut stat: libc::stat = unsafe { mem::uninit() };
match retry(|| unsafe { libc::stat(p.with_ref(|p| p), &mut stat) }) {
0 => Ok(mkstat(&stat, p)),
_ => Err(super::last_error()),
#[cfg(unix)]
fn os_lstat(p: &CString) -> IoResult<io::FileStat> {
- let mut stat: libc::stat = unsafe { intrinsics::uninit() };
+ let mut stat: libc::stat = unsafe { mem::uninit() };
match retry(|| unsafe { libc::lstat(p.with_ref(|p| p), &mut stat) }) {
0 => Ok(mkstat(&stat, p)),
_ => Err(super::last_error()),
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
unsafe {
- let storage: libc::sockaddr_storage = intrinsics::init();
+ let storage: libc::sockaddr_storage = mem::init();
let len = match ip_to_inaddr(addr.ip) {
InAddr(inaddr) => {
let storage: *mut libc::sockaddr_in = cast::transmute(&storage);
*mut libc::socklen_t) -> libc::c_int)
-> IoResult<ip::SocketAddr>
{
- let mut storage: libc::sockaddr_storage = unsafe { intrinsics::init() };
+ let mut storage: libc::sockaddr_storage = unsafe { mem::init() };
let mut len = mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
unsafe {
let storage = &mut storage as *mut libc::sockaddr_storage;
LOCK.lock();
if !INITIALIZED {
- let mut data: WSADATA = intrinsics::init();
+ let mut data: WSADATA = mem::init();
let ret = WSAStartup(0x202, // version 2.2
&mut data);
assert_eq!(ret, 0);
pub fn native_accept(&mut self) -> IoResult<TcpStream> {
unsafe {
- let mut storage: libc::sockaddr_storage = intrinsics::init();
+ let mut storage: libc::sockaddr_storage = mem::init();
let storagep = &mut storage as *mut libc::sockaddr_storage;
let size = mem::size_of::<libc::sockaddr_storage>();
let mut size = size as libc::socklen_t;
impl rtio::RtioUdpSocket for UdpSocket {
fn recvfrom(&mut self, buf: &mut [u8]) -> IoResult<(uint, ip::SocketAddr)> {
unsafe {
- let mut storage: libc::sockaddr_storage = intrinsics::init();
+ let mut storage: libc::sockaddr_storage = mem::init();
let storagep = &mut storage as *mut libc::sockaddr_storage;
let mut addrlen: libc::socklen_t =
mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
use std::comm::Data;
use std::hashmap::HashMap;
use std::libc;
+use std::mem;
use std::os;
use std::ptr;
use std::rt::rtio;
use std::sync::atomics;
-use std::unstable::intrinsics;
use io::file::FileDesc;
use io::IoResult;
// returns the current time (in milliseconds)
fn now() -> u64 {
unsafe {
- let mut now: libc::timeval = intrinsics::init();
+ let mut now: libc::timeval = mem::init();
assert_eq!(imp::gettimeofday(&mut now, ptr::null()), 0);
return (now.tv_sec as u64) * 1000 + (now.tv_usec as u64) / 1000;
}
}
fn helper(input: libc::c_int, messages: Port<Req>) {
- let mut set: imp::fd_set = unsafe { intrinsics::init() };
+ let mut set: imp::fd_set = unsafe { mem::init() };
let mut fd = FileDesc::new(input, true);
- let mut timeout: libc::timeval = unsafe { intrinsics::init() };
+ let mut timeout: libc::timeval = unsafe { mem::init() };
// active timers are those which are able to be selected upon (and it's a
// sorted list, and dead timers are those which have expired, but ownership
use std::os;
use std::rt::rtio;
use std::hashmap::HashMap;
-use std::unstable::intrinsics;
+use std::mem;
use io::file::FileDesc;
use io::IoResult;
}
add(efd, input);
- let events: [imp::epoll_event, ..16] = unsafe { intrinsics::init() };
+ let events: [imp::epoll_event, ..16] = unsafe { mem::init() };
let mut map: HashMap<libc::c_int, (Chan<()>, bool)> = HashMap::new();
'outer: loop {
let n = match unsafe {
use std::fmt;
use std::io;
use std::libc;
+use std::mem;
use std::str;
use std::unstable::intrinsics;
use std::vec;
flags: 0,
link_attributes: None,
};
- let mut callbacks: sd_callbacks = intrinsics::init();
+ let mut callbacks: sd_callbacks = mem::init();
sdhtml_renderer(&callbacks, &options, 0);
let opaque = my_opaque {
MKDEXT_STRIKETHROUGH;
let callbacks = sd_callbacks {
blockcode: block,
- other: intrinsics::init()
+ other: mem::init()
};
let tests = tests as *mut ::test::Collector as *libc::c_void;
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
unsafe {
- let mut storage: libc::sockaddr_storage = intrinsics::init();
+ let mut storage: libc::sockaddr_storage = mem::init();
let len = match addr.ip {
ip::Ipv4Addr(a, b, c, d) => {
let storage: &mut libc::sockaddr_in =
};
// Allocate a sockaddr_storage since we don't know if it's ipv4 or ipv6
- let mut sockaddr: libc::sockaddr_storage = unsafe { intrinsics::init() };
+ let mut sockaddr: libc::sockaddr_storage = unsafe { mem::init() };
let mut namelen = mem::size_of::<libc::sockaddr_storage>() as c_int;
let sockaddr_p = &mut sockaddr as *mut libc::sockaddr_storage;
use ops::Drop;
use cmp::Eq;
use clone::Clone;
+use mem;
use option::{Option, Some, None};
use ptr::RawPtr;
use ptr;
use str;
use vec::{ImmutableVector, MutableVector};
use vec;
-use unstable::intrinsics;
use rt::global_heap::malloc_raw;
/// The representation of a C String.
// Unsafe function that handles possibly copying the &[u8] into a stack array.
unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T {
if v.len() < BUF_LEN {
- let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
+ let mut buf: [u8, .. BUF_LEN] = mem::uninit();
vec::bytes::copy_memory(buf, v);
buf[v.len()] = 0;
/// Casts the value at `src` to U. The two types must have the same length.
#[inline]
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
- let mut dest: U = intrinsics::uninit();
+ let mut dest: U = mem::uninit();
let dest_ptr: *mut u8 = transmute(&mut dest);
let src_ptr: *u8 = transmute(src);
copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>());
pref_align_of::<T>()
}
+/// Create a value initialized to zero.
+///
+/// `init` is unsafe because it returns a zeroed-out datum,
+/// which is unsafe unless T is Pod.
+#[inline]
+pub unsafe fn init<T>() -> T {
+ intrinsics::init()
+}
+
+/// Create an uninitialized value.
+#[inline]
+pub unsafe fn uninit<T>() -> T {
+ intrinsics::uninit()
+}
+
#[cfg(test)]
mod tests {
use mem::*;
#[cfg(not(test))]
use cmp::Equiv;
use iter::{range, Iterator};
+use mem;
use option::{Option, Some, None};
use unstable::intrinsics;
use util::swap;
#[inline]
pub unsafe fn swap_ptr<T>(x: *mut T, y: *mut T) {
// Give ourselves some scratch space to work with
- let mut tmp: T = intrinsics::uninit();
+ let mut tmp: T = mem::uninit();
let t: *mut T = &mut tmp;
// Perform the swap
*/
#[inline(always)]
pub unsafe fn read_ptr<T>(src: *T) -> T {
- let mut tmp: T = intrinsics::uninit();
+ let mut tmp: T = mem::uninit();
copy_nonoverlapping_memory(&mut tmp, src, 1);
tmp
}
use cmp;
use libc::consts::os::posix01::{PTHREAD_CREATE_JOINABLE, PTHREAD_STACK_MIN};
use libc;
+ use mem;
use os;
use ptr;
- use unstable::intrinsics;
use unstable::stack::RED_ZONE;
pub type rust_thread = libc::pthread_t;
pub type rust_thread_return = *u8;
pub unsafe fn create(stack: uint, p: ~proc()) -> rust_thread {
- let mut native: libc::pthread_t = intrinsics::uninit();
- let mut attr: libc::pthread_attr_t = intrinsics::uninit();
+ let mut native: libc::pthread_t = mem::uninit();
+ let mut attr: libc::pthread_attr_t = mem::uninit();
assert_eq!(pthread_attr_init(&mut attr), 0);
assert_eq!(pthread_attr_setdetachstate(&mut attr,
PTHREAD_CREATE_JOINABLE), 0);
use mem;
use uint;
use util::replace;
-use unstable::intrinsics::init;
+use mem::init;
use vec;
use ptr::RawPtr;
use vec::{ImmutableVector, Items, MutableVector, MutItems, OwnedVector};
/// Create a value initialized to zero.
///
/// `init` is unsafe because it returns a zeroed-out datum,
- /// which is unsafe unless T is POD. We don't have a POD
- /// kind yet. (See #4074).
+ /// which is unsafe unless T is Pod.
pub fn init<T>() -> T;
/// Create an uninitialized value.
use libc;
use self::os::{PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER,
pthread_mutex_t, pthread_cond_t};
- use unstable::intrinsics;
+ use mem;
type pthread_mutexattr_t = libc::c_void;
type pthread_condattr_t = libc::c_void;
impl Mutex {
pub unsafe fn new() -> Mutex {
let mut m = Mutex {
- lock: intrinsics::init(),
- cond: intrinsics::init(),
+ lock: mem::init(),
+ cond: mem::init(),
};
pthread_mutex_init(&mut m.lock, 0 as *libc::c_void);
//! Miscellaneous helpers for common patterns
use cast;
+use mem;
use ptr;
-use unstable::intrinsics;
/// The identity function.
#[inline]
pub fn swap<T>(x: &mut T, y: &mut T) {
unsafe {
// Give ourselves some scratch space to work with
- let mut tmp: T = intrinsics::uninit();
+ let mut tmp: T = mem::uninit();
let t: *mut T = &mut tmp;
// Perform the swap, `&mut` pointers never alias
use std::i32::range;
use std::libc::{STDIN_FILENO, c_int, fdopen, fgets, fileno, fopen, fstat};
use std::libc::{stat, strlen};
+use std::mem::init;
use std::ptr::null;
-use std::unstable::intrinsics::init;
use std::vec::{reverse};
static LINE_LEN: uint = 80;
enum v {}
pub fn main() {
- let y: v = unsafe { ::std::unstable::intrinsics::uninit() };
+ let y: v = unsafe { ::std::mem::uninit() };
}
// Test the uninit() construct returning various empty types.
use std::vec;
-use std::unstable::intrinsics;
+use std::mem;
#[deriving(Clone)]
struct Foo;
pub fn main() {
unsafe {
- let _x: Foo = intrinsics::uninit();
- let _x: [Foo, ..2] = intrinsics::uninit();
+ let _x: Foo = mem::uninit();
+ let _x: [Foo, ..2] = mem::uninit();
}
}