diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-03-18 09:36:18 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-03-18 16:32:32 -0700 |
| commit | fccf5a00056b1d72065951a4428070326df1cfb5 (patch) | |
| tree | 481e99dcf4197b0b25cd765877c1b132f768d772 /src/libcore/ptr.rs | |
| parent | 94a95067e017252d4928a4292a6aeef66902e694 (diff) | |
| download | rust-fccf5a00056b1d72065951a4428070326df1cfb5.tar.gz rust-fccf5a00056b1d72065951a4428070326df1cfb5.zip | |
Register new snapshots
Diffstat (limited to 'src/libcore/ptr.rs')
| -rw-r--r-- | src/libcore/ptr.rs | 143 |
1 files changed, 5 insertions, 138 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index f28c26d1798..1cbea057e88 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -262,145 +262,13 @@ pub unsafe fn write<T>(dst: *mut T, src: T) { intrinsics::move_val_init(&mut *dst, src) } -#[cfg(stage0)] -/// Methods on raw pointers -#[stable(feature = "rust1", since = "1.0.0")] -pub trait PtrExt { - /// The type which is being pointed at - type Target: ?Sized; - - /// Returns true if the pointer is null. - #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool; - - /// Returns `None` if the pointer is null, or else returns a reference to - /// the value wrapped in `Some`. - /// - /// # Safety - /// - /// While this method and its mutable counterpart are useful for - /// null-safety, it is important to note that this is still an unsafe - /// operation because the returned value could be pointing to invalid - /// memory. - #[unstable(feature = "core", - reason = "Option is not clearly the right return type, and we may want \ - to tie the return lifetime to a borrow of the raw pointer")] - unsafe fn as_ref<'a>(&self) -> Option<&'a Self::Target>; - - /// Calculates the offset from a pointer. `count` is in units of T; e.g. a - /// `count` of 3 represents a pointer offset of `3 * sizeof::<T>()` bytes. - /// - /// # Safety - /// - /// The offset must be in-bounds of the object, or one-byte-past-the-end. - /// Otherwise `offset` invokes Undefined Behaviour, regardless of whether - /// the pointer is used. - #[stable(feature = "rust1", since = "1.0.0")] - unsafe fn offset(self, count: isize) -> Self where Self::Target: Sized; -} - -#[cfg(stage0)] -/// Methods on mutable raw pointers -#[stable(feature = "rust1", since = "1.0.0")] -pub trait MutPtrExt { - /// The type which is being pointed at - type Target: ?Sized; - - /// Returns `None` if the pointer is null, or else returns a mutable - /// reference to the value wrapped in `Some`. - /// - /// # Safety - /// - /// As with `as_ref`, this is unsafe because it cannot verify the validity - /// of the returned pointer. - #[unstable(feature = "core", - reason = "Option is not clearly the right return type, and we may want \ - to tie the return lifetime to a borrow of the raw pointer")] - unsafe fn as_mut<'a>(&self) -> Option<&'a mut Self::Target>; -} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized> PtrExt for *const T { - type Target = T; - - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool { self == 0 as *const T } - - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - unsafe fn offset(self, count: isize) -> *const T where T: Sized { - intrinsics::offset(self, count) - } - - #[inline] - #[unstable(feature = "core", - reason = "return value does not necessarily convey all possible \ - information")] - unsafe fn as_ref<'a>(&self) -> Option<&'a T> { - if self.is_null() { - None - } else { - Some(&**self) - } - } -} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized> PtrExt for *mut T { - type Target = T; - - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - fn is_null(self) -> bool { self == 0 as *mut T } - - #[inline] - #[stable(feature = "rust1", since = "1.0.0")] - unsafe fn offset(self, count: isize) -> *mut T where T: Sized { - intrinsics::offset(self, count) as *mut T - } - - #[inline] - #[unstable(feature = "core", - reason = "return value does not necessarily convey all possible \ - information")] - unsafe fn as_ref<'a>(&self) -> Option<&'a T> { - if self.is_null() { - None - } else { - Some(&**self) - } - } -} - -#[cfg(stage0)] -#[stable(feature = "rust1", since = "1.0.0")] -impl<T: ?Sized> MutPtrExt for *mut T { - type Target = T; - - #[inline] - #[unstable(feature = "core", - reason = "return value does not necessarily convey all possible \ - information")] - unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> { - if self.is_null() { - None - } else { - Some(&mut **self) - } - } -} - -#[cfg(not(stage0))] #[stable(feature = "rust1", since = "1.0.0")] #[lang = "const_ptr"] impl<T: ?Sized> *const T { /// Returns true if the pointer is null. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn is_null(self) -> bool { + pub fn is_null(self) -> bool where T: Sized { self == 0 as *const T } @@ -417,7 +285,7 @@ impl<T: ?Sized> *const T { reason = "Option is not clearly the right return type, and we may want \ to tie the return lifetime to a borrow of the raw pointer")] #[inline] - pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> { + pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> where T: Sized { if self.is_null() { None } else { @@ -440,14 +308,13 @@ impl<T: ?Sized> *const T { } } -#[cfg(not(stage0))] #[stable(feature = "rust1", since = "1.0.0")] #[lang = "mut_ptr"] impl<T: ?Sized> *mut T { /// Returns true if the pointer is null. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn is_null(self) -> bool { + pub fn is_null(self) -> bool where T: Sized { self == 0 as *mut T } @@ -464,7 +331,7 @@ impl<T: ?Sized> *mut T { reason = "Option is not clearly the right return type, and we may want \ to tie the return lifetime to a borrow of the raw pointer")] #[inline] - pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> { + pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> where T: Sized { if self.is_null() { None } else { @@ -497,7 +364,7 @@ impl<T: ?Sized> *mut T { reason = "return value does not necessarily convey all possible \ information")] #[inline] - pub unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> { + pub unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> where T: Sized { if self.is_null() { None } else { |
