Skip to content

Commit f9fe251

Browse files
committed
auto merge of #15569 : pcwalton/rust/reexport-intrinsics, r=cmr
code bloat. This didn't make a difference in any compile times that I saw, but it fits what we're doing with `transmute` and seems prudent. r? @alexcrichton
2 parents a1bd5d3 + 6f96abf commit f9fe251

File tree

2 files changed

+67
-89
lines changed

2 files changed

+67
-89
lines changed

src/libcore/intrinsics.rs

+63-9
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,73 @@ extern "rust-intrinsic" {
326326
/// integer, since the conversion would throw away aliasing information.
327327
pub fn offset<T>(dst: *const T, offset: int) -> *const T;
328328

329-
/// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with
330-
/// a size of `count` * `size_of::<T>()` and an alignment of
331-
/// `min_align_of::<T>()`
329+
/// Copies data from one location to another.
330+
///
331+
/// Copies `count` elements (not bytes) from `src` to `dst`. The source
332+
/// and destination may *not* overlap.
333+
///
334+
/// `copy_nonoverlapping_memory` is semantically equivalent to C's `memcpy`.
335+
///
336+
/// # Example
337+
///
338+
/// A safe swap function:
339+
///
340+
/// ```
341+
/// use std::mem;
342+
/// use std::ptr;
343+
///
344+
/// fn swap<T>(x: &mut T, y: &mut T) {
345+
/// unsafe {
346+
/// // Give ourselves some scratch space to work with
347+
/// let mut t: T = mem::uninitialized();
348+
///
349+
/// // Perform the swap, `&mut` pointers never alias
350+
/// ptr::copy_nonoverlapping_memory(&mut t, &*x, 1);
351+
/// ptr::copy_nonoverlapping_memory(x, &*y, 1);
352+
/// ptr::copy_nonoverlapping_memory(y, &t, 1);
353+
///
354+
/// // y and t now point to the same thing, but we need to completely forget `tmp`
355+
/// // because it's no longer relevant.
356+
/// mem::forget(t);
357+
/// }
358+
/// }
359+
/// ```
360+
///
361+
/// # Safety Note
362+
///
363+
/// If the source and destination overlap then the behavior of this
364+
/// function is undefined.
365+
#[unstable]
332366
pub fn copy_nonoverlapping_memory<T>(dst: *mut T, src: *const T, count: uint);
333367

334-
/// Equivalent to the appropriate `llvm.memmove.p0i8.0i8.*` intrinsic, with
335-
/// a size of `count` * `size_of::<T>()` and an alignment of
336-
/// `min_align_of::<T>()`
368+
/// Copies data from one location to another.
369+
///
370+
/// Copies `count` elements (not bytes) from `src` to `dst`. The source
371+
/// and destination may overlap.
372+
///
373+
/// `copy_memory` is semantically equivalent to C's `memmove`.
374+
///
375+
/// # Example
376+
///
377+
/// Efficiently create a Rust vector from an unsafe buffer:
378+
///
379+
/// ```
380+
/// use std::ptr;
381+
///
382+
/// unsafe fn from_buf_raw<T>(ptr: *const T, elts: uint) -> Vec<T> {
383+
/// let mut dst = Vec::with_capacity(elts);
384+
/// dst.set_len(elts);
385+
/// ptr::copy_memory(dst.as_mut_ptr(), ptr, elts);
386+
/// dst
387+
/// }
388+
/// ```
389+
///
390+
#[unstable]
337391
pub fn copy_memory<T>(dst: *mut T, src: *const T, count: uint);
338392

339-
/// Equivalent to the appropriate `llvm.memset.p0i8.*` intrinsic, with a
340-
/// size of `count` * `size_of::<T>()` and an alignment of
341-
/// `min_align_of::<T>()`
393+
/// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
394+
/// bytes of memory starting at `dst` to `c`.
395+
#[experimental = "uncertain about naming and semantics"]
342396
pub fn set_memory<T>(dst: *mut T, val: u8, count: uint);
343397

344398
/// Equivalent to the appropriate `llvm.memcpy.p0i8.0i8.*` intrinsic, with

src/libcore/ptr.rs

+4-80
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ use option::{Some, None, Option};
9595

9696
use cmp::{PartialEq, Eq, PartialOrd, Equiv, Ordering, Less, Equal, Greater};
9797

98+
pub use intrinsics::copy_memory;
99+
pub use intrinsics::copy_nonoverlapping_memory;
100+
pub use intrinsics::set_memory;
101+
98102
/// Create a null pointer.
99103
///
100104
/// # Example
@@ -123,86 +127,6 @@ pub fn null<T>() -> *const T { 0 as *const T }
123127
#[unstable = "may need a different name after pending changes to pointer types"]
124128
pub fn mut_null<T>() -> *mut T { 0 as *mut T }
125129

126-
/// Copies data from one location to another.
127-
///
128-
/// Copies `count` elements (not bytes) from `src` to `dst`. The source
129-
/// and destination may overlap.
130-
///
131-
/// `copy_memory` is semantically equivalent to C's `memmove`.
132-
///
133-
/// # Example
134-
///
135-
/// Efficiently create a Rust vector from an unsafe buffer:
136-
///
137-
/// ```
138-
/// use std::ptr;
139-
///
140-
/// unsafe fn from_buf_raw<T>(ptr: *const T, elts: uint) -> Vec<T> {
141-
/// let mut dst = Vec::with_capacity(elts);
142-
/// dst.set_len(elts);
143-
/// ptr::copy_memory(dst.as_mut_ptr(), ptr, elts);
144-
/// dst
145-
/// }
146-
/// ```
147-
///
148-
#[inline]
149-
#[unstable]
150-
pub unsafe fn copy_memory<T>(dst: *mut T, src: *const T, count: uint) {
151-
intrinsics::copy_memory(dst, src, count)
152-
}
153-
154-
/// Copies data from one location to another.
155-
///
156-
/// Copies `count` elements (not bytes) from `src` to `dst`. The source
157-
/// and destination may *not* overlap.
158-
///
159-
/// `copy_nonoverlapping_memory` is semantically equivalent to C's `memcpy`.
160-
///
161-
/// # Example
162-
///
163-
/// A safe swap function:
164-
///
165-
/// ```
166-
/// use std::mem;
167-
/// use std::ptr;
168-
///
169-
/// fn swap<T>(x: &mut T, y: &mut T) {
170-
/// unsafe {
171-
/// // Give ourselves some scratch space to work with
172-
/// let mut t: T = mem::uninitialized();
173-
///
174-
/// // Perform the swap, `&mut` pointers never alias
175-
/// ptr::copy_nonoverlapping_memory(&mut t, &*x, 1);
176-
/// ptr::copy_nonoverlapping_memory(x, &*y, 1);
177-
/// ptr::copy_nonoverlapping_memory(y, &t, 1);
178-
///
179-
/// // y and t now point to the same thing, but we need to completely forget `tmp`
180-
/// // because it's no longer relevant.
181-
/// mem::forget(t);
182-
/// }
183-
/// }
184-
/// ```
185-
///
186-
/// # Safety Note
187-
///
188-
/// If the source and destination overlap then the behavior of this
189-
/// function is undefined.
190-
#[inline]
191-
#[unstable]
192-
pub unsafe fn copy_nonoverlapping_memory<T>(dst: *mut T,
193-
src: *const T,
194-
count: uint) {
195-
intrinsics::copy_nonoverlapping_memory(dst, src, count)
196-
}
197-
198-
/// Invokes memset on the specified pointer, setting `count * size_of::<T>()`
199-
/// bytes of memory starting at `dst` to `c`.
200-
#[inline]
201-
#[experimental = "uncertain about naming and semantics"]
202-
pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) {
203-
intrinsics::set_memory(dst, c, count)
204-
}
205-
206130
/// Zeroes out `count * size_of::<T>` bytes of memory at `dst`
207131
#[inline]
208132
#[experimental = "uncertain about naming and semantics"]

0 commit comments

Comments
 (0)