Skip to content

Commit cada1d7

Browse files
authored
Unrolled build for #136801
Rollup merge of #136801 - sorairolake:add-random-for-tuple, r=joshtriplett Implement `Random` for tuple Implement `Random` for tuples of arity 12 or less. Each element is expected to implement `Random`. I think it's OK to implement this trait for the following types: - Primitive integer types and `bool` - Arrays and tuples of the above values - ~~`NonZero<T>`~~, `Saturating<T>` and `Wrapping<T>` The necessity of this trait is debated (<#130703 (comment)>), but if we decide to keep it in the future when the `random` module is stabilized, I think it would be useful to have this trait implemented for tuples. Tracking issue: #130703 r? `@joboet`
2 parents 4e97337 + 07e8b28 commit cada1d7

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

library/core/src/primitive_docs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,11 +1083,13 @@ mod prim_str {}
10831083
/// * [`Debug`]
10841084
/// * [`Default`]
10851085
/// * [`Hash`]
1086+
/// * [`Random`]
10861087
/// * [`From<[T; N]>`][from]
10871088
///
10881089
/// [from]: convert::From
10891090
/// [`Debug`]: fmt::Debug
10901091
/// [`Hash`]: hash::Hash
1092+
/// [`Random`]: random::Random
10911093
///
10921094
/// The following traits are implemented for tuples of any length. These traits have
10931095
/// implementations that are automatically generated by the compiler, so are not limited by

library/core/src/tuple.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::cmp::Ordering::{self, *};
44
use crate::marker::{ConstParamTy_, PointeeSized, StructuralPartialEq, UnsizedConstParamTy};
55
use crate::ops::ControlFlow::{self, Break, Continue};
6+
use crate::random::{Random, RandomSource};
67

78
// Recursive macro for implementing n-ary tuple functions and operations
89
//
@@ -139,6 +140,16 @@ macro_rules! tuple_impls {
139140
}
140141
}
141142

143+
maybe_tuple_doc! {
144+
$($T)+ @
145+
#[unstable(feature = "random", issue = "130703")]
146+
impl<$($T: Random),+> Random for ($($T,)+) {
147+
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
148+
($({ let x: $T = Random::random(source); x},)+)
149+
}
150+
}
151+
}
152+
142153
maybe_tuple_doc! {
143154
$($T)+ @
144155
#[stable(feature = "array_tuple_conv", since = "1.71.0")]

0 commit comments

Comments
 (0)