Skip to content

Commit 7433ba6

Browse files
committed
std: get rid of sys_common::io
1 parent a9df224 commit 7433ba6

File tree

17 files changed

+84
-88
lines changed

17 files changed

+84
-88
lines changed

library/std/src/fs/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::os::unix::fs::symlink as junction_point;
1414
use crate::os::windows::fs::{OpenOptionsExt, junction_point, symlink_dir, symlink_file};
1515
use crate::path::Path;
1616
use crate::sync::Arc;
17-
use crate::sys_common::io::test::{TempDir, tmpdir};
17+
use crate::test_helpers::{TempDir, tmpdir};
1818
use crate::time::{Duration, Instant, SystemTime};
1919
use crate::{env, str, thread};
2020

library/std/src/io/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ pub mod prelude;
344344
mod stdio;
345345
mod util;
346346

347-
const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
347+
const DEFAULT_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
348348

349349
pub(crate) use stdio::cleanup;
350350

library/std/src/lib.rs

+1-24
Original file line numberDiff line numberDiff line change
@@ -739,27 +739,4 @@ mod sealed {
739739

740740
#[cfg(test)]
741741
#[allow(dead_code)] // Not used in all configurations.
742-
pub(crate) mod test_helpers {
743-
/// Test-only replacement for `rand::thread_rng()`, which is unusable for
744-
/// us, as we want to allow running stdlib tests on tier-3 targets which may
745-
/// not have `getrandom` support.
746-
///
747-
/// Does a bit of a song and dance to ensure that the seed is different on
748-
/// each call (as some tests sadly rely on this), but doesn't try that hard.
749-
///
750-
/// This is duplicated in the `core`, `alloc` test suites (as well as
751-
/// `std`'s integration tests), but figuring out a mechanism to share these
752-
/// seems far more painful than copy-pasting a 7 line function a couple
753-
/// times, given that even under a perma-unstable feature, I don't think we
754-
/// want to expose types from `rand` from `std`.
755-
#[track_caller]
756-
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
757-
use core::hash::{BuildHasher, Hash, Hasher};
758-
let mut hasher = crate::hash::RandomState::new().build_hasher();
759-
core::panic::Location::caller().hash(&mut hasher);
760-
let hc64 = hasher.finish();
761-
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
762-
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
763-
rand::SeedableRng::from_seed(seed)
764-
}
765-
}
742+
pub(crate) mod test_helpers;

