Skip to content

Commit 7cf0259

Browse files
authored
Unrolled build for rust-lang#120160
Rollup merge of rust-lang#120160 - reitermarkus:nonzero-traits, r=dtolnay Manually implement derived `NonZero` traits. Step 3 as mentioned in rust-lang#100428 (review). Manually implement the traits that would cause “borrow of layout constrained field with interior mutability” errors when switching to `NonZero<T>`. r? ```@dtolnay```
2 parents 5d3d347 + 0e3035b commit 7cf0259

File tree

1 file changed

+101
-8
lines changed

1 file changed

+101
-8
lines changed

library/core/src/num/nonzero.rs

+101-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Definitions of integer that is known not to equal zero.
22
3+
use crate::cmp::Ordering;
34
use crate::fmt;
5+
use crate::hash::{Hash, Hasher};
6+
use crate::marker::StructuralPartialEq;
47
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
58
use crate::str::FromStr;
69

@@ -31,13 +34,6 @@ pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {
3134
type NonZero;
3235
}
3336

34-
#[unstable(
35-
feature = "nonzero_internals",
36-
reason = "implementation detail which may disappear or be replaced at any time",
37-
issue = "none"
38-
)]
39-
pub(crate) type NonZero<T> = <T as ZeroablePrimitive>::NonZero;
40-
4137
macro_rules! impl_zeroable_primitive {
4238
($NonZero:ident ( $primitive:ty )) => {
4339
#[unstable(
@@ -71,6 +67,13 @@ impl_zeroable_primitive!(NonZeroI64(i64));
7167
impl_zeroable_primitive!(NonZeroI128(i128));
7268
impl_zeroable_primitive!(NonZeroIsize(isize));
7369

70+
#[unstable(
71+
feature = "nonzero_internals",
72+
reason = "implementation detail which may disappear or be replaced at any time",
73+
issue = "none"
74+
)]
75+
pub(crate) type NonZero<T> = <T as ZeroablePrimitive>::NonZero;
76+
7477
macro_rules! impl_nonzero_fmt {
7578
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
7679
$(
@@ -128,7 +131,7 @@ macro_rules! nonzero_integer {
128131
///
129132
/// [null pointer optimization]: crate::option#representation
130133
#[$stability]
131-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
134+
#[derive(Copy, Eq)]
132135
#[repr(transparent)]
133136
#[rustc_layout_scalar_valid_range_start(1)]
134137
#[rustc_nonnull_optimization_guaranteed]
@@ -494,6 +497,96 @@ macro_rules! nonzero_integer {
494497
}
495498
}
496499

500+
#[$stability]
501+
impl Clone for $Ty {
502+
#[inline]
503+
fn clone(&self) -> Self {
504+
// SAFETY: The contained value is non-zero.
505+
unsafe { Self(self.0) }
506+
}
507+
}
508+
509+
#[$stability]
510+
impl PartialEq for $Ty {
511+
#[inline]
512+
fn eq(&self, other: &Self) -> bool {
513+
self.0 == other.0
514+
}
515+
516+
#[inline]
517+
fn ne(&self, other: &Self) -> bool {
518+
self.0 != other.0
519+
}
520+
}
521+
522+
#[unstable(feature = "structural_match", issue = "31434")]
523+
impl StructuralPartialEq for $Ty {}
524+
525+
#[$stability]
526+
impl PartialOrd for $Ty {
527+
#[inline]
528+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
529+
self.0.partial_cmp(&other.0)
530+
}
531+
532+
#[inline]
533+
fn lt(&self, other: &Self) -> bool {
534+
self.0 < other.0
535+
}
536+
537+
#[inline]
538+
fn le(&self, other: &Self) -> bool {
539+
self.0 <= other.0
540+
}
541+
542+
#[inline]
543+
fn gt(&self, other: &Self) -> bool {
544+
self.0 > other.0
545+
}
546+
547+
#[inline]
548+
fn ge(&self, other: &Self) -> bool {
549+
self.0 >= other.0
550+
}
551+
}
552+
553+
#[$stability]
554+
impl Ord for $Ty {
555+
#[inline]
556+
fn cmp(&self, other: &Self) -> Ordering {
557+
self.0.cmp(&other.0)
558+
}
559+
560+
#[inline]
561+
fn max(self, other: Self) -> Self {
562+
// SAFETY: The maximum of two non-zero values is still non-zero.
563+
unsafe { Self(self.0.max(other.0)) }
564+
}
565+
566+
#[inline]
567+
fn min(self, other: Self) -> Self {
568+
// SAFETY: The minimum of two non-zero values is still non-zero.
569+
unsafe { Self(self.0.min(other.0)) }
570+
}
571+
572+
#[inline]
573+
fn clamp(self, min: Self, max: Self) -> Self {
574+
// SAFETY: A non-zero value clamped between two non-zero values is still non-zero.
575+
unsafe { Self(self.0.clamp(min.0, max.0)) }
576+
}
577+
}
578+
579+
#[$stability]
580+
impl Hash for $Ty {
581+
#[inline]
582+
fn hash<H>(&self, state: &mut H)
583+
where
584+
H: Hasher,
585+
{
586+
self.0.hash(state)
587+
}
588+
}
589+
497590
#[stable(feature = "from_nonzero", since = "1.31.0")]
498591
impl From<$Ty> for $Int {
499592
#[doc = concat!("Converts a `", stringify!($Ty), "` into an `", stringify!($Int), "`")]

0 commit comments

Comments
 (0)