Skip to content

Commit 791244e

Browse files
committed
[must squash] use macro for i8 and u8, readd random_number
1 parent 96e17a3 commit 791244e

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

library/core/src/random.rs

+2-28
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,6 @@ impl Random for bool {
2929
}
3030
}
3131

32-
impl Random for u8 {
33-
/// Generates a random value.
34-
///
35-
/// **Warning:** Be careful when manipulating the resulting value! This
36-
/// method samples according to a uniform distribution, so a value of 1 is
37-
/// just as likely as [`MAX`](Self::MAX). By using modulo operations, some
38-
/// values can become more likely than others. Use audited crates when in
39-
/// doubt.
40-
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
41-
let mut byte = [0];
42-
source.fill_bytes(&mut byte);
43-
byte[0]
44-
}
45-
}
46-
47-
impl Random for i8 {
48-
/// Generates a random value.
49-
///
50-
/// **Warning:** Be careful when manipulating the resulting value! This
51-
/// method samples according to a uniform distribution, so a value of 1 is
52-
/// just as likely as [`MAX`](Self::MAX). By using modulo operations, some
53-
/// values can become more likely than others. Use audited crates when in
54-
/// doubt.
55-
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self {
56-
u8::random(source) as i8
57-
}
58-
}
59-
6032
macro_rules! impl_primitive {
6133
($t:ty) => {
6234
impl Random for $t {
@@ -76,6 +48,8 @@ macro_rules! impl_primitive {
7648
};
7749
}
7850

51+
impl_primitive!(u8);
52+
impl_primitive!(i8);
7953
impl_primitive!(u16);
8054
impl_primitive!(i16);
8155
impl_primitive!(u32);

library/std/src/sys/pal/windows/pipe.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
33
use crate::os::windows::prelude::*;
44
use crate::path::Path;
55
use crate::random::{DefaultRandomSource, Random};
6+
use crate::sync::atomic::AtomicUsize;
7+
use crate::sync::atomic::Ordering::Relaxed;
68
use crate::sys::c;
79
use crate::sys::fs::{File, OpenOptions};
810
use crate::sys::handle::Handle;
@@ -78,7 +80,7 @@ pub fn anon_pipe(ours_readable: bool, their_handle_inheritable: bool) -> io::Res
7880
name = format!(
7981
r"\\.\pipe\__rust_anonymous_pipe1__.{}.{}",
8082
c::GetCurrentProcessId(),
81-
usize::random(&mut DefaultRandomSource),
83+
random_number(),
8284
);
8385
let wide_name = OsStr::new(&name).encode_wide().chain(Some(0)).collect::<Vec<_>>();
8486
let mut flags = c::FILE_FLAG_FIRST_PIPE_INSTANCE | c::FILE_FLAG_OVERLAPPED;
@@ -206,6 +208,17 @@ pub fn spawn_pipe_relay(
206208
Ok(theirs)
207209
}
208210

211+
fn random_number() -> usize {
212+
static N: AtomicUsize = AtomicUsize::new(0);
213+
loop {
214+
if N.load(Relaxed) != 0 {
215+
return N.fetch_add(1, Relaxed);
216+
}
217+
218+
N.store(usize::random(&mut DefaultRandomSource), Relaxed);
219+
}
220+
}
221+
209222
impl AnonPipe {
210223
pub fn handle(&self) -> &Handle {
211224
&self.inner

0 commit comments

Comments
 (0)