1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
//! FreeBSD implementation of Name/value pairs library.
//! All insert operation clone values using `dup(2)` system call. So you
//! don't have to worry about the lifetime of the value. Unless you leak NvList
//! yourself using `unsafe`
//! you don't have to worry about anything. Once list goes out of scope it will
//! call to C library
//! to free all resources associated with it. `nvlist_take_*` and
//! `nvlist_move_*` operations are
//! not supported for this very reason.
//!
//! It's missing a few features:
//!
//! - Sending to socket
//! - Receiving from socket
//! - Insert/Remove file descriptors
//! - Insert/Remove binary
//! - Take operations
//! - Iterator interface
use libc::ENOMEM;
// Importing all because it's cold, I dont want to turn on heater and it's hard
// to type.
use libnv_sys::*;
use std::{convert::{From, Into},
ffi::{CStr, CString},
os::unix::io::AsRawFd,
slice};
use crate::{NvError, NvResult};
/// Enumeration of available data types that the API supports.
pub enum NvType {
/// Empty type
None = 0,
/// There is no associated data with the name
Null = 1,
/// The value is a `bool` value
Bool = 2,
/// The value is a `u64` value
Number = 3,
/// The value is a C string
String = 4,
/// The value is another `nvlist`
NvList = 5,
/// The value is a file descriptor
Descriptor = 6,
/// The value is a binary buffer
Binary = 7,
/// The value is an array of `bool` values
BoolArray = 8,
/// The value is an array of `u64` values
NumberArray = 9,
/// The value is an array of C strings
StringArray = 10,
/// The value is an array of other `nvlist`'s
NvListArray = 11,
/// The value is an array of file descriptors
DescriptorArray = 12,
}
/// Options available for creation of an `nvlist`
#[repr(i32)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum NvFlag {
/// No user specified options.
None = 0,
/// Perform case-insensitive lookups of provided names.
IgnoreCase = 1,
/// Names in the nvlist do not have to be unique.
NoUnique = 2,
/// Allow duplicate case-insensitive keys.
Both = 3,
}
impl From<i32> for NvFlag {
/// This should be TryFrom. This function WILL panic if you pass incorrect
/// value to it. This should be impossible.
fn from(source: i32) -> Self {
match source {
0 => NvFlag::None,
1 => NvFlag::IgnoreCase,
2 => NvFlag::NoUnique,
3 => NvFlag::Both,
_ => panic!("Incorrect value passed to NvFlag"),
}
}
}
macro_rules! impl_list_op {
($type_:ty, $method:ident, false) => {
impl NvTypeOp for $type_ {
/// Add a `$type_` value to the `NvList`
fn add_to_list(&self, list: &mut NvList, name: &str) -> NvResult<()> {
return list.$method(name, *self);
}
}
};
($type_:ty, $method:ident, true) => {
impl NvTypeOp for $type_ {
/// Add a `$type_` value to the `NvList`
fn add_to_list(&self, list: &mut NvList, name: &str) -> NvResult<()> {
return list.$method(name, &*self);
}
}
};
}
/// This allows usage of insert method with basic types. Implement this for your
/// own types if you don't want to convert to primitive types every time.
pub trait NvTypeOp {
/// Add self to given list.
fn add_to_list(&self, list: &mut NvList, name: &str) -> NvResult<()>;
}
impl_list_op! {bool, insert_bool, false}
impl_list_op! {[bool], insert_bools, true}
impl_list_op! {u8, insert_number, false}
impl_list_op! {u16, insert_number, false}
impl_list_op! {u32, insert_number, false}
impl_list_op! {u64, insert_number, false}
impl_list_op! {[u64], insert_numbers, true}
impl_list_op! {str, insert_string, true}
impl_list_op! {NvList, insert_nvlist, true}
/// If `Some` insert content to the list. If `None` insert null.
impl<T> NvTypeOp for Option<T>
where
T: NvTypeOp,
{
fn add_to_list(&self, list: &mut NvList, name: &str) -> NvResult<()> {
match self {
Some(ref val) => val.add_to_list(list, name),
None => list.insert_null(name),
}
}
}
/// A list of name/value pairs.
#[derive(Debug)]
pub struct NvList {
ptr: *mut nvlist,
}
#[doc(hidden)]
/// Return new list with no flags.
impl Default for NvList {
fn default() -> NvList { NvList::new(NvFlag::None).expect("Failed to create new list") }
}
impl NvList {
/// Make a copy of a pointer. Danger zone.
pub fn as_ptr(&self) -> *mut nvlist { self.ptr }
fn check_if_error(&self) -> NvResult<()> {
match self.error() {
errno if errno == 0 => Ok(()),
errno => Err(NvError::NativeError(errno)),
}
}
/// Create a new name/value pair list (`nvlist`). Call this can only fail
/// when system is out of memory.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let nvlist = NvList::new(NvFlag::None).unwrap();
/// ```
pub fn new(flags: NvFlag) -> NvResult<NvList> {
let raw_list = unsafe { nvlist_create(flags as i32) };
if raw_list.is_null() {
Err(NvError::NativeError(ENOMEM))
} else {
Ok(NvList { ptr: raw_list })
}
}
/// Determines if the `nvlist` is empty.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
/// let nvlist = NvList::new(NvFlag::IgnoreCase).unwrap();
/// assert!(nvlist.is_empty());
/// ```
pub fn is_empty(&self) -> bool { unsafe { nvlist_empty(self.ptr) } }
/// The flags the `nvlist` was created with.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
/// let nvlist = NvList::new(NvFlag::NoUnique).unwrap();
///
/// assert_eq!(nvlist.flags(), NvFlag::NoUnique);
/// ```
pub fn flags(&self) -> NvFlag { NvFlag::from(unsafe { nvlist_flags(self.ptr) }) }
/// Gets error value that the list may have accumulated.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
/// let list = NvList::new(NvFlag::NoUnique).unwrap();
///
/// assert_eq!(0, list.error());
/// ```
pub fn error(&self) -> i32 { unsafe { nvlist_error(self.ptr) } }
/// Sets the `NvList` to be in an error state.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::new(NvFlag::Both).unwrap();
///
/// // EINVAL
/// list.set_error(0x16).unwrap();
///
/// assert_eq!(0x16, list.error());
/// ```
pub fn set_error(&mut self, error: i32) -> NvResult<()> {
if self.error() != 0 {
Err(NvError::AlreadySet)
} else {
unsafe { nvlist_set_error(self.ptr, error) }
Ok(())
}
}
/// Sugared way to add a single value to the NvList.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag, NvTypeOp};
///
/// let mut list = NvList::default();
///
/// let the_answer: u32 = 1776;
/// let not_the_answer: Option<u64> = None;
///
/// list.insert("Important year", the_answer);
/// list.insert("not important year", not_the_answer);
/// let copy = list.clone();
/// list.insert("foo", copy);
///
/// assert_eq!(list.get_number("Important year").unwrap().unwrap(), 1776);
/// ```
pub fn insert<T: NvTypeOp>(&mut self, name: &str, value: T) -> NvResult<()> {
value.add_to_list(self, name)
}
/// Add a null value to the `NvList`.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag, NvTypeOp};
/// let mut list = NvList::new(NvFlag::Both).unwrap();
/// list.insert_null("Hello, World!");
/// ```
pub fn insert_null(&mut self, name: &str) -> NvResult<()> {
let c_name = CString::new(name)?;
unsafe {
nvlist_add_null(self.ptr, c_name.as_ptr());
}
self.check_if_error()
}
/// Add a number to the `NvList`. Number will be converted into u64.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::new(NvFlag::Both).unwrap();
///
/// list.insert_number("Important year", 1776u64);
/// ```
pub fn insert_number<I: Into<u64>>(&mut self, name: &str, value: I) -> NvResult<()> {
let c_name = CString::new(name)?;
unsafe {
nvlist_add_number(self.ptr, c_name.as_ptr(), value.into());
}
self.check_if_error()
}
/// Add a `bool` to the list.
pub fn insert_bool(&mut self, name: &str, value: bool) -> NvResult<()> {
let c_name = CString::new(name)?;
unsafe {
nvlist_add_bool(self.ptr, c_name.as_ptr(), value);
}
self.check_if_error()
}
/// Add string to the list.
pub fn insert_string(&mut self, name: &str, value: &str) -> NvResult<()> {
let c_name = CString::new(name)?;
let c_value = CString::new(value)?;
unsafe {
nvlist_add_string(self.ptr, c_name.as_ptr(), c_value.as_ptr());
}
self.check_if_error()
}
/// Add `NvList` to the list.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::default();
///
/// let other_list = NvList::default();
///
/// list.insert_nvlist("other list", &other_list).unwrap();
/// ```
pub fn insert_nvlist(&mut self, name: &str, value: &NvList) -> NvResult<()> {
let c_name = CString::new(name)?;
if !value.as_ptr().is_null() {
unsafe {
nvlist_add_nvlist(self.ptr, c_name.as_ptr(), value.as_ptr());
}
}
self.check_if_error()
}
/// Add binary data to the list. TODO: make this safe.
pub unsafe fn add_binary(&mut self, name: &str, value: *mut i8, size: u32) -> NvResult<()> {
let c_name = CString::new(name)?;
nvlist_add_binary(self.ptr, c_name.as_ptr(), value, size);
self.check_if_error()
}
/// Add an array of `bool` values.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::new(NvFlag::Both).unwrap();
///
/// let slice = [true, false, true, false];
///
/// list.insert_bools("Important year", &slice);
/// ```
pub fn insert_bools(&mut self, name: &str, value: &[bool]) -> NvResult<()> {
let c_name = CString::new(name)?;
unsafe {
nvlist_add_bool_array(self.ptr, c_name.as_ptr(), value.as_ptr(), value.len());
}
self.check_if_error()
}
/// Add an array if `u64`. TODO: Make it work with any number...
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::new(NvFlag::None).unwrap();
///
/// let slice = [1776, 2017];
///
/// list.insert_numbers("Important year", &slice);
/// ```
pub fn insert_numbers(&mut self, name: &str, value: &[u64]) -> NvResult<()> {
let c_name = CString::new(name)?;
unsafe {
nvlist_add_number_array(self.ptr, c_name.as_ptr(), value.as_ptr(), value.len());
}
self.check_if_error()
}
/// Add an array of strings
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::new(NvFlag::None).unwrap();
///
/// let orig = ["Hello", "World!"];
///
/// list.insert_strings("key", &orig).unwrap();
///
/// let vec = list.get_strings("key").unwrap().unwrap();
///
/// assert_eq!(*vec, ["Hello", "World!"]);
/// ```
pub fn insert_strings(&mut self, name: &str, value: &[&str]) -> NvResult<()> {
let c_name = CString::new(name)?;
let strings: Vec<CString> = value
.iter()
.map(|e| CString::new(*e))
.map(|e| e.expect("Failed to convert str to Cstring"))
.collect();
unsafe {
let pointers: Vec<*const i8> = strings.iter().map(|e| e.as_ptr()).collect();
nvlist_add_string_array(
self.ptr,
c_name.as_ptr(),
pointers.as_slice().as_ptr(),
strings.len(),
);
}
self.check_if_error()
}
/// Add an array of `NvList`s
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::new(NvFlag::Both).unwrap();
///
/// let slice = [NvList::new(NvFlag::Both).unwrap(),
/// NvList::new(NvFlag::Both).unwrap(),
/// NvList::new(NvFlag::None).unwrap()];
///
/// list.insert_nvlists("nvlists", &slice);
///
/// let mut nvlists = list.get_nvlists("nvlists").unwrap().unwrap();
///
/// assert_eq!(NvFlag::None, nvlists.pop().unwrap().flags());
/// ```
pub fn insert_nvlists(&mut self, name: &str, value: &[NvList]) -> NvResult<()> {
let c_name = CString::new(name)?;
let vec = value.to_vec();
unsafe {
let lists: Vec<*const nvlist> =
vec.iter().map(|item| item.as_ptr() as *const nvlist).collect();
nvlist_add_nvlist_array(
self.ptr,
c_name.as_ptr(),
lists.as_slice().as_ptr(),
lists.len(),
);
}
self.check_if_error()
}
/// Returns `true` if a name/value pair exists in the `NvList` and `false`
/// otherwise.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::new(NvFlag::Both).unwrap();
///
/// let result = list.insert_number("Important year", 1776u64);
/// assert!(result.is_ok());
///
/// assert!(list.contains_key("Important year").unwrap());
/// ```
pub fn contains_key(&self, name: &str) -> NvResult<bool> {
let c_name = CString::new(name)?;
unsafe { Ok(nvlist_exists(self.ptr, c_name.as_ptr())) }
}
/// Returns `true` if a name/value pair of the specified type exists and
/// `false` otherwise.
/// ```
/// use libnv::libnv::{NvList, NvFlag, NvType};
///
/// let mut list = NvList::new(NvFlag::Both).unwrap();
///
/// let result = list.insert_number("Important year", 1776u64);
/// assert!(result.is_ok());
///
/// assert!(!list.contains_key_with_type("Important year", NvType::Bool).unwrap());
/// ```
pub fn contains_key_with_type(&self, name: &str, ty: NvType) -> NvResult<bool> {
let c_name = CString::new(name)?;
unsafe { Ok(nvlist_exists_type(self.ptr, c_name.as_ptr(), ty as i32)) }
}
/// Get the first matching `bool` value paired with
/// the given name.
///
/// ```
/// use libnv::libnv::NvList;
///
/// let mut list = NvList::default();
///
/// list.insert_bool("Did history start on 1776?", true).unwrap();
///
/// assert!(list.get_bool("Did history start on 1776?").unwrap().unwrap(),
/// true);
/// ```
pub fn get_bool(&self, name: &str) -> NvResult<Option<bool>> {
let c_name = CString::new(name)?;
unsafe {
if nvlist_exists_bool(self.ptr, c_name.as_ptr()) {
Ok(Some(nvlist_get_bool(self.ptr, c_name.as_ptr())))
} else {
Ok(None)
}
}
}
/// Get the first matching `u64` value paired with
/// the given name.
pub fn get_number(&self, name: &str) -> NvResult<Option<u64>> {
let c_name = CString::new(name)?;
unsafe {
if nvlist_exists_number(self.ptr, c_name.as_ptr()) {
Ok(Some(nvlist_get_number(self.ptr, c_name.as_ptr())))
} else {
Ok(None)
}
}
}
/// Get the first matching `u64` value paired with
/// the given name
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// // Note: we're allowing duplicate values per name
/// let mut list = NvList::default();
///
/// list.insert_string("Hello", "World!").unwrap();
///
/// assert_eq!(list.get_string("Hello").unwrap().unwrap(), "World!");
/// ```
pub fn get_string(&self, name: &str) -> NvResult<Option<String>> {
let c_name = CString::new(name)?;
unsafe {
if nvlist_exists_string(self.ptr, c_name.as_ptr()) {
let ret = nvlist_get_string(self.ptr, c_name.as_ptr());
if ret.is_null() {
Ok(None)
} else {
let len = strlen(ret);
Ok(Some(String::from_raw_parts(ret as *mut u8, len, len)))
}
} else {
Ok(None)
}
}
}
/// Get the first matching `NvList` value paired with
/// the given name and clone it
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::new(NvFlag::Both).unwrap();
///
/// list.insert_bool("other list", true).unwrap();
///
/// let mut other_list = NvList::new(NvFlag::None).unwrap();
/// other_list.insert_number("Important year", 42u32).unwrap();
///
/// list.insert_nvlist("other list", &other_list).unwrap();
///
/// // Since we use `get_nvlist` we will get the
/// // NvList not the boolean value
/// let other_nvlist = list.get_nvlist("other list").unwrap().unwrap();
///
/// assert_eq!(other_nvlist.get_number("Important year").unwrap().unwrap(),
/// 42);
/// ```
pub fn get_nvlist(&self, name: &str) -> NvResult<Option<NvList>> {
let c_name = CString::new(name)?;
unsafe {
if nvlist_exists_nvlist(self.ptr, c_name.as_ptr()) {
let res = nvlist_get_nvlist(self.ptr, c_name.as_ptr());
Ok(Some(NvList { ptr: nvlist_clone(res) }))
} else {
Ok(None)
}
}
}
/// Get a `&[bool]` from the `NvList`
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// // Note: we're allowing duplicate values per name
/// let mut list = NvList::new(NvFlag::None).unwrap();
///
/// list.insert_bools("true/false", &[true, false, true]).unwrap();
///
/// assert_eq!(list.get_bools("true/false").unwrap().unwrap(), &[true,
/// false, true]);
/// ```
pub fn get_bools<'a>(&'a self, name: &str) -> NvResult<Option<&'a [bool]>> {
let c_name = CString::new(name)?;
unsafe {
if nvlist_exists_bool_array(self.ptr, c_name.as_ptr()) {
let mut len: usize = 0;
let arr = nvlist_get_bool_array(self.ptr, c_name.as_ptr(), &mut len as *mut usize);
Ok(Some(slice::from_raw_parts(arr as *const bool, len)))
} else {
Ok(None)
}
}
}
/// Get a `&[u64]` slice from the `NvList`
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// // Note: we're allowing duplicate values per name
/// let mut list = NvList::default();
///
/// list.insert_numbers("The Year", &[1, 7, 7, 6]).unwrap();
///
/// assert_eq!(list.get_numbers("The Year").unwrap().unwrap(), &[1, 7, 7,
/// 6]);
/// ```
pub fn get_numbers(&self, name: &str) -> NvResult<Option<&[u64]>> {
let c_name = CString::new(name)?;
unsafe {
if nvlist_exists_number_array(self.ptr, c_name.as_ptr()) {
let mut len: usize = 0;
let arr =
nvlist_get_number_array(self.ptr, c_name.as_ptr(), &mut len as *mut usize);
Ok(Some(slice::from_raw_parts(arr as *const u64, len)))
} else {
Ok(None)
}
}
}
/// Get a `Vec<String>` of the first string slice added to the `NvList`
/// for the given name
pub fn get_strings(&self, name: &str) -> NvResult<Option<Vec<String>>> {
let c_name = CString::new(name)?;
unsafe {
if nvlist_exists_string_array(self.ptr, c_name.as_ptr()) {
let mut len: usize = 0;
let arr =
nvlist_get_string_array(self.ptr, c_name.as_ptr(), &mut len as *mut usize);
let slice = slice::from_raw_parts(arr as *const *const i8, len);
let strings = slice
.iter()
.copied()
.map(|ptr| CStr::from_ptr(ptr))
.map(|cstr| cstr.to_string_lossy())
.map(String::from)
.collect();
Ok(Some(strings))
} else {
Ok(None)
}
}
}
/// Get an array of `NvList`.
///
/// ```
/// use libnv::libnv::{NvList, NvFlag};
///
/// let mut list = NvList::new(NvFlag::None).unwrap();
///
/// list.insert_nvlists("lists", &[NvList::default(),
/// NvList::default()]).unwrap();
///
/// let vec = list.get_nvlists("lists").unwrap().unwrap();
///
/// assert_eq!(vec.len(), 2);
/// assert_eq!(vec[0].flags(), NvFlag::None);
/// ```
pub fn get_nvlists(&self, name: &str) -> NvResult<Option<Vec<NvList>>> {
let c_name = CString::new(name)?;
unsafe {
if nvlist_exists_nvlist_array(self.ptr, c_name.as_ptr()) {
let mut len: usize = 0;
let arr =
nvlist_get_nvlist_array(self.ptr, c_name.as_ptr(), &mut len as *mut usize);
let slice = slice::from_raw_parts(arr as *const *const nvlist, len);
Ok(Some(slice.iter().map(|item| NvList { ptr: nvlist_clone(*item) }).collect()))
} else {
Ok(None)
}
}
}
/// Write `NvList` to a file descriptor.
///
/// ```
/// use std::fs::File;
/// use libnv::libnv::NvList;
///
/// let mut list = NvList::default();
///
/// list.insert_number("Important year", 1776u64);
///
/// list.dump(File::create("/tmp/libnv_nv.dump").unwrap());
/// ```
pub fn dump<T: AsRawFd>(&self, out: T) -> NvResult<()> {
unsafe { nvlist_dump(self.ptr, out.as_raw_fd()) }
self.check_if_error()
}
/// The size of the current list
pub fn len(&self) -> i32 { unsafe { nvlist_size(self.ptr) } }
/// Removes a key from the `NvList`.
pub fn remove(&mut self, name: &str) -> NvResult<()> {
let c_name = CString::new(name)?;
unsafe {
nvlist_free(self.ptr, c_name.as_ptr());
}
self.check_if_error()
}
/// Remove the element of the given name and type
/// from the `NvList`
pub fn remove_with_type(&mut self, name: &str, ty: NvType) -> NvResult<()> {
let c_name = CString::new(name)?;
unsafe {
nvlist_free_type(self.ptr, c_name.as_ptr(), ty as i32);
}
self.check_if_error()
}
}
impl Clone for NvList {
/// Clone list using libnv method. This will perform deep copy.
fn clone(&self) -> NvList { NvList { ptr: unsafe { nvlist_clone(self.ptr) } } }
}
impl Drop for NvList {
/// Using libnv method.
fn drop(&mut self) {
unsafe {
nvlist_destroy(self.ptr);
}
}
}