ombre: Use RNG in `tempdir` crate
dc2f3f167b6c0624b2c95d18a2b97872a4f517c6
1 parent
df755097
ombre/src/rng.rs
+1 -2
| 99 | 99 | } |
|
| 100 | 100 | ||
| 101 | 101 | /// Generates a random value of type `T` within the given range. |
|
| 102 | 102 | /// Panics if the range is empty. |
|
| 103 | 103 | #[inline] |
|
| 104 | - | pub fn gen<T, R>(&mut self, range: R) -> T |
|
| 104 | + | pub fn gen<T>(&mut self, range: impl RangeBounds<T>) -> T |
|
| 105 | 105 | where |
|
| 106 | 106 | T: Copy + PartialOrd + Sub<Output = T> + Add<Output = T> + Bounded + One + fmt::Debug, |
|
| 107 | - | R: RangeBounds<T>, |
|
| 108 | 107 | Uniform<T>: Distribution<T>, |
|
| 109 | 108 | { |
|
| 110 | 109 | Uniform::from_range(range).sample(self) |
|
| 111 | 110 | } |
|
| 112 | 111 |
ombre/src/tempdir.rs
+4 -6
| 2 | 2 | //! |
|
| 3 | 3 | //! This module provides functionality for creating and managing temporary directories |
|
| 4 | 4 | //! that are automatically cleaned up when they go out of scope. |
|
| 5 | 5 | use std::fs; |
|
| 6 | 6 | use std::path::{Path, PathBuf}; |
|
| 7 | - | use std::time::{SystemTime, UNIX_EPOCH}; |
|
| 8 | 7 | use std::{env, io, process}; |
|
| 9 | 8 | ||
| 9 | + | use crate::rng::Rng; |
|
| 10 | + | ||
| 10 | 11 | /// A temporary directory that is automatically deleted when dropped. |
|
| 11 | 12 | /// |
|
| 12 | 13 | /// The directory is created with a unique name based on the process ID and |
|
| 13 | 14 | /// current timestamp. It is automatically removed when the `TempDir` instance |
|
| 14 | 15 | /// is dropped, including all its contents. |
| 26 | 27 | /// |
|
| 27 | 28 | /// - If the system time cannot be retrieved. |
|
| 28 | 29 | /// - If the temporary directory cannot be created. |
|
| 29 | 30 | /// |
|
| 30 | 31 | pub fn new() -> io::Result<Self> { |
|
| 32 | + | let mut rng = Rng::new(); |
|
| 31 | 33 | let pid = process::id(); |
|
| 32 | - | let nanos = SystemTime::now() |
|
| 33 | - | .duration_since(UNIX_EPOCH) |
|
| 34 | - | .map_err(|e| io::Error::new(io::ErrorKind::Other, e))? |
|
| 35 | - | .as_nanos(); |
|
| 36 | 34 | let base = env::temp_dir(); |
|
| 37 | - | let path = base.join(format!("{pid}.{nanos}")); |
|
| 35 | + | let path = base.join(format!("tmp.{pid}.{}", rng.gen::<u32>(..))); |
|
| 38 | 36 | let _ = fs::create_dir(&path)?; |
|
| 39 | 37 | ||
| 40 | 38 | Ok(TempDir { path }) |
|
| 41 | 39 | } |
|
| 42 | 40 |