Skip to content

Commit 27f9c79

Browse files
committed
auto merge of #12124 : brson/rust/intrinsics, r=thestinger
As mentioned #11956 (comment) I've taken some of the most commonly-used intrinsics and put them in a more logical place, reduced the amount of code looking in `unstable::intrinsics`. r? @thestinger
2 parents 7985fbc + 1c4a2fd commit 27f9c79

28 files changed

+131
-103
lines changed

src/doc/guide-ffi.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ and `free`:
176176
~~~~
177177
use std::cast;
178178
use std::libc::{c_void, size_t, malloc, free};
179+
use std::mem;
179180
use std::ptr;
180-
use std::unstable::intrinsics;
181181
182182
// Define a wrapper around the handle returned by the foreign code.
183183
// Unique<T> has the same semantics as ~T
@@ -199,7 +199,7 @@ impl<T: Send> Unique<T> {
199199
// `*ptr` is uninitialized, and `*ptr = value` would attempt to destroy it
200200
// move_val_init moves a value into this memory without
201201
// attempting to drop the original value.
202-
intrinsics::move_val_init(&mut *ptr, value);
202+
mem::move_val_init(&mut *ptr, value);
203203
Unique{ptr: ptr}
204204
}
205205
}
@@ -226,7 +226,7 @@ impl<T: Send> Unique<T> {
226226
impl<T: Send> Drop for Unique<T> {
227227
fn drop(&mut self) {
228228
unsafe {
229-
let x = intrinsics::uninit(); // dummy value to swap in
229+
let x = mem::uninit(); // dummy value to swap in
230230
// We need to move the object out of the box, so that
231231
// the destructor is called (at the end of this scope.)
232232
ptr::replace_ptr(self.ptr, x);

src/libarena/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ use collections::list;
3232
use std::cast::{transmute, transmute_mut, transmute_mut_region};
3333
use std::cast;
3434
use std::cell::{Cell, RefCell};
35+
use std::mem;
3536
use std::num;
3637
use std::ptr;
3738
use std::kinds::marker;
38-
use std::mem;
3939
use std::rc::Rc;
4040
use std::rt::global_heap;
4141
use std::unstable::intrinsics::{TyDesc, get_tydesc};
@@ -216,7 +216,7 @@ impl Arena {
216216
unsafe {
217217
let ptr = self.alloc_pod_inner(mem::size_of::<T>(), mem::min_align_of::<T>());
218218
let ptr: *mut T = transmute(ptr);
219-
intrinsics::move_val_init(&mut (*ptr), op());
219+
mem::move_val_init(&mut (*ptr), op());
220220
return transmute(ptr);
221221
}
222222
}
@@ -278,7 +278,7 @@ impl Arena {
278278
// has *not* been initialized yet.
279279
*ty_ptr = transmute(tydesc);
280280
// Actually initialize it
281-
intrinsics::move_val_init(&mut(*ptr), op());
281+
mem::move_val_init(&mut(*ptr), op());
282282
// Now that we are done, update the tydesc to indicate that
283283
// the object is there.
284284
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
@@ -379,7 +379,7 @@ impl TypedArenaChunk {
379379
let mut chunk = unsafe {
380380
let chunk = global_heap::exchange_malloc(size);
381381
let mut chunk: ~TypedArenaChunk = cast::transmute(chunk);
382-
intrinsics::move_val_init(&mut chunk.next, next);
382+
mem::move_val_init(&mut chunk.next, next);
383383
chunk
384384
};
385385

@@ -466,7 +466,7 @@ impl<T> TypedArena<T> {
466466
}
467467

468468
let ptr: &'a mut T = cast::transmute(this.ptr);
469-
intrinsics::move_val_init(ptr, object);
469+
mem::move_val_init(ptr, object);
470470
this.ptr = this.ptr.offset(1);
471471
let ptr: &'a T = ptr;
472472
ptr

src/libcollections/priority_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#[allow(missing_doc)];
1414

1515
use std::clone::Clone;
16-
use std::unstable::intrinsics::{move_val_init, init};
16+
use std::mem::{move_val_init, init};
1717
use std::util::{replace, swap};
1818
use std::vec;
1919

src/libnative/io/file.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ use std::io::IoError;
1616
use std::io;
1717
use std::libc::{c_int, c_void};
1818
use std::libc;
19+
use std::mem;
1920
use std::os;
2021
use std::rt::rtio;
21-
use std::unstable::intrinsics;
2222
use std::vec;
2323

2424
use io::{IoResult, retry};
@@ -147,7 +147,7 @@ impl rtio::RtioFileStream for FileDesc {
147147
#[cfg(windows)]
148148
fn os_pread(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<int> {
149149
unsafe {
150-
let mut overlap: libc::OVERLAPPED = intrinsics::init();
150+
let mut overlap: libc::OVERLAPPED = mem::init();
151151
let handle = libc::get_osfhandle(fd) as libc::HANDLE;
152152
let mut bytes_read = 0;
153153
overlap.Offset = offset as libc::DWORD;
@@ -179,7 +179,7 @@ impl rtio::RtioFileStream for FileDesc {
179179
#[cfg(windows)]
180180
fn os_pwrite(fd: c_int, buf: *u8, amt: uint, offset: u64) -> IoResult<()> {
181181
unsafe {
182-
let mut overlap: libc::OVERLAPPED = intrinsics::init();
182+
let mut overlap: libc::OVERLAPPED = mem::init();
183183
let handle = libc::get_osfhandle(fd) as libc::HANDLE;
184184
overlap.Offset = offset as libc::DWORD;
185185
overlap.OffsetHigh = (offset >> 32) as libc::DWORD;
@@ -867,7 +867,7 @@ pub fn stat(p: &CString) -> IoResult<io::FileStat> {
867867

868868
#[cfg(windows)]
869869
fn os_stat(p: &CString) -> IoResult<io::FileStat> {
870-
let mut stat: libc::stat = unsafe { intrinsics::uninit() };
870+
let mut stat: libc::stat = unsafe { mem::uninit() };
871871
as_utf16_p(p.as_str().unwrap(), |up| {
872872
match retry(|| unsafe { libc::wstat(up, &mut stat) }) {
873873
0 => Ok(mkstat(&stat, p)),
@@ -878,7 +878,7 @@ pub fn stat(p: &CString) -> IoResult<io::FileStat> {
878878

879879
#[cfg(unix)]
880880
fn os_stat(p: &CString) -> IoResult<io::FileStat> {
881-
let mut stat: libc::stat = unsafe { intrinsics::uninit() };
881+
let mut stat: libc::stat = unsafe { mem::uninit() };
882882
match retry(|| unsafe { libc::stat(p.with_ref(|p| p), &mut stat) }) {
883883
0 => Ok(mkstat(&stat, p)),
884884
_ => Err(super::last_error()),
@@ -897,7 +897,7 @@ pub fn lstat(p: &CString) -> IoResult<io::FileStat> {
897897

898898
#[cfg(unix)]
899899
fn os_lstat(p: &CString) -> IoResult<io::FileStat> {
900-
let mut stat: libc::stat = unsafe { intrinsics::uninit() };
900+
let mut stat: libc::stat = unsafe { mem::uninit() };
901901
match retry(|| unsafe { libc::lstat(p.with_ref(|p| p), &mut stat) }) {
902902
0 => Ok(mkstat(&stat, p)),
903903
_ => Err(super::last_error()),

src/libnative/io/net.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::libc;
1515
use std::mem;
1616
use std::rt::rtio;
1717
use std::sync::arc::UnsafeArc;
18-
use std::unstable::intrinsics;
1918

2019
use super::{IoResult, retry};
2120
use super::file::keep_going;
@@ -28,10 +27,10 @@ use super::file::keep_going;
2827
#[cfg(unix)] pub type sock_t = super::file::fd_t;
2928

3029
pub fn htons(u: u16) -> u16 {
31-
intrinsics::to_be16(u as i16) as u16
30+
mem::to_be16(u as i16) as u16
3231
}
3332
pub fn ntohs(u: u16) -> u16 {
34-
intrinsics::from_be16(u as i16) as u16
33+
mem::from_be16(u as i16) as u16
3534
}
3635

3736
enum InAddr {
@@ -68,7 +67,7 @@ fn ip_to_inaddr(ip: ip::IpAddr) -> InAddr {
6867

6968
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
7069
unsafe {
71-
let storage: libc::sockaddr_storage = intrinsics::init();
70+
let storage: libc::sockaddr_storage = mem::init();
7271
let len = match ip_to_inaddr(addr.ip) {
7372
InAddr(inaddr) => {
7473
let storage: *mut libc::sockaddr_in = cast::transmute(&storage);
@@ -138,7 +137,7 @@ fn sockname(fd: sock_t,
138137
*mut libc::socklen_t) -> libc::c_int)
139138
-> IoResult<ip::SocketAddr>
140139
{
141-
let mut storage: libc::sockaddr_storage = unsafe { intrinsics::init() };
140+
let mut storage: libc::sockaddr_storage = unsafe { mem::init() };
142141
let mut len = mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;
143142
unsafe {
144143
let storage = &mut storage as *mut libc::sockaddr_storage;
@@ -225,7 +224,7 @@ pub fn init() {
225224

226225
LOCK.lock();
227226
if !INITIALIZED {
228-
let mut data: WSADATA = intrinsics::init();
227+
let mut data: WSADATA = mem::init();
229228
let ret = WSAStartup(0x202, // version 2.2
230229
&mut data);
231230
assert_eq!(ret, 0);
@@ -438,7 +437,7 @@ impl TcpAcceptor {
438437

439438
pub fn native_accept(&mut self) -> IoResult<TcpStream> {
440439
unsafe {
441-
let mut storage: libc::sockaddr_storage = intrinsics::init();
440+
let mut storage: libc::sockaddr_storage = mem::init();
442441
let storagep = &mut storage as *mut libc::sockaddr_storage;
443442
let size = mem::size_of::<libc::sockaddr_storage>();
444443
let mut size = size as libc::socklen_t;
@@ -543,7 +542,7 @@ impl rtio::RtioSocket for UdpSocket {
543542
impl rtio::RtioUdpSocket for UdpSocket {
544543
fn recvfrom(&mut self, buf: &mut [u8]) -> IoResult<(uint, ip::SocketAddr)> {
545544
unsafe {
546-
let mut storage: libc::sockaddr_storage = intrinsics::init();
545+
let mut storage: libc::sockaddr_storage = mem::init();
547546
let storagep = &mut storage as *mut libc::sockaddr_storage;
548547
let mut addrlen: libc::socklen_t =
549548
mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;

src/libnative/io/timer_other.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@
4949
use std::comm::Data;
5050
use std::hashmap::HashMap;
5151
use std::libc;
52+
use std::mem;
5253
use std::os;
5354
use std::ptr;
5455
use std::rt::rtio;
5556
use std::sync::atomics;
56-
use std::unstable::intrinsics;
5757

5858
use io::file::FileDesc;
5959
use io::IoResult;
@@ -87,17 +87,17 @@ pub enum Req {
8787
// returns the current time (in milliseconds)
8888
fn now() -> u64 {
8989
unsafe {
90-
let mut now: libc::timeval = intrinsics::init();
90+
let mut now: libc::timeval = mem::init();
9191
assert_eq!(imp::gettimeofday(&mut now, ptr::null()), 0);
9292
return (now.tv_sec as u64) * 1000 + (now.tv_usec as u64) / 1000;
9393
}
9494
}
9595

9696
fn helper(input: libc::c_int, messages: Port<Req>) {
97-
let mut set: imp::fd_set = unsafe { intrinsics::init() };
97+
let mut set: imp::fd_set = unsafe { mem::init() };
9898

9999
let mut fd = FileDesc::new(input, true);
100-
let mut timeout: libc::timeval = unsafe { intrinsics::init() };
100+
let mut timeout: libc::timeval = unsafe { mem::init() };
101101

102102
// active timers are those which are able to be selected upon (and it's a
103103
// sorted list, and dead timers are those which have expired, but ownership

src/libnative/io/timer_timerfd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::ptr;
3434
use std::os;
3535
use std::rt::rtio;
3636
use std::hashmap::HashMap;
37-
use std::unstable::intrinsics;
37+
use std::mem;
3838

3939
use io::file::FileDesc;
4040
use io::IoResult;
@@ -75,7 +75,7 @@ fn helper(input: libc::c_int, messages: Port<Req>) {
7575
}
7676

7777
add(efd, input);
78-
let events: [imp::epoll_event, ..16] = unsafe { intrinsics::init() };
78+
let events: [imp::epoll_event, ..16] = unsafe { mem::init() };
7979
let mut map: HashMap<libc::c_int, (Chan<()>, bool)> = HashMap::new();
8080
'outer: loop {
8181
let n = match unsafe {

src/librustc/util/sha2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use extra::hex::ToHex;
2222
/// format.
2323
fn write_u32_be(dst: &mut[u8], input: u32) {
2424
use std::cast::transmute;
25-
use std::unstable::intrinsics::to_be32;
25+
use std::mem::to_be32;
2626
assert!(dst.len() == 4);
2727
unsafe {
2828
let x: *mut i32 = transmute(dst.unsafe_mut_ref(0));
@@ -33,7 +33,7 @@ fn write_u32_be(dst: &mut[u8], input: u32) {
3333
/// Read a vector of bytes into a vector of u32s. The values are read in big-endian format.
3434
fn read_u32v_be(dst: &mut[u32], input: &[u8]) {
3535
use std::cast::transmute;
36-
use std::unstable::intrinsics::to_be32;
36+
use std::mem::to_be32;
3737
assert!(dst.len() * 4 == input.len());
3838
unsafe {
3939
let mut x: *mut i32 = transmute(dst.unsafe_mut_ref(0));

src/librustdoc/html/markdown.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use std::cast;
2828
use std::fmt;
2929
use std::io;
3030
use std::libc;
31+
use std::mem;
3132
use std::str;
3233
use std::unstable::intrinsics;
3334
use std::vec;
@@ -144,7 +145,7 @@ pub fn render(w: &mut io::Writer, s: &str) -> fmt::Result {
144145
flags: 0,
145146
link_attributes: None,
146147
};
147-
let mut callbacks: sd_callbacks = intrinsics::init();
148+
let mut callbacks: sd_callbacks = mem::init();
148149

149150
sdhtml_renderer(&callbacks, &options, 0);
150151
let opaque = my_opaque {
@@ -197,7 +198,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
197198
MKDEXT_STRIKETHROUGH;
198199
let callbacks = sd_callbacks {
199200
blockcode: block,
200-
other: intrinsics::init()
201+
other: mem::init()
201202
};
202203

203204
let tests = tests as *mut ::test::Collector as *libc::c_void;

src/librustuv/net.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use std::mem;
1717
use std::ptr;
1818
use std::rt::rtio;
1919
use std::rt::task::BlockedTask;
20-
use std::unstable::intrinsics;
2120

2221
use access::Access;
2322
use homing::{HomingIO, HomeHandle};
@@ -33,8 +32,8 @@ use uvll;
3332
/// Generic functions related to dealing with sockaddr things
3433
////////////////////////////////////////////////////////////////////////////////
3534

36-
pub fn htons(u: u16) -> u16 { intrinsics::to_be16(u as i16) as u16 }
37-
pub fn ntohs(u: u16) -> u16 { intrinsics::from_be16(u as i16) as u16 }
35+
pub fn htons(u: u16) -> u16 { mem::to_be16(u as i16) as u16 }
36+
pub fn ntohs(u: u16) -> u16 { mem::from_be16(u as i16) as u16 }
3837

3938
pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
4039
len: uint) -> ip::SocketAddr {
@@ -80,7 +79,7 @@ pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
8079

8180
fn addr_to_sockaddr(addr: ip::SocketAddr) -> (libc::sockaddr_storage, uint) {
8281
unsafe {
83-
let mut storage: libc::sockaddr_storage = intrinsics::init();
82+
let mut storage: libc::sockaddr_storage = mem::init();
8483
let len = match addr.ip {
8584
ip::Ipv4Addr(a, b, c, d) => {
8685
let storage: &mut libc::sockaddr_in =
@@ -134,7 +133,7 @@ fn socket_name(sk: SocketNameKind,
134133
};
135134

136135
// Allocate a sockaddr_storage since we don't know if it's ipv4 or ipv6
137-
let mut sockaddr: libc::sockaddr_storage = unsafe { intrinsics::init() };
136+
let mut sockaddr: libc::sockaddr_storage = unsafe { mem::init() };
138137
let mut namelen = mem::size_of::<libc::sockaddr_storage>() as c_int;
139138

140139
let sockaddr_p = &mut sockaddr as *mut libc::sockaddr_storage;

src/libserialize/ebml.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub mod reader {
132132

133133
pub fn vuint_at(data: &[u8], start: uint) -> Res {
134134
use std::ptr::offset;
135-
use std::unstable::intrinsics::from_be32;
135+
use std::mem::from_be32;
136136

137137
if data.len() - start < 4 {
138138
return vuint_at_slow(data, start);

src/libstd/c_str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ use kinds::marker;
7070
use ops::Drop;
7171
use cmp::Eq;
7272
use clone::Clone;
73+
use mem;
7374
use option::{Option, Some, None};
7475
use ptr::RawPtr;
7576
use ptr;
7677
use str::StrSlice;
7778
use str;
7879
use vec::{ImmutableVector, MutableVector};
7980
use vec;
80-
use unstable::intrinsics;
8181
use rt::global_heap::malloc_raw;
8282

8383
/// The representation of a C String.
@@ -327,7 +327,7 @@ impl<'a> ToCStr for &'a [u8] {
327327
// Unsafe function that handles possibly copying the &[u8] into a stack array.
328328
unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: |*libc::c_char| -> T) -> T {
329329
if v.len() < BUF_LEN {
330-
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
330+
let mut buf: [u8, .. BUF_LEN] = mem::uninit();
331331
vec::bytes::copy_memory(buf, v);
332332
buf[v.len()] = 0;
333333

src/libstd/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ptr::copy_nonoverlapping_memory;
1818
/// Casts the value at `src` to U. The two types must have the same length.
1919
#[inline]
2020
pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
21-
let mut dest: U = intrinsics::uninit();
21+
let mut dest: U = mem::uninit();
2222
let dest_ptr: *mut u8 = transmute(&mut dest);
2323
let src_ptr: *u8 = transmute(src);
2424
copy_nonoverlapping_memory(dest_ptr, src_ptr, mem::size_of::<U>());

0 commit comments

Comments
 (0)