library/std/src/os/unix/fs/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::*;
33
#[test]
44
fn read_vectored_at() {
55
let msg = b"preadv is working!";
6-
let dir = crate::sys_common::io::test::tmpdir();
6+
let dir = crate::test_helpers::tmpdir();
77

88
let filename = dir.join("preadv.txt");
99
{
@@ -31,7 +31,7 @@ fn read_vectored_at() {
3131
#[test]
3232
fn write_vectored_at() {
3333
let msg = b"pwritev is not working!";
34-
let dir = crate::sys_common::io::test::tmpdir();
34+
let dir = crate::test_helpers::tmpdir();
3535

3636
let filename = dir.join("preadv.txt");
3737
{

library/std/src/os/unix/net/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::os::android::net::{SocketAddrExt, UnixSocketExt};
77
use crate::os::linux::net::{SocketAddrExt, UnixSocketExt};
88
#[cfg(any(target_os = "android", target_os = "linux"))]
99
use crate::os::unix::io::AsRawFd;
10-
use crate::sys_common::io::test::tmpdir;
10+
use crate::test_helpers::tmpdir;
1111
use crate::thread;
1212
use crate::time::Duration;
1313

library/std/src/process/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ fn debug_print() {
697697
#[test]
698698
#[cfg(windows)]
699699
fn run_bat_script() {
700-
let tempdir = crate::sys_common::io::test::tmpdir();
700+
let tempdir = crate::test_helpers::tmpdir();
701701
let script_path = tempdir.join("hello.cmd");
702702

703703
crate::fs::write(&script_path, "@echo Hello, %~1!").unwrap();
@@ -716,7 +716,7 @@ fn run_bat_script() {
716716
#[test]
717717
#[cfg(windows)]
718718
fn run_canonical_bat_script() {
719-
let tempdir = crate::sys_common::io::test::tmpdir();
719+
let tempdir = crate::test_helpers::tmpdir();
720720
let script_path = tempdir.join("hello.cmd");
721721

722722
crate::fs::write(&script_path, "@echo Hello, %~1!").unwrap();

library/std/src/sys/io/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ mod is_terminal {
3838

3939
pub use io_slice::{IoSlice, IoSliceMut};
4040
pub use is_terminal::is_terminal;
41+
42+
// Bare metal platforms usually have very small amounts of RAM
43+
// (in the order of hundreds of KB)
44+
pub const DEFAULT_BUF_SIZE: usize = if cfg!(target_os = "espidf") { 512 } else { 8 * 1024 };

library/std/src/sys/pal/sgx/stdio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl io::Write for Stderr {
6262
}
6363
}
6464

65-
pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
65+
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
6666

6767
pub fn is_ebadf(err: &io::Error) -> bool {
6868
// FIXME: Rust normally maps Unix EBADF to `Uncategorized`

library/std/src/sys/pal/unix/kernel_copy/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::fs::OpenOptions;
22
use crate::io;
33
use crate::io::{BufRead, Read, Result, Seek, SeekFrom, Write};
44
use crate::os::unix::io::AsRawFd;
5-
use crate::sys_common::io::test::tmpdir;
5+
use crate::test_helpers::tmpdir;
66

77
#[test]
88
fn copy_specialization() -> Result<()> {

library/std/src/sys/pal/unix/stdio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn is_ebadf(err: &io::Error) -> bool {
9292
err.raw_os_error() == Some(libc::EBADF as i32)
9393
}
9494

95-
pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
95+
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
9696

9797
pub fn panic_output() -> Option<impl io::Write> {
9898
Some(Stderr::new())

library/std/src/sys/pal/wasi/stdio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl io::Write for Stderr {
101101
}
102102
}
103103

104-
pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
104+
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
105105

106106
pub fn is_ebadf(err: &io::Error) -> bool {
107107
err.raw_os_error() == Some(wasi::ERRNO_BADF.raw().into())

library/std/src/sys/pal/windows/process/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn windows_exe_resolver() {
158158
use super::resolve_exe;
159159
use crate::io;
160160
use crate::sys::fs::symlink;
161-
use crate::sys_common::io::test::tmpdir;
161+
use crate::test_helpers::tmpdir;
162162

163163
let env_paths = || env::var_os("PATH");
164164

library/std/src/sys/pal/zkvm/stdio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl io::Write for Stderr {
5454
}
5555
}
5656

57-
pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
57+
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
5858

5959
pub fn is_ebadf(_err: &io::Error) -> bool {
6060
true

library/std/src/sys_common/io.rs

-49
This file was deleted.

library/std/src/sys_common/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
mod tests;
2222

2323
pub mod fs;
24-
pub mod io;
2524
pub mod process;
2625
pub mod wstr;
2726
pub mod wtf8;

library/std/src/test_helpers.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use rand::{RngCore, SeedableRng};
2+
3+
use crate::hash::{BuildHasher, Hash, Hasher, RandomState};
4+
use crate::panic::Location;
5+
use crate::path::{Path, PathBuf};
6+
use crate::{env, fs, thread};
7+
8+
/// Test-only replacement for `rand::thread_rng()`, which is unusable for
9+
/// us, as we want to allow running stdlib tests on tier-3 targets which may
10+
/// not have `getrandom` support.
11+
///
12+
/// Does a bit of a song and dance to ensure that the seed is different on
13+
/// each call (as some tests sadly rely on this), but doesn't try that hard.
14+
///
15+
/// This is duplicated in the `core`, `alloc` test suites (as well as
16+
/// `std`'s integration tests), but figuring out a mechanism to share these
17+
/// seems far more painful than copy-pasting a 7 line function a couple
18+
/// times, given that even under a perma-unstable feature, I don't think we
19+
/// want to expose types from `rand` from `std`.
20+
#[track_caller]
21+
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
22+
let mut hasher = RandomState::new().build_hasher();
23+
Location::caller().hash(&mut hasher);
24+
let hc64 = hasher.finish();
25+
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
26+
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
27+
SeedableRng::from_seed(seed)
28+
}
29+
30+
pub struct TempDir(PathBuf);
31+
32+
impl TempDir {
33+
pub fn join(&self, path: &str) -> PathBuf {
34+
let TempDir(ref p) = *self;
35+
p.join(path)
36+
}
37+
38+
pub fn path(&self) -> &Path {
39+
let TempDir(ref p) = *self;
40+
p
41+
}
42+
}
43+
44+
impl Drop for TempDir {
45+
fn drop(&mut self) {
46+
// Gee, seeing how we're testing the fs module I sure hope that we
47+
// at least implement this correctly!
48+
let TempDir(ref p) = *self;
49+
let result = fs::remove_dir_all(p);
50+
// Avoid panicking while panicking as this causes the process to
51+
// immediately abort, without displaying test results.
52+
if !thread::panicking() {
53+
result.unwrap();
54+
}
55+
}
56+
}
57+
58+
#[track_caller] // for `test_rng`
59+
pub fn tmpdir() -> TempDir {
60+
let p = env::temp_dir();
61+
let mut r = test_rng();
62+
let ret = p.join(&format!("rust-{}", r.next_u32()));
63+
fs::create_dir(&ret).unwrap();
64+
TempDir(ret)
65+
}

library/std/tests/common/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
1818
rand::SeedableRng::from_seed(seed)
1919
}
2020

21-
// Copied from std::sys_common::io
21+
// Copied from std::test_helpers
2222
pub(crate) struct TempDir(PathBuf);
2323

2424
impl TempDir {

0 commit comments

Comments
 (0)