diff options
| author | Jorge Aparicio <japaricious@gmail.com> | 2015-03-10 23:13:36 -0500 |
|---|---|---|
| committer | Jorge Aparicio <japaricious@gmail.com> | 2015-03-16 21:57:42 -0500 |
| commit | 8afcaabee32fb41eaf065041d7510c6762f12822 (patch) | |
| tree | 3e276fa5f1927f2e69c40689ebd638bce37f08ff | |
| parent | 633c593bc3f9787decfaf943cdc5659f132ade50 (diff) | |
| download | rust-8afcaabee32fb41eaf065041d7510c6762f12822.tar.gz rust-8afcaabee32fb41eaf065041d7510c6762f12822.zip | |
impl<T> *const T, impl<T> *mut T
| -rw-r--r-- | src/liballoc/heap.rs | 1 | ||||
| -rw-r--r-- | src/liballoc/rc.rs | 3 | ||||
| -rw-r--r-- | src/libcollections/slice.rs | 1 | ||||
| -rw-r--r-- | src/libcore/prelude.rs | 1 | ||||
| -rw-r--r-- | src/libcore/ptr.rs | 118 | ||||
| -rw-r--r-- | src/libcore/slice.rs | 1 | ||||
| -rw-r--r-- | src/libcore/str/mod.rs | 1 | ||||
| -rw-r--r-- | src/libstd/collections/hash/table.rs | 3 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 1 | ||||
| -rw-r--r-- | src/libstd/old_io/extensions.rs | 1 | ||||
| -rw-r--r-- | src/libstd/old_io/mod.rs | 1 | ||||
| -rw-r--r-- | src/libstd/os.rs | 1 | ||||
| -rw-r--r-- | src/libstd/prelude/v1.rs | 1 | ||||
| -rw-r--r-- | src/libstd/rt/at_exit_imp.rs | 1 |
14 files changed, 135 insertions, 0 deletions
diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs index f9936b7a16a..d1c8ab348d4 100644 --- a/src/liballoc/heap.rs +++ b/src/liballoc/heap.rs @@ -8,6 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(stage0)] #[cfg(not(test))] use core::ptr::PtrExt; diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 115acd4a0ef..3692149db44 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -159,7 +159,10 @@ use core::nonzero::NonZero; use core::ops::{Deref, Drop}; use core::option::Option; use core::option::Option::{Some, None}; +#[cfg(stage0)] use core::ptr::{self, PtrExt}; +#[cfg(not(stage0))] +use core::ptr; use core::result::Result; use core::result::Result::{Ok, Err}; use core::intrinsics::assume; diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index c98d0cb35e1..0fe513d3118 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -99,6 +99,7 @@ use core::mem; use core::num::wrapping::WrappingOps; use core::ops::FnMut; use core::option::Option::{self, Some, None}; +#[cfg(stage0)] use core::ptr::PtrExt; use core::ptr; use core::result::Result; diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs index 6c79233da68..fb793a62390 100644 --- a/src/libcore/prelude.rs +++ b/src/libcore/prelude.rs @@ -42,6 +42,7 @@ pub use iter::{Extend, IteratorExt}; pub use iter::{Iterator, DoubleEndedIterator}; pub use iter::{ExactSizeIterator}; pub use option::Option::{self, Some, None}; +#[cfg(stage0)] pub use ptr::{PtrExt, MutPtrExt}; pub use result::Result::{self, Ok, Err}; pub use slice::{AsSlice, SliceExt}; diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 32123a8271c..f28c26d1798 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -262,6 +262,7 @@ 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 { @@ -298,6 +299,7 @@ pub trait PtrExt { 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 { @@ -317,6 +319,7 @@ pub trait MutPtrExt { 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; @@ -344,6 +347,7 @@ impl<T: ?Sized> PtrExt for *const T { } } +#[cfg(stage0)] #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized> PtrExt for *mut T { type Target = T; @@ -371,6 +375,7 @@ impl<T: ?Sized> PtrExt for *mut T { } } +#[cfg(stage0)] #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized> MutPtrExt for *mut T { type Target = T; @@ -388,6 +393,119 @@ impl<T: ?Sized> MutPtrExt for *mut T { } } +#[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 { + self == 0 as *const T + } + + /// 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")] + #[inline] + pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> { + if self.is_null() { + None + } else { + Some(&**self) + } + } + + /// 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")] + #[inline] + pub unsafe fn offset(self, count: isize) -> *const T where T: Sized { + intrinsics::offset(self, count) + } +} + +#[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 { + self == 0 as *mut T + } + + /// 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")] + #[inline] + pub unsafe fn as_ref<'a>(&self) -> Option<&'a T> { + if self.is_null() { + None + } else { + Some(&**self) + } + } + + /// 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")] + #[inline] + pub unsafe fn offset(self, count: isize) -> *mut T where T: Sized { + intrinsics::offset(self, count) as *mut T + } + + /// 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 = "return value does not necessarily convey all possible \ + information")] + #[inline] + pub unsafe fn as_mut<'a>(&self) -> Option<&'a mut T> { + if self.is_null() { + None + } else { + Some(&mut **self) + } + } +} + // Equality for pointers #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized> PartialEq for *const T { diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 62cffa9b86e..c8a8dd88501 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -49,6 +49,7 @@ use option::Option::{None, Some}; use result::Result; use result::Result::{Ok, Err}; use ptr; +#[cfg(stage0)] use ptr::PtrExt; use mem; use mem::size_of; diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 6f72890d96f..3873f305b42 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -30,6 +30,7 @@ use mem; use num::Int; use ops::{Fn, FnMut}; use option::Option::{self, None, Some}; +#[cfg(stage0)] use ptr::PtrExt; use raw::{Repr, Slice}; use result::Result::{self, Ok, Err}; diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index 69fd0a57d5f..7812363d55e 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -24,7 +24,10 @@ use num::wrapping::{OverflowingOps, WrappingOps}; use ops::{Deref, DerefMut, Drop}; use option::Option; use option::Option::{Some, None}; +#[cfg(stage0)] use ptr::{self, PtrExt, Unique}; +#[cfg(not(stage0))] +use ptr::{self, Unique}; use rt::heap::{allocate, deallocate, EMPTY}; use collections::hash_state::HashState; diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index c89edf72842..72d014e77a7 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -20,6 +20,7 @@ use iter::Iterator; use marker::Sized; use ops::{Drop, FnOnce}; use option::Option::{self, Some, None}; +#[cfg(stage0)] use ptr::PtrExt; use result::Result::{Ok, Err}; use result; diff --git a/src/libstd/old_io/extensions.rs b/src/libstd/old_io/extensions.rs index b67ec847c79..a81275952c5 100644 --- a/src/libstd/old_io/extensions.rs +++ b/src/libstd/old_io/extensions.rs @@ -26,6 +26,7 @@ use num::Int; use ops::FnOnce; use option::Option; use option::Option::{Some, None}; +#[cfg(stage0)] use ptr::PtrExt; use result::Result::{Ok, Err}; #[cfg(stage0)] diff --git a/src/libstd/old_io/mod.rs b/src/libstd/old_io/mod.rs index 89e813de5b6..2c978177c50 100644 --- a/src/libstd/old_io/mod.rs +++ b/src/libstd/old_io/mod.rs @@ -935,6 +935,7 @@ impl<'a> Reader for &'a mut (Reader+'a) { // API yet. If so, it should be a method on Vec. unsafe fn slice_vec_capacity<'a, T>(v: &'a mut Vec<T>, start: uint, end: uint) -> &'a mut [T] { use slice; + #[cfg(stage0)] use ptr::PtrExt; assert!(start <= end); diff --git a/src/libstd/os.rs b/src/libstd/os.rs index e04d7f3f240..fc05eb1d627 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -52,6 +52,7 @@ use option::Option::{Some, None}; use option::Option; use old_path::{Path, GenericPath, BytesContainer}; use path::{self, PathBuf}; +#[cfg(stage0)] use ptr::PtrExt; use ptr; use result::Result::{Err, Ok}; diff --git a/src/libstd/prelude/v1.rs b/src/libstd/prelude/v1.rs index e21ae1657ee..4327b26260a 100644 --- a/src/libstd/prelude/v1.rs +++ b/src/libstd/prelude/v1.rs @@ -40,6 +40,7 @@ #[doc(no_inline)] pub use iter::{Iterator, IteratorExt, Extend}; #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use option::Option::{self, Some, None}; +#[cfg(stage0)] #[stable(feature = "rust1", since = "1.0.0")] #[doc(no_inline)] pub use ptr::{PtrExt, MutPtrExt}; #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libstd/rt/at_exit_imp.rs b/src/libstd/rt/at_exit_imp.rs index 08755ba829f..f6bb87f011d 100644 --- a/src/libstd/rt/at_exit_imp.rs +++ b/src/libstd/rt/at_exit_imp.rs @@ -12,6 +12,7 @@ //! //! Documentation can be found on the `rt::at_exit` function. +#[cfg(stage0)] use core::prelude::*; use boxed; |
