diff options
| author | bors <bors@rust-lang.org> | 2013-07-08 18:49:46 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-07-08 18:49:46 -0700 |
| commit | a48ca3290df992fde2f74ccf5b9f4e36563af8da (patch) | |
| tree | 816142177cf53d6185626aee592ef837b5e1d93a /src/libstd/ptr.rs | |
| parent | 30c8aac677a754e0d4ebc16f261618f15d15a6e2 (diff) | |
| parent | 0c6d02f391aa668b2ead91e8a4ed545475ac2c90 (diff) | |
| download | rust-a48ca3290df992fde2f74ccf5b9f4e36563af8da.tar.gz rust-a48ca3290df992fde2f74ccf5b9f4e36563af8da.zip | |
auto merge of #7262 : nikomatsakis/rust/ref-bindings-in-irrefut-patterns, r=catamorphism
Correct treatment of irrefutable patterns. The old code was wrong in many, many ways. `ref` bindings didn't work, it sometimes copied when it should have moved, the borrow checker didn't even look at such patterns at all, we weren't consistent about preventing values with destructors from being pulled apart, etc. Fixes #3224. Fixes #3225. Fixes #3255. Fixes #6225. Fixes #6386. r? @catamorphism
Diffstat (limited to 'src/libstd/ptr.rs')
| -rw-r--r-- | src/libstd/ptr.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index aee6f1bd204..2b42c085009 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -143,6 +143,30 @@ pub unsafe fn set_memory<T>(dst: *mut T, c: u8, count: uint) { } /** + * Zeroes out `count * size_of::<T>` bytes of memory at `dst` + */ +#[inline] +#[cfg(not(stage0))] +pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) { + set_memory(dst, 0, count); +} + +/** + * Zeroes out `count * size_of::<T>` bytes of memory at `dst` + */ +#[inline] +#[cfg(stage0)] +pub unsafe fn zero_memory<T>(dst: *mut T, count: uint) { + let mut count = count * sys::size_of::<T>(); + let mut dst = dst as *mut u8; + while count > 0 { + *dst = 0; + dst = mut_offset(dst, 1); + count -= 1; + } +} + +/** * Swap the values at two mutable locations of the same type, without * deinitialising or copying either one. */ @@ -172,6 +196,32 @@ pub unsafe fn replace_ptr<T>(dest: *mut T, mut src: T) -> T { src } +/** + * Reads the value from `*src` and returns it. Does not copy `*src`. + */ +#[inline(always)] +pub unsafe fn read_ptr<T>(src: *mut T) -> T { + let mut tmp: T = intrinsics::uninit(); + let t: *mut T = &mut tmp; + copy_memory(t, src, 1); + tmp +} + +/** + * Reads the value from `*src` and nulls it out. + * This currently prevents destructors from executing. + */ +#[inline(always)] +pub unsafe fn read_and_zero_ptr<T>(dest: *mut T) -> T { + // Copy the data out from `dest`: + let tmp = read_ptr(dest); + + // Now zero out `dest`: + zero_memory(dest, 1); + + tmp +} + /// Transform a region pointer - &T - to an unsafe pointer - *T. #[inline] pub fn to_unsafe_ptr<T>(thing: &T) -> *T { |
