Function fastnbt::value::from_value
source · [−]pub fn from_value<'de, T>(value: &'de Value) -> Result<T, Error> where
T: Deserialize<'de>,
Expand description
Interpret a fastnbt::Value
as an instance of type T
.
Example
use serde::Deserialize;
use fastnbt::nbt;
#[derive(Deserialize, Debug)]
struct User {
fingerprint: String,
location: String,
}
// The type of `j` is `fastnbt::Value`
let j = nbt!({
"fingerprint": "0xF9BA143B95FF6D82",
"location": "Menlo Park, CA"
});
let u: User = fastnbt::from_value(&j).unwrap();
println!("{:#?}", u);
Errors
This conversion can fail if the structure of the Value does not match the
structure expected by T
, for example if T
is a struct type but the Value
contains something other than an NBT compound. It can also fail if the structure
is correct but T
’s implementation of Deserialize
decides that something
is wrong with the data, for example required struct fields are missing from
the NBT compound or some number is too big to fit in the expected primitive
type.