Function bcs::from_bytes
source · pub fn from_bytes<'a, T>(bytes: &'a [u8]) -> Result<T>where
T: Deserialize<'a>,
Expand description
Deserializes a &[u8]
into a type.
This function will attempt to interpret bytes
as the BCS serialized form of T
and
deserialize T
from bytes
.
§Examples
use bcs::from_bytes;
use serde::Deserialize;
#[derive(Deserialize)]
struct Ip([u8; 4]);
#[derive(Deserialize)]
struct Port(u16);
#[derive(Deserialize)]
struct SocketAddr {
ip: Ip,
port: Port,
}
let bytes = vec![0x7f, 0x00, 0x00, 0x01, 0x41, 0x1f];
let socket_addr: SocketAddr = from_bytes(&bytes).unwrap();
assert_eq!(socket_addr.ip.0, [127, 0, 0, 1]);
assert_eq!(socket_addr.port.0, 8001);