Skip to content

Commit bd5047c

Browse files
committed
Take file paths by value to avoid internal cloning
Also: Store file paths as `Cow<'static, str>` internally to avoid cloning for const paths.
1 parent 692d39f commit bd5047c

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/lib.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod fat;
2525
mod file_data_source;
2626

2727
use std::{
28+
borrow::Cow,
2829
collections::BTreeMap,
2930
fs,
3031
path::{Path, PathBuf},
@@ -45,12 +46,12 @@ const CONFIG_FILE_NAME: &str = "boot.json";
4546
///
4647
/// It can currently create `MBR` (BIOS), `GPT` (UEFI), and `TFTP` (UEFI) images.
4748
pub struct DiskImageBuilder {
48-
files: BTreeMap<String, FileDataSource>,
49+
files: BTreeMap<Cow<'static, str>, FileDataSource>,
4950
}
5051

5152
impl DiskImageBuilder {
5253
/// Create a new instance of DiskImageBuilder, with the specified kernel.
53-
pub fn new(kernel: &Path) -> Self {
54+
pub fn new(kernel: PathBuf) -> Self {
5455
let mut obj = Self::empty();
5556
obj.set_kernel(kernel);
5657
obj
@@ -64,13 +65,19 @@ impl DiskImageBuilder {
6465
}
6566

6667
/// Add or replace a kernel to be included in the final image.
67-
pub fn set_kernel(&mut self, path: &Path) -> &mut Self {
68-
self.set_file_source(KERNEL_FILE_NAME, FileDataSource::File(path.to_path_buf()))
68+
pub fn set_kernel(&mut self, path: PathBuf) -> &mut Self {
69+
self.set_file_source(
70+
KERNEL_FILE_NAME.into(),
71+
FileDataSource::File(path.to_path_buf()),
72+
)
6973
}
7074

7175
/// Add or replace a ramdisk to be included in the final image.
72-
pub fn set_ramdisk(&mut self, path: &Path) -> &mut Self {
73-
self.set_file_source(RAMDISK_FILE_NAME, FileDataSource::File(path.to_path_buf()))
76+
pub fn set_ramdisk(&mut self, path: PathBuf) -> &mut Self {
77+
self.set_file_source(
78+
RAMDISK_FILE_NAME.into(),
79+
FileDataSource::File(path.to_path_buf()),
80+
)
7481
}
7582

7683
/// Configures the runtime behavior of the bootloader.
@@ -83,16 +90,16 @@ impl DiskImageBuilder {
8390
///
8491
/// Note that the bootloader only loads the kernel and ramdisk files into memory on boot.
8592
/// Other files need to be loaded manually by the kernel.
86-
pub fn set_file_contents(&mut self, destination: &str, data: Vec<u8>) -> &mut Self {
87-
self.set_file_source(destination, FileDataSource::Data(data))
93+
pub fn set_file_contents(&mut self, destination: String, data: Vec<u8>) -> &mut Self {
94+
self.set_file_source(destination.into(), FileDataSource::Data(data))
8895
}
8996

9097
/// Add a file with the specified source file to the disk image
9198
///
9299
/// Note that the bootloader only loads the kernel and ramdisk files into memory on boot.
93100
/// Other files need to be loaded manually by the kernel.
94101
pub fn set_file(&mut self, destination: &str, file_path: PathBuf) -> &mut Self {
95-
self.set_file_source(destination, FileDataSource::File(file_path))
102+
self.set_file_source(destination.into(), FileDataSource::File(file_path))
96103
}
97104

98105
#[cfg(feature = "bios")]
@@ -156,6 +163,8 @@ impl DiskImageBuilder {
156163
#[cfg(feature = "uefi")]
157164
/// Create a folder containing the needed files for UEFI TFTP/PXE booting.
158165
pub fn create_uefi_tftp_folder(&self, tftp_path: &Path) -> anyhow::Result<()> {
166+
use std::ops::Deref;
167+
159168
const UEFI_TFTP_BOOT_FILENAME: &str = "bootloader";
160169
let bootloader_path = Path::new(env!("UEFI_BOOTLOADER_PATH"));
161170
fs::create_dir_all(tftp_path)
@@ -171,7 +180,7 @@ impl DiskImageBuilder {
171180
})?;
172181

173182
for f in &self.files {
174-
let to = tftp_path.join(f.0.clone());
183+
let to = tftp_path.join(f.0.deref());
175184

176185
let mut new_file = fs::OpenOptions::new()
177186
.read(true)
@@ -187,8 +196,11 @@ impl DiskImageBuilder {
187196
}
188197

189198
/// Add a file source to the disk image
190-
fn set_file_source(&mut self, destination: &str, source: FileDataSource) -> &mut Self {
191-
let destination = destination.to_string();
199+
fn set_file_source(
200+
&mut self,
201+
destination: Cow<'static, str>,
202+
source: FileDataSource,
203+
) -> &mut Self {
192204
self.files.insert(destination, source);
193205
self
194206
}

0 commit comments

Comments
 (0)