Skip to content

Commit 77de8b2

Browse files
committed
Implement Random for extra integer types
Implement `Random` for `num::{NonZero,Saturating,Wrapping}`.
1 parent e060723 commit 77de8b2

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

library/core/src/num/nonzero.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::hash::{Hash, Hasher};
66
use crate::marker::{Freeze, StructuralPartialEq};
77
use crate::ops::{BitOr, BitOrAssign, Div, DivAssign, Neg, Rem, RemAssign};
88
use crate::panic::{RefUnwindSafe, UnwindSafe};
9+
use crate::random::{Random, RandomSource};
910
use crate::str::FromStr;
1011
use crate::{fmt, intrinsics, ptr, ub_checks};
1112

@@ -360,6 +361,17 @@ where
360361
}
361362
}
362363

364+
#[unstable(feature = "random", issue = "130703")]
365+
impl<T: ZeroablePrimitive + Random> Random for NonZero<T> {
366+
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
367+
loop {
368+
if let Some(n) = Self::new(T::random(source)) {
369+
break n;
370+
}
371+
}
372+
}
373+
}
374+
363375
impl<T> NonZero<T>
364376
where
365377
T: ZeroablePrimitive,

library/core/src/num/saturating.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::ops::{
55
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
66
Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign,
77
};
8+
use crate::random::{Random, RandomSource};
89

910
/// Provides intentionally-saturating arithmetic on `T`.
1011
///
@@ -79,6 +80,13 @@ impl<T: fmt::UpperHex> fmt::UpperHex for Saturating<T> {
7980
}
8081
}
8182

83+
#[unstable(feature = "random", issue = "130703")]
84+
impl<T: Random> Random for Saturating<T> {
85+
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
86+
Self(T::random(source))
87+
}
88+
}
89+
8290
// FIXME the correct implementation is not clear. Waiting for a real world use case at https://github.com/rust-lang/libs-team/issues/230
8391
//
8492
// #[allow(unused_macros)]

library/core/src/num/wrapping.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::ops::{
55
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
66
Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
77
};
8+
use crate::random::{Random, RandomSource};
89

910
/// Provides intentionally-wrapped arithmetic on `T`.
1011
///
@@ -84,6 +85,13 @@ impl<T: fmt::UpperHex> fmt::UpperHex for Wrapping<T> {
8485
}
8586
}
8687

88+
#[unstable(feature = "random", issue = "130703")]
89+
impl<T: Random> Random for Wrapping<T> {
90+
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
91+
Self(T::random(source))
92+
}
93+
}
94+
8795
#[allow(unused_macros)]
8896
macro_rules! sh_impl_signed {
8997
($t:ident, $f:ident) => {

0 commit comments

Comments
 (0)