Macro nbt_lib::unwrap_to_empty

source ·
macro_rules! unwrap_to_empty {
    ($data:expr, $to:ident) => { ... };
    ($data:expr, $name:expr, $to:ident) => { ... };
}
Expand description

Unwraps an Option<NbtValue> and converts it to a specific type using a method named as_<type>.

§Examples

use nbt_lib::unwrap_to_empty;
use nbt_lib::NbtValue;

fn test() -> Result<i32, ()> {
    let data: Option<NbtValue> = Some(NbtValue::Int(42));
    let result: i32 = unwrap_to_empty!(data, i32);
    Ok(result)
}
assert_eq!(test(), Ok(42))
use nbt_lib::unwrap_to_empty;
use nbt_lib::NbtValue;
use std::collections::HashMap;

fn test() -> Result<i32, ()> {
    let mut map = HashMap::new();
    map.insert("key".to_string(), NbtValue::Int(42));
    let data: HashMap<String, NbtValue> = map;
    let result: i32 = unwrap_to_empty!(data, "key", i32);
    Ok(result)
}
assert_eq!(test(), Ok(42))

§Errors

Returns an error if the option is None or if the conversion fails.