Skip to content

Commit

Permalink
feat: add NvList::add_binary (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
asomers authored Jan 12, 2023
1 parent f4a3ada commit 46120fa
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/libnv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,23 @@ impl NvList {
self.check_if_error()
}

/// Add binary data to the list. TODO: make this safe.
/// Add binary data to the list.
#[deprecated(since = "0.4.0", note = "use insert_binary instead")]
pub unsafe fn add_binary(&mut self, name: &str, value: *const i8, size: usize) -> NvResult<()> {
let c_name = CString::new(name)?;
nvlist_add_binary(self.ptr, c_name.as_ptr(), value as *const c_void, size);
self.check_if_error()
}

/// Add a byte array to the list.
pub fn insert_binary(&mut self, name: &str, value: &[u8]) -> NvResult<()> {
let c_name = CString::new(name)?;
unsafe {
nvlist_add_binary(self.ptr, c_name.as_ptr(), value.as_ptr() as *const c_void, value.len());
}
self.check_if_error()
}

/// Add an array of `bool` values.
///
/// ```
Expand Down Expand Up @@ -467,6 +477,31 @@ impl NvList {
unsafe { Ok(nvlist_exists_type(self.ptr, c_name.as_ptr(), ty as i32)) }
}

/// Get the first matching byte slice value for the given name
///
/// ```
/// use libnv::libnv::NvList;
///
/// let x = [1, 2, 3, 4];
/// let mut list = NvList::default();
/// list.insert_binary("x", &x).unwrap();
///
/// let v = list.get_binary("x").unwrap().unwrap();
/// assert_eq!(&x, v);
/// ```
pub fn get_binary(&self, name: &str) -> NvResult<Option<&[u8]>> {
let c_name = CString::new(name)?;
unsafe {
let mut size: usize = 0;
let ret = nvlist_get_binary(self.ptr, c_name.as_ptr(), &mut size as *mut usize);
if ret.is_null() {
Ok(None)
} else {
Ok(Some(slice::from_raw_parts(ret as *const u8, size)))
}
}
}

/// Get the first matching `bool` value paired with
/// the given name.
///
Expand Down

0 comments on commit 46120fa

Please sign in to comment.