Trait ToBytes

Source
pub trait ToBytes<'a> {
    type Bytes: AsRef<[u8]>;

    // Required method
    fn to_bytes(&self) -> Result<Self::Bytes, Error>;
}
Expand description

ToBytes is used to define how a type should be encoded when working with Extism memory. It is used for plugin input and host function output.

ToBytes can be derived by delegating encoding to generic type implementing ToBytes, e.g., Json, Msgpack.

use extism_convert::{Json, ToBytes};
use serde::Serialize;

#[derive(ToBytes, Serialize)]
#[encoding(Json)]
struct Struct {
    hello: String,
}

assert_eq!(Struct { hello: "hi".into() }.to_bytes()?, br#"{"hello":"hi"}"#);

But custom types can also be used, as long as they are new-types with a single generic argument, i.e., Type<T>(T), that implement ToBytes for the struct.

use extism_convert::{Error, ToBytes};

// Custom serialization using `ToString`
struct StringEnc<T>(T);
impl<T: ToString> ToBytes<'_> for StringEnc<&T> {
    type Bytes = String;
     
    fn to_bytes(&self) -> Result<String, Error> {
        Ok(self.0.to_string())
    }
}

#[derive(ToBytes)]
#[encoding(StringEnc)]
struct Struct {
    hello: String,
}

impl ToString for Struct {
   fn to_string(&self) -> String {
       self.hello.clone()     
   }
}

assert_eq!(Struct { hello: "hi".into() }.to_bytes()?, b"hi");

Required Associated Types§

Source

type Bytes: AsRef<[u8]>

A configurable byte slice representation, allows any type that implements AsRef<[u8]>

Required Methods§

Source

fn to_bytes(&self) -> Result<Self::Bytes, Error>

to_bytes converts a value into Self::Bytes

Implementations on Foreign Types§

Source§

impl ToBytes<'_> for Value

Source§

impl ToBytes<'_> for f32

Source§

type Bytes = [u8; 4]

Source§

fn to_bytes(&self) -> Result<<f32 as ToBytes<'_>>::Bytes, Error>

Source§

impl ToBytes<'_> for f64

Source§

type Bytes = [u8; 8]

Source§

fn to_bytes(&self) -> Result<<f64 as ToBytes<'_>>::Bytes, Error>

Source§

impl ToBytes<'_> for i32

Source§

type Bytes = [u8; 4]

Source§

fn to_bytes(&self) -> Result<<i32 as ToBytes<'_>>::Bytes, Error>

Source§

impl ToBytes<'_> for i64

Source§

type Bytes = [u8; 8]

Source§

fn to_bytes(&self) -> Result<<i64 as ToBytes<'_>>::Bytes, Error>

Source§

impl ToBytes<'_> for u32

Source§

type Bytes = [u8; 4]

Source§

fn to_bytes(&self) -> Result<<u32 as ToBytes<'_>>::Bytes, Error>

Source§

impl ToBytes<'_> for u64

Source§

type Bytes = [u8; 8]

Source§

fn to_bytes(&self) -> Result<<u64 as ToBytes<'_>>::Bytes, Error>

Source§

impl ToBytes<'_> for ()

Source§

type Bytes = [u8; 0]

Source§

fn to_bytes(&self) -> Result<<() as ToBytes<'_>>::Bytes, Error>

Source§

impl ToBytes<'_> for String

Source§

impl ToBytes<'_> for Vec<u8>

Source§

type Bytes = Vec<u8>

Source§

fn to_bytes(&self) -> Result<<Vec<u8> as ToBytes<'_>>::Bytes, Error>

Source§

impl<'a> ToBytes<'a> for &'a str

Source§

type Bytes = &'a str

Source§

fn to_bytes(&self) -> Result<<&'a str as ToBytes<'a>>::Bytes, Error>

Source§

impl<'a> ToBytes<'a> for &'a [u8]

Source§

type Bytes = &'a [u8]

Source§

fn to_bytes(&self) -> Result<<&'a [u8] as ToBytes<'a>>::Bytes, Error>

Source§

impl<'a, T> ToBytes<'a> for Option<T>
where T: ToBytes<'a>,

Source§

type Bytes = Vec<u8>

Source§

fn to_bytes(&self) -> Result<<Option<T> as ToBytes<'a>>::Bytes, Error>

Source§

impl<'a, T> ToBytes<'a> for &'a T
where T: ToBytes<'a>,

Source§

type Bytes = <T as ToBytes<'a>>::Bytes

Source§

fn to_bytes(&self) -> Result<<&'a T as ToBytes<'a>>::Bytes, Error>

Implementors§

Source§

impl<'a, T> ToBytes<'a> for Json<T>
where T: Serialize,

Source§

impl<'a, T> ToBytes<'a> for Msgpack<T>
where T: Serialize,

Source§

impl<'a, T> ToBytes<'a> for Raw<'a, T>
where T: Pod,

Source§

type Bytes = &'a [u8]

Source§

impl<T> ToBytes<'_> for Base64<T>
where T: AsRef<[u8]>,

Source§

impl<T> ToBytes<'_> for Prost<T>
where T: Message,