Trait extism_convert::FromBytes
source · pub trait FromBytes<'a>: Sized {
// Required method
fn from_bytes(data: &'a [u8]) -> Result<Self, Error>;
}
Expand description
FromBytes
is used to define how a type should be decoded when working with
Extism memory. It is used for plugin output and host function input.
FromBytes
can be derived by delegating encoding to generic type implementing
FromBytes
, e.g., Json
, Msgpack
.
use extism_convert::{Json, FromBytes};
use serde::Deserialize;
#[derive(FromBytes, Deserialize, PartialEq, Debug)]
#[encoding(Json)]
struct Struct {
hello: String,
}
assert_eq!(Struct::from_bytes(br#"{"hello":"hi"}"#)?, Struct { hello: "hi".into() });
Custom encodings can also be used, through new-types with a single generic
argument, i.e., Type<T>(T)
, that implement FromBytesOwned
for the struct.
use std::str::{self, FromStr};
use std::convert::Infallible;
use extism_convert::{Error, FromBytes, FromBytesOwned};
// Custom serialization using `FromStr`
struct StringEnc<T>(T);
impl<T: FromStr> FromBytesOwned for StringEnc<T> where Error: From<<T as FromStr>::Err> {
fn from_bytes_owned(data: &[u8]) -> Result<Self, Error> {
Ok(Self(str::from_utf8(data)?.parse()?))
}
}
#[derive(FromBytes, PartialEq, Debug)]
#[encoding(StringEnc)]
struct Struct {
hello: String,
}
impl FromStr for Struct {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Infallible> {
Ok(Self { hello: s.to_owned() })
}
}
assert_eq!(Struct::from_bytes(b"hi")?, Struct { hello: "hi".into() });
Required Methods§
sourcefn from_bytes(data: &'a [u8]) -> Result<Self, Error>
fn from_bytes(data: &'a [u8]) -> Result<Self, Error>
Decode a value from a slice of bytes
Object Safety§
This trait is not object safe.