Skip to content

Commit 4712cd2

Browse files
authored
Merge pull request #2091 from hermit-os/to_owned
refactor: consistently use `to_owned` over `to_string` for `str`
2 parents cd10098 + b309143 commit 4712cd2

File tree

10 files changed

+69
-67
lines changed

10 files changed

+69
-67
lines changed

src/drivers/fs/virtio_fs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use alloc::borrow::ToOwned;
12
use alloc::boxed::Box;
2-
use alloc::string::{String, ToString};
3+
use alloc::string::String;
34
use alloc::vec::Vec;
45
use core::mem::MaybeUninit;
56
use core::str;
@@ -242,7 +243,7 @@ impl FuseInterface for VirtioFsDriver {
242243
let tag = self.dev_cfg.raw.as_ptr().tag().read();
243244
let tag = str::from_utf8(&tag).unwrap();
244245
let tag = tag.split('\0').next().unwrap();
245-
tag.to_string()
246+
tag.to_owned()
246247
}
247248
}
248249

src/env.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Central parsing of the command-line parameters.
22
3-
use alloc::string::{String, ToString};
3+
use alloc::borrow::ToOwned;
4+
use alloc::string::String;
45
use alloc::vec::Vec;
56
use core::{ptr, str};
67

@@ -99,7 +100,7 @@ impl Default for Cli {
99100
while let Some(word) = words.next() {
100101
if word.as_str().starts_with("virtio_mmio.device=") {
101102
let v: Vec<&str> = word.as_str().split('=').collect();
102-
mmio.push(v[1].to_string());
103+
mmio.push(v[1].to_owned());
103104
continue;
104105
}
105106

@@ -135,7 +136,7 @@ impl Default for Cli {
135136
error!("could not parse bootarg: {word}");
136137
continue;
137138
};
138-
env_vars.insert(key.to_string(), value.to_string());
139+
env_vars.insert(key.to_owned(), value.to_owned());
139140
}
140141
_ => error!("could not parse bootarg: {word}"),
141142
}

src/fs/fuse.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use async_trait::async_trait;
1616
use embedded_io::{ErrorType, Read, Write};
1717
use fuse_abi::linux::*;
1818

19-
use crate::alloc::string::ToString;
2019
#[cfg(not(feature = "pci"))]
2120
use crate::arch::kernel::mmio::get_filesystem_driver;
2221
#[cfg(feature = "pci")]
@@ -978,9 +977,9 @@ impl FuseDirectoryHandle {
978977
impl ObjectInterface for FuseDirectoryHandle {
979978
async fn getdents(&self, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {
980979
let path: CString = if let Some(name) = &self.name {
981-
CString::new("/".to_string() + name).unwrap()
980+
CString::new("/".to_owned() + name).unwrap()
982981
} else {
983-
CString::new("/".to_string()).unwrap()
982+
CString::new("/").unwrap()
984983
};
985984

986985
debug!("FUSE opendir: {path:#?}");
@@ -1214,7 +1213,7 @@ impl VfsNode for FuseDirectory {
12141213
)
12151214
};
12161215
entries.push(DirectoryEntry::new(unsafe {
1217-
core::str::from_utf8_unchecked(name).to_string()
1216+
core::str::from_utf8_unchecked(name).to_owned()
12181217
}));
12191218
}
12201219

@@ -1461,7 +1460,7 @@ pub(crate) fn init() {
14611460
dirent.namelen.try_into().unwrap(),
14621461
)
14631462
};
1464-
entries.push(unsafe { core::str::from_utf8_unchecked(name).to_string() });
1463+
entries.push(unsafe { core::str::from_utf8_unchecked(name).to_owned() });
14651464
}
14661465

14671466
let (cmd, rsp_payload_len) = ops::Release::create(fuse_nid, fuse_fh);
@@ -1481,7 +1480,7 @@ pub(crate) fn init() {
14811480
);
14821481

