macro_rules! unwrap_to { ($data:expr, $t:ty) => { ... }; ($data:expr, $name:expr, $t:ty) => { ... }; }
Expand description
Unwraps an Option<NbtValue> and converts it into a specified type.
§Examples
use nbt_lib::unwrap_to;
use nbt_lib::NbtValue;
use nbt_lib::traits::FromNbtValue;
#[derive(Debug, PartialEq)]
struct Test { data: i32 };
impl FromNbtValue for Test {
fn from_nbt_value(value: NbtValue) -> Result<Self, ()> {
match value {
NbtValue::Int(v) => Ok(Self { data: v }),
_ => Err(())
}
}
}
fn test() -> Result<Test, ()> {
let data: Option<NbtValue> = Some(NbtValue::Int(42));
let result: Test = unwrap_to!(data, Test);
Ok(result)
}
assert_eq!(test(), Ok(Test { data: 42 }));use nbt_lib::unwrap_to;
use nbt_lib::NbtValue;
use std::collections::HashMap;
use nbt_lib::traits::FromNbtValue;
#[derive(Debug, PartialEq)]
struct Test { data: i32 };
impl FromNbtValue for Test {
fn from_nbt_value(value: NbtValue) -> Result<Self, ()> {
match value {
NbtValue::Int(v) => Ok(Self { data: v }),
_ => Err(())
}
}
}
fn test() -> Result<Test, ()> {
let mut map = HashMap::new();
map.insert("data".to_string(), NbtValue::Int(42));
let data: HashMap<String, NbtValue> = map;
let result: Test = unwrap_to!(data, "data", Test);
Ok(result)
}
assert_eq!(test(), Ok(Test { data: 42 }));§Errors
Returns an error if the option is None or if the conversion fails.