Skip to content

Commit 666b677

Browse files
committed
rust: replace handwritten str_to_cstr_vec with alloc::ffi::CString
alloc::ffi::CString was added in Rust 1.64 and can be used as a drop in to our handwritten function.
1 parent 276bce1 commit 666b677

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

src/rust/bitbox02/src/util.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,9 @@ pub fn truncate_str(s: &str, len: usize) -> &str {
105105
/// Converts a Rust string to a null terminated C string by appending a null
106106
/// terminator. Returns `Err(())` if the input already contians a null byte.
107107
pub fn str_to_cstr_vec(input: &str) -> Result<Vec<u8>, ()> {
108-
let bytes = input.as_bytes();
109-
if bytes.contains(&0) {
110-
Err(())
111-
} else {
112-
let mut out = Vec::with_capacity(input.len() + 1);
113-
out.extend_from_slice(bytes);
114-
out.push(0); // null terminator
115-
Ok(out)
116-
}
108+
Ok(alloc::ffi::CString::new(input)
109+
.or(Err(()))?
110+
.into_bytes_with_nul())
117111
}
118112

119113
#[cfg(test)]

0 commit comments

Comments
 (0)