14831482
for i in entries {
1484-
let i_cstr = CString::new(i.clone()).unwrap();
1483+
let i_cstr = CString::new(i.as_str()).unwrap();
14851484
let (cmd, rsp_payload_len) = ops::Lookup::create(i_cstr);
14861485
let rsp = get_filesystem_driver()
14871486
.unwrap()

src/fs/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ pub(crate) mod fuse;
33
mod mem;
44
mod uhyve;
55

6+
use alloc::borrow::ToOwned;
67
use alloc::boxed::Box;
7-
use alloc::string::{String, ToString};
8+
use alloc::string::String;
89
use alloc::sync::Arc;
910
use alloc::vec::Vec;
1011
use core::ops::BitAnd;
@@ -360,7 +361,7 @@ pub(crate) fn init() {
360361
}
361362

362363
let mut cwd = WORKING_DIRECTORY.lock();
363-
*cwd = Some("/tmp".to_string());
364+
*cwd = Some("/tmp".to_owned());
364365
drop(cwd);
365366

366367
#[cfg(all(feature = "fuse", feature = "pci"))]
@@ -501,7 +502,7 @@ pub fn set_cwd(cwd: &str) -> io::Result<()> {
501502

502503
let mut working_dir = WORKING_DIRECTORY.lock();
503504
if cwd.starts_with("/") {
504-
*working_dir = Some(cwd.to_string());
505+
*working_dir = Some(cwd.to_owned());
505506
} else {
506507
let Some(working_dir) = working_dir.as_mut() else {
507508
return Err(Errno::Badf);
@@ -589,7 +590,7 @@ impl File {
589590

590591
Ok(File {
591592
fd,
592-
path: path.to_string(),
593+
path: path.to_owned(),
593594
})
594595
}
595596

@@ -603,7 +604,7 @@ impl File {
603604

604605
Ok(File {
605606
fd,
606-
path: path.to_string(),
607+
path: path.to_owned(),
607608
})
608609
}
609610

src/fs/uhyve.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::borrow::ToOwned;
22
use alloc::boxed::Box;
33
use alloc::ffi::CString;
4-
use alloc::string::{String, ToString};
4+
use alloc::string::String;
55
use alloc::sync::Arc;
66
use alloc::vec::Vec;
77

@@ -264,7 +264,7 @@ pub(crate) fn init() {
264264
}
265265
} else {
266266
// No FDT -> Uhyve legacy mounting (to /root)
267-
let mount_point = hermit_var_or!("UHYVE_MOUNT", "/root").to_string();
267+
let mount_point = hermit_var_or!("UHYVE_MOUNT", "/root").to_owned();
268268
info!("Mounting uhyve filesystem at {mount_point}");
269269
fs::FILESYSTEM
270270
.get()

xtask/src/cargo_build.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ impl CargoBuild {
2525
args.extend(
2626
self.no_default_features_args()
2727
.iter()
28-
.map(|s| s.to_string()),
28+
.map(|&s| s.to_owned()),
2929
);
30-
args.extend(self.features_args().map(|s| s.to_string()));
31-
args.extend(self.release_args().iter().map(|s| s.to_string()));
30+
args.extend(self.features_args().map(|s| s.to_owned()));
31+
args.extend(self.release_args().iter().map(|&s| s.to_owned()));
3232
if let Some(profile) = &self.artifact.profile {
33-
args.push("--profile".to_string());
34-
args.push(profile.to_string());
33+
args.push("--profile".to_owned());
34+
args.push(profile.to_owned());
3535
}
3636
args
3737
}
3838

3939
pub fn target_dir_args(&self) -> Vec<String> {
4040
if self.artifact.target_dir.is_some() {
4141
vec![
42-
"--target-dir".to_string(),
42+
"--target-dir".to_owned(),
4343
self.artifact
4444
.target_dir()
4545
.into_os_string()

xtask/src/ci/firecracker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Firecracker {
2828
let config_path = Path::new("firecracker_vm_config.json");
2929
sh.write_file(config_path, config)?;
3030

31-
let firecracker = env::var("FIRECRACKER").unwrap_or_else(|_| "firecracker".to_string());
31+
let firecracker = env::var("FIRECRACKER").unwrap_or_else(|_| "firecracker".to_owned());
3232
let program = if self.sudo {
3333
"sudo"
3434
} else {

xtask/src/ci/qemu.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Qemu {
111111
.args(self.machine_args(arch))
112112
.args(self.cpu_args(arch))
113113
.args(&["-smp", &effective_smp.to_string()])
114-
.args(&["-m".to_string(), format!("{memory}M")])
114+
.args(&["-m".to_owned(), format!("{memory}M")])
115115
.args(&["-global", "virtio-mmio.force-legacy=off"])
116116
.args(self.device_args(memory))
117117
.args(self.cmdline_args(image_name));
@@ -199,22 +199,22 @@ impl Qemu {
199199
let vars = prebuilt.get_file(Arch::X64, FileType::Vars);
200200

201201
vec![
202-
"-drive".to_string(),
202+
"-drive".to_owned(),
203203
format!("if=pflash,format=raw,readonly=on,file={}", code.display()),
204-
"-drive".to_string(),
204+
"-drive".to_owned(),
205205
format!("if=pflash,format=raw,readonly=on,file={}", vars.display()),
206-
"-drive".to_string(),
207-
"format=raw,file=fat:rw:target/esp".to_string(),
206+
"-drive".to_owned(),
207+
"format=raw,file=fat:rw:target/esp".to_owned(),
208208
]
209209
} else {
210-
let mut image_args = vec!["-kernel".to_string(), loader];
210+
let mut image_args = vec!["-kernel".to_owned(), loader];
211211
match arch {
212212
Arch::X86_64 | Arch::Riscv64 => {
213-
image_args.push("-initrd".to_string());
214-
image_args.push(image.to_str().unwrap().to_string());
213+
image_args.push("-initrd".to_owned());
214+
image_args.push(image.to_str().unwrap().to_owned());
215215
}
216216
Arch::Aarch64 | Arch::Aarch64Be => {
217-
image_args.push("-device".to_string());
217+
image_args.push("-device".to_owned());
218218
image_args.push(format!(
219219
"guest-loader,addr=0x48000000,initrd={}",
220220
image.display()
@@ -230,18 +230,18 @@ impl Qemu {
230230
fn machine_args(&self, arch: Arch) -> Vec<String> {
231231
if self.microvm {
232232
vec![
233-
"-M".to_string(),
233+
"-M".to_owned(),
234234
"microvm,x-option-roms=off,pit=off,pic=off,rtc=on,auto-kernel-cmdline=off,acpi=off"
235-
.to_string(),
236-
"-global".to_string(),
237-
"virtio-mmio.force-legacy=off".to_string(),
238-
"-nodefaults".to_string(),
239-
"-no-user-config".to_string(),
235+
.to_owned(),
236+
"-global".to_owned(),
237+
"virtio-mmio.force-legacy=off".to_owned(),
238+
"-nodefaults".to_owned(),
239+
"-no-user-config".to_owned(),
240240
]
241241
} else if self.pci_e {
242242
vec!["-machine".to_owned(), "q35".to_owned()]
243243
} else if arch == Arch::Aarch64 || arch == Arch::Aarch64Be {
244-
vec!["-machine".to_string(), "virt,gic-version=3".to_string()]
244+
vec!["-machine".to_owned(), "virt,gic-version=3".to_owned()]
245245
} else if arch == Arch::Riscv64 {
246246
// CadenceGem requires sifive_u
247247
let machine = if self.devices.contains(&Device::CadenceGem) {
@@ -250,10 +250,10 @@ impl Qemu {
250250
"virt"
251251
};
252252
vec![
253-
"-machine".to_string(),
254-
machine.to_string(),
255-
"-bios".to_string(),
256-
"opensbi-1.7-rv-bin/share/opensbi/lp64/generic/firmware/fw_jump.bin".to_string(),
253+
"-machine".to_owned(),
254+
machine.to_owned(),
255+
"-bios".to_owned(),
256+
"opensbi-1.7-rv-bin/share/opensbi/lp64/generic/firmware/fw_jump.bin".to_owned(),
257257
]
258258
} else {
259259
vec![]
@@ -266,27 +266,27 @@ impl Qemu {
266266
let mut cpu_args = if self.accel {
267267
if cfg!(target_os = "linux") {
268268
vec![
269-
"-enable-kvm".to_string(),
270-
"-cpu".to_string(),
271-
"host".to_string(),
269+
"-enable-kvm".to_owned(),
270+
"-cpu".to_owned(),
271+
"host".to_owned(),
272272
]
273273
} else {
274274
todo!()
275275
}
276276
} else {
277-
vec!["-cpu".to_string(), "Skylake-Client".to_string()]
277+
vec!["-cpu".to_owned(), "Skylake-Client".to_owned()]
278278
};
279-
cpu_args.push("-device".to_string());
280-
cpu_args.push("isa-debug-exit,iobase=0xf4,iosize=0x04".to_string());
279+
cpu_args.push("-device".to_owned());
280+
cpu_args.push("isa-debug-exit,iobase=0xf4,iosize=0x04".to_owned());
281281
cpu_args
282282
}
283283
Arch::Aarch64 | Arch::Aarch64Be => {
284284
let mut cpu_args = if self.accel {
285285
todo!()
286286
} else {
287-
vec!["-cpu".to_string(), "cortex-a72".to_string()]
287+
vec!["-cpu".to_owned(), "cortex-a72".to_owned()]
288288
};
289-
cpu_args.push("-semihosting".to_string());
289+
cpu_args.push("-semihosting".to_owned());
290290
cpu_args
291291
}
292292
Arch::Riscv64 => {
@@ -297,7 +297,7 @@ impl Qemu {
297297
// possibly because it requires sifive_u as the machine.
298298
vec![]
299299
} else {
300-
vec!["-cpu".to_string(), "rv64".to_string()]
300+
vec!["-cpu".to_owned(), "rv64".to_owned()]
301301
}
302302
}
303303
}
@@ -346,15 +346,15 @@ impl Qemu {
346346
.flat_map(|device| match device {
347347
Device::CadenceGem => {
348348
vec![
349-
"-nic".to_string(),
349+
"-nic".to_owned(),
350350
format!("{netdev_options},model=cadence_gem"),
351351
]
352352
}
353353
device @ (Device::Rtl8139 | Device::VirtioNetMmio | Device::VirtioNetPci) => {
354354
let mut netdev_args = vec![
355-
"-netdev".to_string(),
356-
netdev_options.to_string(),
357-
"-device".to_string(),
355+
"-netdev".to_owned(),
356+
netdev_options.to_owned(),
357+
"-device".to_owned(),
358358
];
359359

360360
let mut device_arg = match device {
@@ -363,7 +363,7 @@ impl Qemu {
363363
Device::Rtl8139 => "rtl8139,netdev=net0",
364364
_ => unreachable!(),
365365
}
366-
.to_string();
366+
.to_owned();
367367

368368
if !self.no_default_virtio_features
369369
&& (device == Device::VirtioNetPci || device == Device::VirtioNetMmio)
@@ -382,18 +382,18 @@ impl Qemu {
382382
""
383383
};
384384
vec![
385-
"-chardev".to_string(),
386-
"socket,id=char0,path=./vhostqemu".to_string(),
387-
"-device".to_string(),
385+
"-chardev".to_owned(),
386+
"socket,id=char0,path=./vhostqemu".to_owned(),
387+
"-device".to_owned(),
388388
format!(
389389
"vhost-user-fs-pci,queue-size=1024{default_virtio_features},chardev=char0,tag=root"
390390
),
391-
"-object".to_string(),
391+
"-object".to_owned(),
392392
format!(
393393
"memory-backend-file,id=mem,size={memory}M,mem-path=/dev/shm,share=on"
394394
),
395-
"-numa".to_string(),
396-
"node,memdev=mem".to_string(),
395+
"-numa".to_owned(),
396+
"node,memdev=mem".to_owned(),
397397
]
398398
}
399399
device @ (Device::VirtioConsoleMmio | Device::VirtioConsolePci) => {

xtask/src/ci/rs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl Rs {
5252
}
5353

5454
if self.smp > 1 {
55-
self.cargo_build.features.push("hermit/smp".to_string());
55+
self.cargo_build.features.push("hermit/smp".to_owned());
5656
}
5757

5858
let mut cargo = crate::cargo();

xtask/src/ci/uhyve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Uhyve {
1717
pub fn run(self, image: &Path, smp: usize) -> Result<()> {
1818
let sh = crate::sh()?;
1919

20-
let uhyve = env::var("UHYVE").unwrap_or_else(|_| "uhyve".to_string());
20+
let uhyve = env::var("UHYVE").unwrap_or_else(|_| "uhyve".to_owned());
2121
let program = if self.sudo { "sudo" } else { uhyve.as_str() };
2222
let arg = self.sudo.then_some(uhyve.as_str());
2323
let smp_arg = format!("--cpu-count={smp}");

0 commit comments

Comments
 (0)