forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#130755 - workingjubilee:rollup-zpja9b3, r=wor…
…kingjubilee Rollup of 7 pull requests Successful merges: - rust-lang#129201 (std: implement the `random` feature (alternative version)) - rust-lang#130536 (bootstrap: Set the dylib path when building books with rustdoc) - rust-lang#130551 (Fix `break_last_token`.) - rust-lang#130657 (Remove x86_64-fuchsia and aarch64-fuchsia target aliases) - rust-lang#130721 (Add more test cases for block-no-opening-brace) - rust-lang#130736 (Add rustfmt 2024 reformatting to git blame ignore) - rust-lang#130746 (readd `@tgross35` and `@joboet` to the review rotation) r? `@ghost` `@rustbot` modify labels: rollup
- Loading branch information
Showing
59 changed files
with
948 additions
and
565 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! Random value generation. | ||
//! | ||
//! The [`Random`] trait allows generating a random value for a type using a | ||
//! given [`RandomSource`]. | ||
|
||
/// A source of randomness. | ||
#[unstable(feature = "random", issue = "130703")] | ||
pub trait RandomSource { | ||
/// Fills `bytes` with random bytes. | ||
fn fill_bytes(&mut self, bytes: &mut [u8]); | ||
} | ||
|
||
/// A trait for getting a random value for a type. | ||
/// | ||
/// **Warning:** Be careful when manipulating random values! The | ||
/// [`random`](Random::random) method on integers samples them with a uniform | ||
/// distribution, so a value of 1 is just as likely as [`i32::MAX`]. By using | ||
/// modulo operations, some of the resulting values can become more likely than | ||
/// others. Use audited crates when in doubt. | ||
#[unstable(feature = "random", issue = "130703")] | ||
pub trait Random: Sized { | ||
/// Generates a random value. | ||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self; | ||
} | ||
|
||
impl Random for bool { | ||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||
u8::random(source) & 1 == 1 | ||
} | ||
} | ||
|
||
macro_rules! impl_primitive { | ||
($t:ty) => { | ||
impl Random for $t { | ||
/// Generates a random value. | ||
/// | ||
/// **Warning:** Be careful when manipulating the resulting value! This | ||
/// method samples according to a uniform distribution, so a value of 1 is | ||
/// just as likely as [`MAX`](Self::MAX). By using modulo operations, some | ||
/// values can become more likely than others. Use audited crates when in | ||
/// doubt. | ||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||
let mut bytes = (0 as Self).to_ne_bytes(); | ||
source.fill_bytes(&mut bytes); | ||
Self::from_ne_bytes(bytes) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_primitive!(u8); | ||
impl_primitive!(i8); | ||
impl_primitive!(u16); | ||
impl_primitive!(i16); | ||
impl_primitive!(u32); | ||
impl_primitive!(i32); | ||
impl_primitive!(u64); | ||
impl_primitive!(i64); | ||
impl_primitive!(u128); | ||
impl_primitive!(i128); | ||
impl_primitive!(usize); | ||
impl_primitive!(isize); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.