Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::hash::{Hash, Hasher};
use crate::marker::{Freeze, StructuralPartialEq};
use crate::ops::{BitOr, BitOrAssign, Div, DivAssign, Neg, Rem, RemAssign};
use crate::panic::{RefUnwindSafe, UnwindSafe};
use crate::random::{Random, RandomSource};
use crate::str::FromStr;
use crate::{fmt, intrinsics, ptr, ub_checks};

Expand Down Expand Up @@ -360,6 +361,17 @@ where
}
}

#[unstable(feature = "random", issue = "130703")]
impl<T: ZeroablePrimitive + Random> Random for NonZero<T> {
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
loop {
if let Some(n) = Self::new(T::random(source)) {
break n;
}
}
}
}

impl<T> NonZero<T>
where
T: ZeroablePrimitive,
Expand Down
8 changes: 8 additions & 0 deletions library/core/src/num/saturating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign,
};
use crate::random::{Random, RandomSource};

/// Provides intentionally-saturating arithmetic on `T`.
///
Expand Down Expand Up @@ -79,6 +80,13 @@ impl<T: fmt::UpperHex> fmt::UpperHex for Saturating<T> {
}
}

#[unstable(feature = "random", issue = "130703")]
impl<T: Random> Random for Saturating<T> {
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
Self(T::random(source))
}
}

// FIXME the correct implementation is not clear. Waiting for a real world use case at https://github.com/rust-lang/libs-team/issues/230
//
// #[allow(unused_macros)]
Expand Down
8 changes: 8 additions & 0 deletions library/core/src/num/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
};
use crate::random::{Random, RandomSource};

/// Provides intentionally-wrapped arithmetic on `T`.
///
Expand Down Expand Up @@ -84,6 +85,13 @@ impl<T: fmt::UpperHex> fmt::UpperHex for Wrapping<T> {
}
}

#[unstable(feature = "random", issue = "130703")]
impl<T: Random> Random for Wrapping<T> {
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
Self(T::random(source))
}
}

#[allow(unused_macros)]
macro_rules! sh_impl_signed {
($t:ident, $f:ident) => {
Expand Down
Loading