]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys_common/io.rs
Rollup merge of #98530 - davidkna:known-bug-ref, r=Mark-Simulacrum
[rust.git] / library / std / src / sys_common / io.rs
1 // Bare metal platforms usually have very small amounts of RAM
2 // (in the order of hundreds of KB)
3 pub const DEFAULT_BUF_SIZE: usize = if cfg!(target_os = "espidf") { 512 } else { 8 * 1024 };
4
5 #[cfg(test)]
6 #[allow(dead_code)] // not used on emscripten
7 pub mod test {
8     use crate::env;
9     use crate::fs;
10     use crate::path::{Path, PathBuf};
11     use crate::thread;
12     use rand::RngCore;
13
14     pub struct TempDir(PathBuf);
15
16     impl TempDir {
17         pub fn join(&self, path: &str) -> PathBuf {
18             let TempDir(ref p) = *self;
19             p.join(path)
20         }
21
22         pub fn path(&self) -> &Path {
23             let TempDir(ref p) = *self;
24             p
25         }
26     }
27
28     impl Drop for TempDir {
29         fn drop(&mut self) {
30             // Gee, seeing how we're testing the fs module I sure hope that we
31             // at least implement this correctly!
32             let TempDir(ref p) = *self;
33             let result = fs::remove_dir_all(p);
34             // Avoid panicking while panicking as this causes the process to
35             // immediately abort, without displaying test results.
36             if !thread::panicking() {
37                 result.unwrap();
38             }
39         }
40     }
41
42     pub fn tmpdir() -> TempDir {
43         let p = env::temp_dir();
44         let mut r = rand::thread_rng();
45         let ret = p.join(&format!("rust-{}", r.next_u32()));
46         fs::create_dir(&ret).unwrap();
47         TempDir(ret)
48     }
49 }