From 6c33b6177d27a2785d0979dcad6532c7178fcbf0 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Thu, 4 Jan 2024 22:13:41 +0300 Subject: [PATCH] Use Cow. Moo --- embed/src/lib.rs | 5 +++-- embed_macros/src/lib.rs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/embed/src/lib.rs b/embed/src/lib.rs index 98f5859..f7dd2af 100644 --- a/embed/src/lib.rs +++ b/embed/src/lib.rs @@ -1,6 +1,7 @@ #![cfg(procmacro2_semver_exempt)] use std::{ + borrow::Cow, fs, path::{ Path, @@ -119,7 +120,7 @@ impl Dir { #[derive(Debug, Clone)] pub struct File { /// The content of the file in bytes. - pub content: Vec, + pub content: Cow<'static, [u8]>, /// The absolute path of the file. pub path: PathBuf, } @@ -149,7 +150,7 @@ fn read_dir(path: &PathBuf) -> Vec { entries.push(DirEntry::Dir(Dir { children, path })) } else if filetype.is_file() { - let content = fs::read(&path).expect("Failed to read file contents"); + let content = Cow::Owned(fs::read(&path).expect("Failed to read file contents")); entries.push(DirEntry::File(File { content, path })) } diff --git a/embed_macros/src/lib.rs b/embed_macros/src/lib.rs index f549298..ece7181 100644 --- a/embed_macros/src/lib.rs +++ b/embed_macros/src/lib.rs @@ -95,7 +95,7 @@ fn read_dir(directory: &PathBuf) -> Vec { } else if filetype.is_file() { entries.push(quote! { ::embed::DirEntry::File(::embed::File { - content: include_bytes!(#path_str).to_vec(), + content: ::std::borrow::Cow::Borrowed(include_bytes!(#path_str)), path: #path, }) });