Skip to content

Commit 27a8af1

Browse files
committed
feat: Make property_string_list take an IntoIterator as input
Also add a way to skip the CString allocation. Note that this can break existing code by making type inference ambiguous, thus this also bumps the crate minor version. Signed-off-by: Ellen Εμιλία Άννα Zscheile <fogti+devel@ytrizja.de>
1 parent ef5bd73 commit 27a8af1

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vm-fdt"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
description = "Crate for writing Flattened Devicetree blobs"
55
authors = ["The Chromium OS Authors"]
66
license = "Apache-2.0 OR BSD-3-Clause"

src/writer.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
use alloc::collections::BTreeMap;
88
use alloc::ffi::CString;
9-
use alloc::string::String;
109
use alloc::vec::Vec;
1110
use core::cmp::{Ord, Ordering};
1211
use core::convert::TryInto;
@@ -452,7 +451,11 @@ impl FdtWriter {
452451
}
453452

454453
/// Write a stringlist property.
455-
pub fn property_string_list(&mut self, name: &str, values: Vec<String>) -> Result<()> {
454+
pub fn property_string_list<Iter, T>(&mut self, name: &str, values: Iter) -> Result<()>
455+
where
456+
Iter: IntoIterator<Item = T>,
457+
T: Into<Vec<u8>>,
458+
{
456459
let mut bytes = Vec::new();
457460
for s in values {
458461
let cstr = CString::new(s).map_err(|_| Error::InvalidString)?;
@@ -461,6 +464,18 @@ impl FdtWriter {
461464
self.property(name, &bytes)
462465
}
463466

467+
/// Write a stringlist property.
468+
pub fn property_cstring_list<Iter>(&mut self, name: &str, values: Iter) -> Result<()>
469+
where
470+
Iter: IntoIterator<Item = CString>,
471+
{
472+
let mut bytes = Vec::new();
473+
for cstr in values {
474+
bytes.extend_from_slice(cstr.to_bytes_with_nul());
475+
}
476+
self.property(name, &bytes)
477+
}
478+
464479
/// Write a 32-bit unsigned integer property.
465480
pub fn property_u32(&mut self, name: &str, val: u32) -> Result<()> {
466481
self.property(name, &val.to_be_bytes())
@@ -706,7 +721,9 @@ mod tests {
706721
fdt.property_u32("u32", 0x12345678).unwrap();
707722
fdt.property_u64("u64", 0x1234567887654321).unwrap();
708723
fdt.property_string("str", "hello").unwrap();
709-
fdt.property_string_list("strlst", vec!["hi".into(), "bye".into()])
724+
fdt.property_string_list("strlst", vec!["hi", "bye"])
725+
.unwrap();
726+
fdt.property_cstring_list("strlst2", vec![CString::new("hi").unwrap(), CString::new("bye").unwrap()])
710727
.unwrap();
711728
fdt.property_array_u32("arru32", &[0x12345678, 0xAABBCCDD])
712729
.unwrap();
@@ -716,14 +733,14 @@ mod tests {
716733
let actual_fdt = fdt.finish().unwrap();
717734
let expected_fdt = vec![
718735
0xd0, 0x0d, 0xfe, 0xed, // 0000: magic (0xd00dfeed)
719-
0x00, 0x00, 0x00, 0xee, // 0004: totalsize (0xEE)
736+
0x00, 0x00, 0x01, 0x0a, // 0004: totalsize (0x10A)
720737
0x00, 0x00, 0x00, 0x38, // 0008: off_dt_struct (0x38)
721-
0x00, 0x00, 0x00, 0xc8, // 000C: off_dt_strings (0xC8)
738+
0x00, 0x00, 0x00, 0xdc, // 000C: off_dt_strings (0xDC)
722739
0x00, 0x00, 0x00, 0x28, // 0010: off_mem_rsvmap (0x28)
723740
0x00, 0x00, 0x00, 0x11, // 0014: version (0x11 = 17)
724741
0x00, 0x00, 0x00, 0x10, // 0018: last_comp_version (0x10 = 16)
725742
0x00, 0x00, 0x00, 0x00, // 001C: boot_cpuid_phys (0)
726-
0x00, 0x00, 0x00, 0x26, // 0020: size_dt_strings (0x26)
743+
0x00, 0x00, 0x00, 0x2e, // 0020: size_dt_strings (0x2E)
727744
0x00, 0x00, 0x00, 0x90, // 0024: size_dt_struct (0x90)
728745
0x00, 0x00, 0x00, 0x00, // 0028: rsvmap terminator (address = 0 high)
729746
0x00, 0x00, 0x00, 0x00, // 002C: rsvmap terminator (address = 0 low)
@@ -956,7 +973,7 @@ mod tests {
956973
#[test]
957974
fn invalid_prop_string_list_value_nul() {
958975
let mut fdt = FdtWriter::new().unwrap();
959-
let strs = vec!["test".into(), "abc\0def".into()];
976+
let strs = vec!["test", "abc\0def"];
960977
assert_eq!(
961978
fdt.property_string_list("mystr", strs).unwrap_err(),
962979
Error::InvalidString

0 commit comments

Comments
 (0)