Skip to content

Commit

Permalink
Remove the private generic NonZero<T> wrapper type.
Browse files Browse the repository at this point in the history
Instead, use `#[rustc_layout_scalar_valid_range_start(1)]` directly
on relevant libcore types.
  • Loading branch information
SimonSapin committed Dec 26, 2018
1 parent 79d8a0f commit 7a09115
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 53 deletions.
6 changes: 2 additions & 4 deletions src/etc/debugger_pretty_printers_common.py
Expand Up @@ -342,8 +342,7 @@ def extract_length_ptr_and_cap_from_std_vec(vec_val):

vec_ptr_val = buf.get_child_at_index(0)
capacity = buf.get_child_at_index(1).as_integer()
unique_ptr_val = vec_ptr_val.get_child_at_index(0)
data_ptr = unique_ptr_val.get_child_at_index(0)
data_ptr = vec_ptr_val.get_child_at_index(0)
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
return (length, data_ptr, capacity)

Expand All @@ -360,8 +359,7 @@ def extract_tail_head_ptr_and_cap_from_std_vecdeque(vec_val):

vec_ptr_val = buf.get_child_at_index(0)
capacity = buf.get_child_at_index(1).as_integer()
unique_ptr_val = vec_ptr_val.get_child_at_index(0)
data_ptr = unique_ptr_val.get_child_at_index(0)
data_ptr = vec_ptr_val.get_child_at_index(0)
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR
return (tail, head, data_ptr, capacity)

Expand Down
4 changes: 1 addition & 3 deletions src/etc/gdb_rust_pretty_printing.py
Expand Up @@ -322,9 +322,7 @@ def children(self):

# Yield each key (and optionally value) from a BoxedNode.
def children_of_node(boxed_node, height, want_values):
ptr = boxed_node['ptr']['pointer']
# This is written oddly because we don't want to rely on the field name being `__0`.
node_ptr = ptr[ptr.type.fields()[0]]
node_ptr = boxed_node['ptr']['pointer']
if height > 0:
type_name = str(node_ptr.type.target()).replace('LeafNode', 'InternalNode')
node_type = gdb.lookup_type(type_name)
Expand Down
1 change: 0 additions & 1 deletion src/libcore/lib.rs
Expand Up @@ -216,7 +216,6 @@ pub mod alloc;

// note: does not need to be public
mod iter_private;
mod nonzero;
mod tuple;
mod unit;

Expand Down
23 changes: 0 additions & 23 deletions src/libcore/nonzero.rs

This file was deleted.

12 changes: 6 additions & 6 deletions src/libcore/num/mod.rs
Expand Up @@ -6,7 +6,6 @@ use convert::TryFrom;
use fmt;
use intrinsics;
use mem;
use nonzero::NonZero;
use ops;
use str::FromStr;

Expand Down Expand Up @@ -48,7 +47,8 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
#[stable(feature = "nonzero", since = "1.28.0")]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct $Ty(NonZero<$Int>);
#[rustc_layout_scalar_valid_range_start(1)]
pub struct $Ty($Int);
}

