Skip to content

Commit 631b7a4

Browse files
committed
property_string and property_string_list accept AsRef<str>
This allows passing both, str and String. Signed-off-by: Jonathan Klimt <jonathan.klimt@eonerc.rwth-aachen.de>
1 parent ef5bd73 commit 631b7a4

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Upcoming
2+
3+
- `fdt.property_string` and `fdt.property_string_list` now accept `String` and `str` parameters.
4+
15
# v0.3.0
26

37
## Added

src/writer.rs

Lines changed: 42 additions & 20 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;
@@ -446,16 +445,20 @@ impl FdtWriter {
446445
}
447446

448447
/// Write a string property.
449-
pub fn property_string(&mut self, name: &str, val: &str) -> Result<()> {
450-
let cstr_value = CString::new(val).map_err(|_| Error::InvalidString)?;
448+
pub fn property_string<S: AsRef<str>>(&mut self, name: &str, val: S) -> Result<()> {
449+
let cstr_value = CString::new(val.as_ref()).map_err(|_| Error::InvalidString)?;
451450
self.property(name, cstr_value.to_bytes_with_nul())
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<S: AsRef<str>>(
455+
&mut self,
456+
name: &str,
457+
values: Vec<S>,
458+
) -> Result<()> {
456459
let mut bytes = Vec::new();
457460
for s in values {
458-
let cstr = CString::new(s).map_err(|_| Error::InvalidString)?;
461+
let cstr = CString::new(s.as_ref()).map_err(|_| Error::InvalidString)?;
459462
bytes.extend_from_slice(cstr.to_bytes_with_nul());
460463
}
461464
self.property(name, &bytes)
@@ -706,25 +709,27 @@ mod tests {
706709
fdt.property_u32("u32", 0x12345678).unwrap();
707710
fdt.property_u64("u64", 0x1234567887654321).unwrap();
708711
fdt.property_string("str", "hello").unwrap();
709-
fdt.property_string_list("strlst", vec!["hi".into(), "bye".into()])
712+
fdt.property_string_list("strlst", vec!["hi", "bye"])
710713
.unwrap();
711714
fdt.property_array_u32("arru32", &[0x12345678, 0xAABBCCDD])
712715
.unwrap();
713716
fdt.property_array_u64("arru64", &[0x1234567887654321])
714717
.unwrap();
718+
fdt.property_string_list("stringlst", vec![String::from("hi"), String::from("bye")])
719+
.unwrap();
715720
fdt.end_node(root_node).unwrap();
716721
let actual_fdt = fdt.finish().unwrap();
717722
let expected_fdt = vec![
718723
0xd0, 0x0d, 0xfe, 0xed, // 0000: magic (0xd00dfeed)
719-
0x00, 0x00, 0x00, 0xee, // 0004: totalsize (0xEE)
724+
0x00, 0x00, 0x01, 0x0c, // 0004: totalsize (0x10C)
720725
0x00, 0x00, 0x00, 0x38, // 0008: off_dt_struct (0x38)
721-
0x00, 0x00, 0x00, 0xc8, // 000C: off_dt_strings (0xC8)
726+
0x00, 0x00, 0x00, 0xdc, // 000C: off_dt_strings (0xDC)
722727
0x00, 0x00, 0x00, 0x28, // 0010: off_mem_rsvmap (0x28)
723728
0x00, 0x00, 0x00, 0x11, // 0014: version (0x11 = 17)
724729
0x00, 0x00, 0x00, 0x10, // 0018: last_comp_version (0x10 = 16)
725730
0x00, 0x00, 0x00, 0x00, // 001C: boot_cpuid_phys (0)
726-
0x00, 0x00, 0x00, 0x26, // 0020: size_dt_strings (0x26)
727-
0x00, 0x00, 0x00, 0x90, // 0024: size_dt_struct (0x90)
731+
0x00, 0x00, 0x00, 0x30, // 0020: size_dt_strings (0x30)
732+
0x00, 0x00, 0x00, 0xa4, // 0024: size_dt_struct (0xA4)
728733
0x00, 0x00, 0x00, 0x00, // 0028: rsvmap terminator (address = 0 high)
729734
0x00, 0x00, 0x00, 0x00, // 002C: rsvmap terminator (address = 0 low)
730735
0x00, 0x00, 0x00, 0x00, // 0030: rsvmap terminator (size = 0 high)
@@ -763,15 +768,22 @@ mod tests {
763768
0x00, 0x00, 0x00, 0x1f, // 00B4: prop nameoff (0x1F)
764769
0x12, 0x34, 0x56, 0x78, // 00B8: prop u64 value 0 high
765770
0x87, 0x65, 0x43, 0x21, // 00BC: prop u64 value 0 low
766-
0x00, 0x00, 0x00, 0x02, // 00C0: FDT_END_NODE
767-
0x00, 0x00, 0x00, 0x09, // 00C4: FDT_END
768-
b'n', b'u', b'l', b'l', 0x00, // 00C8: strings + 0x00: "null""
769-
b'u', b'3', b'2', 0x00, // 00CD: strings + 0x05: "u32"
770-
b'u', b'6', b'4', 0x00, // 00D1: strings + 0x09: "u64"
771-
b's', b't', b'r', 0x00, // 00D5: strings + 0x0D: "str"
772-
b's', b't', b'r', b'l', b's', b't', 0x00, // 00D9: strings + 0x11: "strlst"
773-
b'a', b'r', b'r', b'u', b'3', b'2', 0x00, // 00E0: strings + 0x18: "arru32"
774-
b'a', b'r', b'r', b'u', b'6', b'4', 0x00, // 00E7: strings + 0x1F: "arru64"
771+
0x00, 0x00, 0x00, 0x03, // 00C0: FDT_PROP (string)
772+
0x00, 0x00, 0x00, 0x07, // 00C4: prop len (7)
773+
0x00, 0x00, 0x00, 0x26, // 00C8: prop nameoff (0x0D)
774+
b'h', b'i', 0x00, b'b', // 00CC: prop value ("hi", "bye")
775+
b'y', b'e', 0x00, 0x00, // 00D0: "ye\0" + padding
776+
0x00, 0x00, 0x00, 0x02, // 00D4: FDT_END_NODE
777+
0x00, 0x00, 0x00, 0x09, // 00D8: FDT_END
778+
b'n', b'u', b'l', b'l', 0x00, // 00DC: strings + 0x00: "null""
779+
b'u', b'3', b'2', 0x00, // 00E1: strings + 0x05: "u32"
780+
b'u', b'6', b'4', 0x00, // 00E5: strings + 0x09: "u64"
781+
b's', b't', b'r', 0x00, // 00E9: strings + 0x0D: "str"
782+
b's', b't', b'r', b'l', b's', b't', 0x00, // 00ED: strings + 0x11: "strlst"
783+
b'a', b'r', b'r', b'u', b'3', b'2', 0x00, // 00F4: strings + 0x18: "arru32"
784+
b'a', b'r', b'r', b'u', b'6', b'4', 0x00, // 00FB: strings + 0x1F: "arru64"
785+
b's', b't', b'r', b'i', b'n', b'g', // 0102: strings + 0x26: "string"
786+
b'l', b's', b't', 0x00, // 0108: strings + 0x2c: "lst\0"
775787
];
776788
assert_eq!(expected_fdt, actual_fdt);
777789
}
@@ -951,12 +963,22 @@ mod tests {
951963
fdt.property_string("mystr", "abc\0def").unwrap_err(),
952964
Error::InvalidString
953965
);
966+
assert_eq!(
967+
fdt.property_string("mystr", String::from("abc\0def"))
968+
.unwrap_err(),
969+
Error::InvalidString
970+
);
954971
}
955972

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"];
977+
assert_eq!(
978+
fdt.property_string_list("mystr", strs).unwrap_err(),
979+
Error::InvalidString
980+
);
981+
let strs = vec![String::from("test"), String::from("abc\0def")];
960982
assert_eq!(
961983
fdt.property_string_list("mystr", strs).unwrap_err(),
962984
Error::InvalidString

0 commit comments

Comments
 (0)