pub fn from_slice<T: BorshDeserialize>(v: &[u8]) -> Result<T>
Expand description
Deserializes an object from a slice of bytes.
§Example
use borsh::{BorshDeserialize, BorshSerialize, from_slice, to_vec};
/// derive is only available if borsh is built with `features = ["derive"]`
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
struct MyStruct {
a: u64,
b: Vec<u8>,
}
let original = MyStruct { a: 10, b: vec![1, 2, 3] };
let encoded = to_vec(&original).unwrap();
let decoded = from_slice::<MyStruct>(&encoded).unwrap();
assert_eq!(original, decoded);
§Panics
If the data is invalid, this function will panic.
§Errors
If the data is invalid, this function will return an error.
§Note
This function will return an error if the data is not fully read.