impl $Ty {
Expand All @@ -60,15 +60,15 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
#[stable(feature = "nonzero", since = "1.28.0")]
#[inline]
pub const unsafe fn new_unchecked(n: $Int) -> Self {
$Ty(NonZero(n))
$Ty(n)
}

/// Create a non-zero if the given value is not zero.
#[stable(feature = "nonzero", since = "1.28.0")]
#[inline]
pub fn new(n: $Int) -> Option<Self> {
if n != 0 {
Some($Ty(unsafe { NonZero(n) }))
Some(unsafe { $Ty(n) })
} else {
None
}
Expand All @@ -78,15 +78,15 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
#[stable(feature = "nonzero", since = "1.28.0")]
#[inline]
pub fn get(self) -> $Int {
self.0 .0
self.0
}

}

#[stable(feature = "from_nonzero", since = "1.31.0")]
impl From<$Ty> for $Int {
fn from(nonzero: $Ty) -> Self {
nonzero.0 .0
nonzero.0
}
}

Expand Down
29 changes: 15 additions & 14 deletions src/libcore/ptr.rs
Expand Up @@ -70,7 +70,6 @@ use fmt;
use hash;
use marker::{PhantomData, Unsize};
use mem::{self, MaybeUninit};
use nonzero::NonZero;

use cmp::Ordering::{self, Less, Equal, Greater};

Expand Down Expand Up @@ -2718,8 +2717,9 @@ impl<T: ?Sized> PartialOrd for *mut T {
(if you also use #[may_dangle]), Send, and/or Sync")]
#[doc(hidden)]
#[repr(transparent)]
#[rustc_layout_scalar_valid_range_start(1)]
pub struct Unique<T: ?Sized> {
pointer: NonZero<*const T>,
pointer: *const T,
// NOTE: this marker has no consequences for variance, but is necessary
// for dropck to understand that we logically own a `T`.
//
Expand Down Expand Up @@ -2776,21 +2776,21 @@ impl<T: ?Sized> Unique<T> {
///
/// `ptr` must be non-null.
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
Unique { pointer: NonZero(ptr as _), _marker: PhantomData }
Unique { pointer: ptr as _, _marker: PhantomData }
}

/// Creates a new `Unique` if `ptr` is non-null.
pub fn new(ptr: *mut T) -> Option<Self> {
if !ptr.is_null() {
Some(Unique { pointer: unsafe { NonZero(ptr as _) }, _marker: PhantomData })
Some(unsafe { Unique { pointer: ptr as _, _marker: PhantomData } })
} else {
None
}
}

/// Acquires the underlying `*mut` pointer.
pub fn as_ptr(self) -> *mut T {
self.pointer.0 as *mut T
self.pointer as *mut T
}

/// Dereferences the content.
Expand Down Expand Up @@ -2838,21 +2838,21 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
#[unstable(feature = "ptr_internals", issue = "0")]
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
fn from(reference: &'a mut T) -> Self {
Unique { pointer: unsafe { NonZero(reference as *mut T) }, _marker: PhantomData }
unsafe { Unique { pointer: reference as *mut T, _marker: PhantomData } }
}
}

#[unstable(feature = "ptr_internals", issue = "0")]
impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
fn from(reference: &'a T) -> Self {
Unique { pointer: unsafe { NonZero(reference as *const T) }, _marker: PhantomData }
unsafe { Unique { pointer: reference as *const T, _marker: PhantomData } }
}
}

#[unstable(feature = "ptr_internals", issue = "0")]
impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
fn from(p: NonNull<T>) -> Self {
Unique { pointer: p.pointer, _marker: PhantomData }
unsafe { Unique { pointer: p.pointer, _marker: PhantomData } }
}
}

Expand All @@ -2875,8 +2875,9 @@ impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
/// provide a public API that follows the normal shared XOR mutable rules of Rust.
#[stable(feature = "nonnull", since = "1.25.0")]
#[repr(transparent)]
#[rustc_layout_scalar_valid_range_start(1)]
pub struct NonNull<T: ?Sized> {
pointer: NonZero<*const T>,
pointer: *const T,
}

/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
Expand Down Expand Up @@ -2918,7 +2919,7 @@ impl<T: ?Sized> NonNull<T> {
#[stable(feature = "nonnull", since = "1.25.0")]
#[inline]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
NonNull { pointer: NonZero(ptr as _) }
NonNull { pointer: ptr as _ }
}

/// Creates a new `NonNull` if `ptr` is non-null.
Expand All @@ -2936,7 +2937,7 @@ impl<T: ?Sized> NonNull<T> {
#[stable(feature = "nonnull", since = "1.25.0")]
#[inline]
pub const fn as_ptr(self) -> *mut T {
self.pointer.0 as *mut T
self.pointer as *mut T
}

/// Dereferences the content.
Expand Down Expand Up @@ -3040,22 +3041,22 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
#[inline]
fn from(unique: Unique<T>) -> Self {
NonNull { pointer: unique.pointer }
unsafe { NonNull { pointer: unique.pointer } }
}
}

#[stable(feature = "nonnull", since = "1.25.0")]
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
#[inline]
fn from(reference: &'a mut T) -> Self {
NonNull { pointer: unsafe { NonZero(reference as *mut T) } }
unsafe { NonNull { pointer: reference as *mut T } }
}
}

#[stable(feature = "nonnull", since = "1.25.0")]
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
#[inline]
fn from(reference: &'a T) -> Self {
NonNull { pointer: unsafe { NonZero(reference as *const T) } }
unsafe { NonNull { pointer: reference as *const T } }
}
}
2 changes: 0 additions & 2 deletions src/test/ui/print_type_sizes/niche-filling.stdout
Expand Up @@ -36,8 +36,6 @@ print-type-size type: `MyOption<std::num::NonZeroU32>`: 4 bytes, alignment: 4 by
print-type-size variant `None`: 0 bytes
print-type-size variant `Some`: 4 bytes
print-type-size field `.0`: 4 bytes
print-type-size type: `core::nonzero::NonZero<u32>`: 4 bytes, alignment: 4 bytes
print-type-size field `.0`: 4 bytes
print-type-size type: `std::num::NonZeroU32`: 4 bytes, alignment: 4 bytes
print-type-size field `.0`: 4 bytes
print-type-size type: `Enum4<(), (), (), MyOption<u8>>`: 2 bytes, alignment: 1 bytes
Expand Down

0 comments on commit 7a09115

Please sign in to comment.