std: get rid of sys_common::io
This commit is contained in:
parent
a9df224ac7
commit
7433ba62b1
17 changed files with 84 additions and 88 deletions
|
@ -14,7 +14,7 @@ use crate::os::unix::fs::symlink as junction_point;
|
||||||
use crate::os::windows::fs::{OpenOptionsExt, junction_point, symlink_dir, symlink_file};
|
use crate::os::windows::fs::{OpenOptionsExt, junction_point, symlink_dir, symlink_file};
|
||||||
use crate::path::Path;
|
use crate::path::Path;
|
||||||
use crate::sync::Arc;
|
use crate::sync::Arc;
|
||||||
use crate::sys_common::io::test::{TempDir, tmpdir};
|
use crate::test_helpers::{TempDir, tmpdir};
|
||||||
use crate::time::{Duration, Instant, SystemTime};
|
use crate::time::{Duration, Instant, SystemTime};
|
||||||
use crate::{env, str, thread};
|
use crate::{env, str, thread};
|
||||||
|
|
||||||
|
|
|
@ -344,7 +344,7 @@ pub mod prelude;
|
||||||
mod stdio;
|
mod stdio;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
const DEFAULT_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
|
const DEFAULT_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
|
||||||
|
|
||||||
pub(crate) use stdio::cleanup;
|
pub(crate) use stdio::cleanup;
|
||||||
|
|
||||||
|
|
|
@ -739,27 +739,4 @@ mod sealed {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[allow(dead_code)] // Not used in all configurations.
|
#[allow(dead_code)] // Not used in all configurations.
|
||||||
pub(crate) mod test_helpers {
|
pub(crate) mod test_helpers;
|
||||||
/// Test-only replacement for `rand::thread_rng()`, which is unusable for
|
|
||||||
/// us, as we want to allow running stdlib tests on tier-3 targets which may
|
|
||||||
/// not have `getrandom` support.
|
|
||||||
///
|
|
||||||
/// Does a bit of a song and dance to ensure that the seed is different on
|
|
||||||
/// each call (as some tests sadly rely on this), but doesn't try that hard.
|
|
||||||
///
|
|
||||||
/// This is duplicated in the `core`, `alloc` test suites (as well as
|
|
||||||
/// `std`'s integration tests), but figuring out a mechanism to share these
|
|
||||||
/// seems far more painful than copy-pasting a 7 line function a couple
|
|
||||||
/// times, given that even under a perma-unstable feature, I don't think we
|
|
||||||
/// want to expose types from `rand` from `std`.
|
|
||||||
#[track_caller]
|
|
||||||
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
|
|
||||||
use core::hash::{BuildHasher, Hash, Hasher};
|
|
||||||
let mut hasher = crate::hash::RandomState::new().build_hasher();
|
|
||||||
core::panic::Location::caller().hash(&mut hasher);
|
|
||||||
let hc64 = hasher.finish();
|
|
||||||
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
|
|
||||||
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
|
|
||||||
rand::SeedableRng::from_seed(seed)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use super::*;
|
||||||
#[test]
|
#[test]
|
||||||
fn read_vectored_at() {
|
fn read_vectored_at() {
|
||||||
let msg = b"preadv is working!";
|
let msg = b"preadv is working!";
|
||||||
let dir = crate::sys_common::io::test::tmpdir();
|
let dir = crate::test_helpers::tmpdir();
|
||||||
|
|
||||||
let filename = dir.join("preadv.txt");
|
let filename = dir.join("preadv.txt");
|
||||||
{
|
{
|
||||||
|
@ -31,7 +31,7 @@ fn read_vectored_at() {
|
||||||
#[test]
|
#[test]
|
||||||
fn write_vectored_at() {
|
fn write_vectored_at() {
|
||||||
let msg = b"pwritev is not working!";
|
let msg = b"pwritev is not working!";
|
||||||
let dir = crate::sys_common::io::test::tmpdir();
|
let dir = crate::test_helpers::tmpdir();
|
||||||
|
|
||||||
let filename = dir.join("preadv.txt");
|
let filename = dir.join("preadv.txt");
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,7 +7,7 @@ use crate::os::android::net::{SocketAddrExt, UnixSocketExt};
|
||||||
use crate::os::linux::net::{SocketAddrExt, UnixSocketExt};
|
use crate::os::linux::net::{SocketAddrExt, UnixSocketExt};
|
||||||
#[cfg(any(target_os = "android", target_os = "linux"))]
|
#[cfg(any(target_os = "android", target_os = "linux"))]
|
||||||
use crate::os::unix::io::AsRawFd;
|
use crate::os::unix::io::AsRawFd;
|
||||||
use crate::sys_common::io::test::tmpdir;
|
use crate::test_helpers::tmpdir;
|
||||||
use crate::thread;
|
use crate::thread;
|
||||||
use crate::time::Duration;
|
use crate::time::Duration;
|
||||||
|
|
||||||
|
|
|
@ -697,7 +697,7 @@ fn debug_print() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
fn run_bat_script() {
|
fn run_bat_script() {
|
||||||
let tempdir = crate::sys_common::io::test::tmpdir();
|
let tempdir = crate::test_helpers::tmpdir();
|
||||||
let script_path = tempdir.join("hello.cmd");
|
let script_path = tempdir.join("hello.cmd");
|
||||||
|
|
||||||
crate::fs::write(&script_path, "@echo Hello, %~1!").unwrap();
|
crate::fs::write(&script_path, "@echo Hello, %~1!").unwrap();
|
||||||
|
@ -716,7 +716,7 @@ fn run_bat_script() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
fn run_canonical_bat_script() {
|
fn run_canonical_bat_script() {
|
||||||
let tempdir = crate::sys_common::io::test::tmpdir();
|
let tempdir = crate::test_helpers::tmpdir();
|
||||||
let script_path = tempdir.join("hello.cmd");
|
let script_path = tempdir.join("hello.cmd");
|
||||||
|
|
||||||
crate::fs::write(&script_path, "@echo Hello, %~1!").unwrap();
|
crate::fs::write(&script_path, "@echo Hello, %~1!").unwrap();
|
||||||
|
|
|
@ -38,3 +38,7 @@ mod is_terminal {
|
||||||
|
|
||||||
pub use io_slice::{IoSlice, IoSliceMut};
|
pub use io_slice::{IoSlice, IoSliceMut};
|
||||||
pub use is_terminal::is_terminal;
|
pub use is_terminal::is_terminal;
|
||||||
|
|
||||||
|
// Bare metal platforms usually have very small amounts of RAM
|
||||||
|
// (in the order of hundreds of KB)
|
||||||
|
pub const DEFAULT_BUF_SIZE: usize = if cfg!(target_os = "espidf") { 512 } else { 8 * 1024 };
|
||||||
|
|
|
@ -62,7 +62,7 @@ impl io::Write for Stderr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
|
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
|
||||||
|
|
||||||
pub fn is_ebadf(err: &io::Error) -> bool {
|
pub fn is_ebadf(err: &io::Error) -> bool {
|
||||||
// FIXME: Rust normally maps Unix EBADF to `Uncategorized`
|
// FIXME: Rust normally maps Unix EBADF to `Uncategorized`
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::fs::OpenOptions;
|
||||||
use crate::io;
|
use crate::io;
|
||||||
use crate::io::{BufRead, Read, Result, Seek, SeekFrom, Write};
|
use crate::io::{BufRead, Read, Result, Seek, SeekFrom, Write};
|
||||||
use crate::os::unix::io::AsRawFd;
|
use crate::os::unix::io::AsRawFd;
|
||||||
use crate::sys_common::io::test::tmpdir;
|
use crate::test_helpers::tmpdir;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn copy_specialization() -> Result<()> {
|
fn copy_specialization() -> Result<()> {
|
||||||
|
|
|
@ -92,7 +92,7 @@ pub fn is_ebadf(err: &io::Error) -> bool {
|
||||||
err.raw_os_error() == Some(libc::EBADF as i32)
|
err.raw_os_error() == Some(libc::EBADF as i32)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
|
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
|
||||||
|
|
||||||
pub fn panic_output() -> Option<impl io::Write> {
|
pub fn panic_output() -> Option<impl io::Write> {
|
||||||
Some(Stderr::new())
|
Some(Stderr::new())
|
||||||
|
|
|
@ -101,7 +101,7 @@ impl io::Write for Stderr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
|
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
|
||||||
|
|
||||||
pub fn is_ebadf(err: &io::Error) -> bool {
|
pub fn is_ebadf(err: &io::Error) -> bool {
|
||||||
err.raw_os_error() == Some(wasi::ERRNO_BADF.raw().into())
|
err.raw_os_error() == Some(wasi::ERRNO_BADF.raw().into())
|
||||||
|
|
|
@ -158,7 +158,7 @@ fn windows_exe_resolver() {
|
||||||
use super::resolve_exe;
|
use super::resolve_exe;
|
||||||
use crate::io;
|
use crate::io;
|
||||||
use crate::sys::fs::symlink;
|
use crate::sys::fs::symlink;
|
||||||
use crate::sys_common::io::test::tmpdir;
|
use crate::test_helpers::tmpdir;
|
||||||
|
|
||||||
let env_paths = || env::var_os("PATH");
|
let env_paths = || env::var_os("PATH");
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ impl io::Write for Stderr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
|
pub const STDIN_BUF_SIZE: usize = crate::sys::io::DEFAULT_BUF_SIZE;
|
||||||
|
|
||||||
pub fn is_ebadf(_err: &io::Error) -> bool {
|
pub fn is_ebadf(_err: &io::Error) -> bool {
|
||||||
true
|
true
|
||||||
|
|
|
@ -1,49 +0,0 @@
|
||||||
// Bare metal platforms usually have very small amounts of RAM
|
|
||||||
// (in the order of hundreds of KB)
|
|
||||||
pub const DEFAULT_BUF_SIZE: usize = if cfg!(target_os = "espidf") { 512 } else { 8 * 1024 };
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
#[allow(dead_code)] // not used on emscripten and wasi
|
|
||||||
pub mod test {
|
|
||||||
use rand::RngCore;
|
|
||||||
|
|
||||||
use crate::path::{Path, PathBuf};
|
|
||||||
use crate::{env, fs, thread};
|
|
||||||
|
|
||||||
pub struct TempDir(PathBuf);
|
|
||||||
|
|
||||||
impl TempDir {
|
|
||||||
pub fn join(&self, path: &str) -> PathBuf {
|
|
||||||
let TempDir(ref p) = *self;
|
|
||||||
p.join(path)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn path(&self) -> &Path {
|
|
||||||
let TempDir(ref p) = *self;
|
|
||||||
p
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for TempDir {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
// Gee, seeing how we're testing the fs module I sure hope that we
|
|
||||||
// at least implement this correctly!
|
|
||||||
let TempDir(ref p) = *self;
|
|
||||||
let result = fs::remove_dir_all(p);
|
|
||||||
// Avoid panicking while panicking as this causes the process to
|
|
||||||
// immediately abort, without displaying test results.
|
|
||||||
if !thread::panicking() {
|
|
||||||
result.unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[track_caller] // for `test_rng`
|
|
||||||
pub fn tmpdir() -> TempDir {
|
|
||||||
let p = env::temp_dir();
|
|
||||||
let mut r = crate::test_helpers::test_rng();
|
|
||||||
let ret = p.join(&format!("rust-{}", r.next_u32()));
|
|
||||||
fs::create_dir(&ret).unwrap();
|
|
||||||
TempDir(ret)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -21,7 +21,6 @@
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
pub mod fs;
|
pub mod fs;
|
||||||
pub mod io;
|
|
||||||
pub mod process;
|
pub mod process;
|
||||||
pub mod wstr;
|
pub mod wstr;
|
||||||
pub mod wtf8;
|
pub mod wtf8;
|
||||||
|
|
65
library/std/src/test_helpers.rs
Normal file
65
library/std/src/test_helpers.rs
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
use rand::{RngCore, SeedableRng};
|
||||||
|
|
||||||
|
use crate::hash::{BuildHasher, Hash, Hasher, RandomState};
|
||||||
|
use crate::panic::Location;
|
||||||
|
use crate::path::{Path, PathBuf};
|
||||||
|
use crate::{env, fs, thread};
|
||||||
|
|
||||||
|
/// Test-only replacement for `rand::thread_rng()`, which is unusable for
|
||||||
|
/// us, as we want to allow running stdlib tests on tier-3 targets which may
|
||||||
|
/// not have `getrandom` support.
|
||||||
|
///
|
||||||
|
/// Does a bit of a song and dance to ensure that the seed is different on
|
||||||
|
/// each call (as some tests sadly rely on this), but doesn't try that hard.
|
||||||
|
///
|
||||||
|
/// This is duplicated in the `core`, `alloc` test suites (as well as
|
||||||
|
/// `std`'s integration tests), but figuring out a mechanism to share these
|
||||||
|
/// seems far more painful than copy-pasting a 7 line function a couple
|
||||||
|
/// times, given that even under a perma-unstable feature, I don't think we
|
||||||
|
/// want to expose types from `rand` from `std`.
|
||||||
|
#[track_caller]
|
||||||
|
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
|
||||||
|
let mut hasher = RandomState::new().build_hasher();
|
||||||
|
Location::caller().hash(&mut hasher);
|
||||||
|
let hc64 = hasher.finish();
|
||||||
|
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
|
||||||
|
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
|
||||||
|
SeedableRng::from_seed(seed)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TempDir(PathBuf);
|
||||||
|
|
||||||
|
impl TempDir {
|
||||||
|
pub fn join(&self, path: &str) -> PathBuf {
|
||||||
|
let TempDir(ref p) = *self;
|
||||||
|
p.join(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn path(&self) -> &Path {
|
||||||
|
let TempDir(ref p) = *self;
|
||||||
|
p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TempDir {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
// Gee, seeing how we're testing the fs module I sure hope that we
|
||||||
|
// at least implement this correctly!
|
||||||
|
let TempDir(ref p) = *self;
|
||||||
|
let result = fs::remove_dir_all(p);
|
||||||
|
// Avoid panicking while panicking as this causes the process to
|
||||||
|
// immediately abort, without displaying test results.
|
||||||
|
if !thread::panicking() {
|
||||||
|
result.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[track_caller] // for `test_rng`
|
||||||
|
pub fn tmpdir() -> TempDir {
|
||||||
|
let p = env::temp_dir();
|
||||||
|
let mut r = test_rng();
|
||||||
|
let ret = p.join(&format!("rust-{}", r.next_u32()));
|
||||||
|
fs::create_dir(&ret).unwrap();
|
||||||
|
TempDir(ret)
|
||||||
|
}
|
|
@ -18,7 +18,7 @@ pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
|
||||||
rand::SeedableRng::from_seed(seed)
|
rand::SeedableRng::from_seed(seed)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copied from std::sys_common::io
|
// Copied from std::test_helpers
|
||||||
pub(crate) struct TempDir(PathBuf);
|
pub(crate) struct TempDir(PathBuf);
|
||||||
|
|
||||||
impl TempDir {
|
impl TempDir {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue