Function rmp_serde::decode::from_slice
source · pub fn from_slice<'a, T>(input: &'a [u8]) -> Result<T, Error>where
T: Deserialize<'a>,
Expand description
Deserialize a temporary scope-bound instance of type T
from a slice, with zero-copy if possible.
Deserialization will be performed in zero-copy manner whenever it is possible, borrowing the data from the slice itself. For example, strings and byte-arrays won’t copied.
§Errors
This conversion can fail if the structure of the Value does not match the structure expected
by T
. 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.
§Examples
use serde::Deserialize;
// Encoded `["Bobby", 8]`.
let buf = [0x92, 0xa5, 0x42, 0x6f, 0x62, 0x62, 0x79, 0x8];
#[derive(Debug, Deserialize, PartialEq)]
struct Dog<'a> {
name: &'a str,
age: u8,
}
assert_eq!(Dog { name: "Bobby", age: 8 }, rmp_serde::from_slice(&buf).unwrap());