diff options
| author | Mark Simulacrum <mark.simulacrum@gmail.com> | 2017-07-26 06:15:01 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-07-26 06:15:01 -0600 |
| commit | b5b7266b786cb953581ba588d400bddcdae4a852 (patch) | |
| tree | c957402d4bf9f3f60cc718e86bf45ebba616bf4b /src/libcore | |
| parent | bad58f27916e7e233cc2916dcc9167708077e792 (diff) | |
| parent | 0d1864b8cf9585e6133aa3da2b06b29cbfb791bd (diff) | |
| download | rust-b5b7266b786cb953581ba588d400bddcdae4a852.tar.gz rust-b5b7266b786cb953581ba588d400bddcdae4a852.zip | |
Rollup merge of #42959 - SimonSapin:nonzero-checked, r=sfackler
Make the "main" constructors of NonZero/Shared/Unique return Option Per discussion in https://github.com/rust-lang/rust/issues/27730#issuecomment-303939441. This is a breaking change to unstable APIs. The old behavior is still available under the name `new_unchecked`. Note that only that one can be `const fn`, since `if` is currently not allowed in constant contexts. In the case of `NonZero` this requires adding a new `is_zero` method to the `Zeroable` trait. I mildly dislike this, but it’s not much worse than having a `Zeroable` trait in the first place. `Zeroable` and `NonZero` are both unstable, this can be reworked later.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/nonzero.rs | 89 | ||||
| -rw-r--r-- | src/libcore/ptr.rs | 62 | ||||
| -rw-r--r-- | src/libcore/tests/nonzero.rs | 6 | ||||
| -rw-r--r-- | src/libcore/tests/ptr.rs | 2 |
4 files changed, 130 insertions, 29 deletions
diff --git a/src/libcore/nonzero.rs b/src/libcore/nonzero.rs index 977438051d9..3ff1068b937 100644 --- a/src/libcore/nonzero.rs +++ b/src/libcore/nonzero.rs @@ -16,22 +16,48 @@ use ops::CoerceUnsized; /// Unsafe trait to indicate what types are usable with the NonZero struct -pub unsafe trait Zeroable {} - -unsafe impl<T:?Sized> Zeroable for *const T {} -unsafe impl<T:?Sized> Zeroable for *mut T {} -unsafe impl Zeroable for isize {} -unsafe impl Zeroable for usize {} -unsafe impl Zeroable for i8 {} -unsafe impl Zeroable for u8 {} -unsafe impl Zeroable for i16 {} -unsafe impl Zeroable for u16 {} -unsafe impl Zeroable for i32 {} -unsafe impl Zeroable for u32 {} -unsafe impl Zeroable for i64 {} -unsafe impl Zeroable for u64 {} -unsafe impl Zeroable for i128 {} -unsafe impl Zeroable for u128 {} +pub unsafe trait Zeroable { + /// Whether this value is zero + fn is_zero(&self) -> bool; +} + +macro_rules! impl_zeroable_for_pointer_types { + ( $( $Ptr: ty )+ ) => { + $( + /// For fat pointers to be considered "zero", only the "data" part needs to be null. + unsafe impl<T: ?Sized> Zeroable for $Ptr { + #[inline] + fn is_zero(&self) -> bool { + // Cast because `is_null` is only available on thin pointers + (*self as *mut u8).is_null() + } + } + )+ + } +} + +macro_rules! impl_zeroable_for_integer_types { + ( $( $Int: ty )+ ) => { + $( + unsafe impl Zeroable for $Int { + #[inline] + fn is_zero(&self) -> bool { + *self == 0 + } + } + )+ + } +} + +impl_zeroable_for_pointer_types! { + *const T + *mut T +} + +impl_zeroable_for_integer_types! { + usize u8 u16 u32 u64 u128 + isize i8 i16 i32 i64 i128 +} /// A wrapper type for raw pointers and integers that will never be /// NULL or 0 that might allow certain optimizations. @@ -43,10 +69,20 @@ impl<T: Zeroable> NonZero<T> { /// Creates an instance of NonZero with the provided value. /// You must indeed ensure that the value is actually "non-zero". #[inline] - pub const unsafe fn new(inner: T) -> NonZero<T> { + pub const unsafe fn new_unchecked(inner: T) -> Self { NonZero(inner) } + /// Creates an instance of NonZero with the provided value. + #[inline] + pub fn new(inner: T) -> Option<Self> { + if inner.is_zero() { + None + } else { + Some(NonZero(inner)) + } + } + /// Gets the inner value. pub fn get(self) -> T { self.0 @@ -54,3 +90,22 @@ impl<T: Zeroable> NonZero<T> { } impl<T: Zeroable+CoerceUnsized<U>, U: Zeroable> CoerceUnsized<NonZero<U>> for NonZero<T> {} + +impl<'a, T: ?Sized> From<&'a mut T> for NonZero<*mut T> { + fn from(reference: &'a mut T) -> Self { + NonZero(reference) + } +} + +impl<'a, T: ?Sized> From<&'a mut T> for NonZero<*const T> { + fn from(reference: &'a mut T) -> Self { + let ptr: *mut T = reference; + NonZero(ptr) + } +} + +impl<'a, T: ?Sized> From<&'a T> for NonZero<*const T> { + fn from(reference: &'a T) -> Self { + NonZero(reference) + } +} diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index b19e07b8578..60cf1a20530 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -16,6 +16,7 @@ #![stable(feature = "rust1", since = "1.0.0")] +use convert::From; use intrinsics; use ops::CoerceUnsized; use fmt; @@ -1098,7 +1099,7 @@ impl<T: Sized> Unique<T> { pub fn empty() -> Self { unsafe { let ptr = mem::align_of::<T>() as *mut T; - Unique::new(ptr) + Unique::new_unchecked(ptr) } } } @@ -1110,8 +1111,13 @@ impl<T: ?Sized> Unique<T> { /// # Safety /// /// `ptr` must be non-null. - pub const unsafe fn new(ptr: *mut T) -> Unique<T> { - Unique { pointer: NonZero::new(ptr), _marker: PhantomData } + pub const unsafe fn new_unchecked(ptr: *mut T) -> Self { + Unique { pointer: NonZero::new_unchecked(ptr), _marker: PhantomData } + } + + /// Creates a new `Unique` if `ptr` is non-null. + pub fn new(ptr: *mut T) -> Option<Self> { + NonZero::new(ptr as *const T).map(|nz| Unique { pointer: nz, _marker: PhantomData }) } /// Acquires the underlying `*mut` pointer. @@ -1138,14 +1144,14 @@ impl<T: ?Sized> Unique<T> { } } -#[unstable(feature = "shared", issue = "27730")] +#[unstable(feature = "unique", issue = "27730")] impl<T: ?Sized> Clone for Unique<T> { fn clone(&self) -> Self { *self } } -#[unstable(feature = "shared", issue = "27730")] +#[unstable(feature = "unique", issue = "27730")] impl<T: ?Sized> Copy for Unique<T> { } #[unstable(feature = "unique", issue = "27730")] @@ -1158,6 +1164,20 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> { } } +#[unstable(feature = "unique", issue = "27730")] +impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> { + fn from(reference: &'a mut T) -> Self { + Unique { pointer: NonZero::from(reference), _marker: PhantomData } + } +} + +#[unstable(feature = "unique", issue = "27730")] +impl<'a, T: ?Sized> From<&'a T> for Unique<T> { + fn from(reference: &'a T) -> Self { + Unique { pointer: NonZero::from(reference), _marker: PhantomData } + } +} + /// A wrapper around a raw `*mut T` that indicates that the possessor /// of this wrapper has shared ownership of the referent. Useful for /// building abstractions like `Rc<T>`, `Arc<T>`, or doubly-linked lists, which @@ -1212,7 +1232,7 @@ impl<T: Sized> Shared<T> { pub fn empty() -> Self { unsafe { let ptr = mem::align_of::<T>() as *mut T; - Shared::new(ptr) + Shared::new_unchecked(ptr) } } } @@ -1224,8 +1244,13 @@ impl<T: ?Sized> Shared<T> { /// # Safety /// /// `ptr` must be non-null. - pub unsafe fn new(ptr: *mut T) -> Self { - Shared { pointer: NonZero::new(ptr), _marker: PhantomData } + pub const unsafe fn new_unchecked(ptr: *mut T) -> Self { + Shared { pointer: NonZero::new_unchecked(ptr), _marker: PhantomData } + } + + /// Creates a new `Shared` if `ptr` is non-null. + pub fn new(ptr: *mut T) -> Option<Self> { + NonZero::new(ptr as *const T).map(|nz| Shared { pointer: nz, _marker: PhantomData }) } /// Acquires the underlying `*mut` pointer. @@ -1278,3 +1303,24 @@ impl<T: ?Sized> fmt::Pointer for Shared<T> { fmt::Pointer::fmt(&self.as_ptr(), f) } } + +#[unstable(feature = "shared", issue = "27730")] +impl<T: ?Sized> From<Unique<T>> for Shared<T> { + fn from(unique: Unique<T>) -> Self { + Shared { pointer: unique.pointer, _marker: PhantomData } + } +} + +#[unstable(feature = "shared", issue = "27730")] +impl<'a, T: ?Sized> From<&'a mut T> for Shared<T> { + fn from(reference: &'a mut T) -> Self { + Shared { pointer: NonZero::from(reference), _marker: PhantomData } + } +} + +#[unstable(feature = "shared", issue = "27730")] +impl<'a, T: ?Sized> From<&'a T> for Shared<T> { + fn from(reference: &'a T) -> Self { + Shared { pointer: NonZero::from(reference), _marker: PhantomData } + } +} diff --git a/src/libcore/tests/nonzero.rs b/src/libcore/tests/nonzero.rs index 588fffda35f..a795dd57504 100644 --- a/src/libcore/tests/nonzero.rs +++ b/src/libcore/tests/nonzero.rs @@ -16,7 +16,7 @@ use std::mem::size_of; #[test] fn test_create_nonzero_instance() { let _a = unsafe { - NonZero::new(21) + NonZero::new_unchecked(21) }; } @@ -28,14 +28,14 @@ fn test_size_nonzero_in_option() { #[test] fn test_match_on_nonzero_option() { let a = Some(unsafe { - NonZero::new(42) + NonZero::new_unchecked(42) }); match a { Some(val) => assert_eq!(val.get(), 42), None => panic!("unexpected None while matching on Some(NonZero(_))") } - match unsafe { Some(NonZero::new(43)) } { + match unsafe { Some(NonZero::new_unchecked(43)) } { Some(val) => assert_eq!(val.get(), 43), None => panic!("unexpected None while matching on Some(NonZero(_))") } diff --git a/src/libcore/tests/ptr.rs b/src/libcore/tests/ptr.rs index e28dc6a6881..c2d53840f8f 100644 --- a/src/libcore/tests/ptr.rs +++ b/src/libcore/tests/ptr.rs @@ -167,7 +167,7 @@ fn test_set_memory() { #[test] fn test_unsized_unique() { let xs: &[i32] = &[1, 2, 3]; - let ptr = unsafe { Unique::new(xs as *const [i32] as *mut [i32]) }; + let ptr = unsafe { Unique::new_unchecked(xs as *const [i32] as *mut [i32]) }; let ys = unsafe { ptr.as_ref() }; let zs: &[i32] = &[1, 2, 3]; assert!(ys == zs); |
