/rust/registry/src/index.crates.io-6f17d22bba15001f/tempfile-3.3.0/src/util.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use fastrand; |
2 | | use std::ffi::{OsStr, OsString}; |
3 | | use std::path::{Path, PathBuf}; |
4 | | use std::{io, iter::repeat_with}; |
5 | | |
6 | | use crate::error::IoResultExt; |
7 | | |
8 | 3.50k | fn tmpname(prefix: &OsStr, suffix: &OsStr, rand_len: usize) -> OsString { |
9 | 3.50k | let mut buf = OsString::with_capacity(prefix.len() + suffix.len() + rand_len); |
10 | 3.50k | buf.push(prefix); |
11 | 3.50k | let mut char_buf = [0u8; 4]; |
12 | 21.0k | for c in repeat_with(fastrand::alphanumeric).take(rand_len) { |
13 | 21.0k | buf.push(c.encode_utf8(&mut char_buf)); |
14 | 21.0k | } |
15 | 3.50k | buf.push(suffix); |
16 | 3.50k | buf |
17 | 3.50k | } |
18 | | |
19 | 3.50k | pub fn create_helper<F, R>( |
20 | 3.50k | base: &Path, |
21 | 3.50k | prefix: &OsStr, |
22 | 3.50k | suffix: &OsStr, |
23 | 3.50k | random_len: usize, |
24 | 3.50k | f: F, |
25 | 3.50k | ) -> io::Result<R> |
26 | 3.50k | where |
27 | 3.50k | F: Fn(PathBuf) -> io::Result<R>, |
28 | 3.50k | { |
29 | 3.50k | let num_retries = if random_len != 0 { |
30 | 3.50k | crate::NUM_RETRIES |
31 | | } else { |
32 | 0 | 1 |
33 | | }; |
34 | | |
35 | 3.50k | for _ in 0..num_retries { |
36 | 3.50k | let path = base.join(tmpname(prefix, suffix, random_len)); |
37 | 3.50k | return match f(path) { |
38 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => continue, |
39 | 3.50k | res => res, |
40 | | }; |
41 | | } |
42 | | |
43 | 0 | Err(io::Error::new( |
44 | 0 | io::ErrorKind::AlreadyExists, |
45 | 0 | "too many temporary files exist", |
46 | 0 | )) |
47 | 0 | .with_err_path(|| base) Unexecuted instantiation: tempfile::util::create_helper::<<tempfile::Builder>::tempfile_in<&std::path::PathBuf>::{closure#0}, tempfile::file::NamedTempFile>::{closure#0} Unexecuted instantiation: tempfile::util::create_helper::<tempfile::file::imp::unix::create_unix::{closure#0}, std::fs::File>::{closure#0} Unexecuted instantiation: tempfile::util::create_helper::<tempfile::dir::create, tempfile::dir::TempDir>::{closure#0} |
48 | 3.50k | } Unexecuted instantiation: tempfile::util::create_helper::<<tempfile::Builder>::tempfile_in<&std::path::PathBuf>::{closure#0}, tempfile::file::NamedTempFile> tempfile::util::create_helper::<tempfile::file::imp::unix::create_unix::{closure#0}, std::fs::File> Line | Count | Source | 19 | 3.50k | pub fn create_helper<F, R>( | 20 | 3.50k | base: &Path, | 21 | 3.50k | prefix: &OsStr, | 22 | 3.50k | suffix: &OsStr, | 23 | 3.50k | random_len: usize, | 24 | 3.50k | f: F, | 25 | 3.50k | ) -> io::Result<R> | 26 | 3.50k | where | 27 | 3.50k | F: Fn(PathBuf) -> io::Result<R>, | 28 | 3.50k | { | 29 | 3.50k | let num_retries = if random_len != 0 { | 30 | 3.50k | crate::NUM_RETRIES | 31 | | } else { | 32 | 0 | 1 | 33 | | }; | 34 | | | 35 | 3.50k | for _ in 0..num_retries { | 36 | 3.50k | let path = base.join(tmpname(prefix, suffix, random_len)); | 37 | 3.50k | return match f(path) { | 38 | 0 | Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => continue, | 39 | 3.50k | res => res, | 40 | | }; | 41 | | } | 42 | | | 43 | 0 | Err(io::Error::new( | 44 | 0 | io::ErrorKind::AlreadyExists, | 45 | 0 | "too many temporary files exist", | 46 | 0 | )) | 47 | 0 | .with_err_path(|| base) | 48 | 3.50k | } |
Unexecuted instantiation: tempfile::util::create_helper::<tempfile::dir::create, tempfile::dir::TempDir> |