1
Fork 0
mirror of https://github.com/RGBCube/embd-rs synced 2025-07-27 05:27:44 +00:00

Don't allocate PathBuf uselessly

This commit is contained in:
RGBCube 2024-01-04 23:24:49 +03:00
parent a6eba8386c
commit d7f67fcf4c
No known key found for this signature in database
2 changed files with 13 additions and 14 deletions

View file

@ -3,10 +3,7 @@
use std::{ use std::{
borrow::Cow, borrow::Cow,
fs, fs,
path::{ path::Path,
Path,
PathBuf,
},
}; };
#[doc(hidden)] #[doc(hidden)]
@ -97,7 +94,7 @@ pub struct Dir {
/// The entries the directory houses. /// The entries the directory houses.
pub children: Vec<DirEntry>, pub children: Vec<DirEntry>,
/// The absolute path of the directory. /// The absolute path of the directory.
pub path: PathBuf, pub path: Cow<'static, Path>,
} }
impl Dir { impl Dir {
@ -122,10 +119,10 @@ pub struct File {
/// The content of the file in bytes. /// The content of the file in bytes.
pub content: Cow<'static, [u8]>, pub content: Cow<'static, [u8]>,
/// The absolute path of the file. /// The absolute path of the file.
pub path: PathBuf, pub path: Cow<'static, Path>,
} }
fn read_dir(path: &PathBuf) -> Vec<DirEntry> { fn read_dir(path: &Path) -> Vec<DirEntry> {
let mut entries = Vec::new(); let mut entries = Vec::new();
for entry in fs::read_dir(path).expect("Failed to list directory contents") { for entry in fs::read_dir(path).expect("Failed to list directory contents") {
@ -133,13 +130,15 @@ fn read_dir(path: &PathBuf) -> Vec<DirEntry> {
let filetype = entry.file_type().expect("Failed to read entry filetype"); let filetype = entry.file_type().expect("Failed to read entry filetype");
let path = entry let path = Cow::Owned::<'static, Path>(
.path() entry
.canonicalize() .path()
.expect("Failed to canonicalize path"); .canonicalize()
.expect("Failed to canonicalize path"),
);
if filetype.is_dir() { if filetype.is_dir() {
let children = read_dir(&path); let children = read_dir(path.as_ref());
entries.push(DirEntry::Dir(Dir { children, path })) entries.push(DirEntry::Dir(Dir { children, path }))
} else if filetype.is_file() { } else if filetype.is_file() {
@ -167,7 +166,7 @@ pub fn __dir_runtime(neighbor: &str, path: &str) -> Dir {
Dir { Dir {
children, children,
path: directory, path: Cow::Owned(directory),
} }
} }

View file

@ -27,7 +27,7 @@ impl ToTokens for PathBuf2 {
.expect("Failed to get the string representation of PathBuf"); .expect("Failed to get the string representation of PathBuf");
tokens.extend(quote! { tokens.extend(quote! {
::std::path::PathBuf::from(#raw) ::std::borrow::Cow::Borrowed(::std::path::Path::new(#raw))
}); });
} }
} }