extism_convert

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<'a> ToBytes<'a> for &'a str

source§

type Bytes = &'a str

source§

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

source§

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

source§

type Bytes = &'a [u8]

source§

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

source§

impl<'a> ToBytes<'a> for Value

source§

type Bytes = Vec<u8>

source§

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

source§

impl<'a> ToBytes<'a> for f32

source§

type Bytes = [u8; 4]

source§

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

source§

impl<'a> ToBytes<'a> for f64

source§

type Bytes = [u8; 8]

source§

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

source§

impl<'a> ToBytes<'a> for i32

source§

type Bytes = [u8; 4]

source§

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

source§

impl<'a> ToBytes<'a> for i64

source§

type Bytes = [u8; 8]

source§

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

source§

impl<'a> ToBytes<'a> for u32

source§

type Bytes = [u8; 4]

source§

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

source§

impl<'a> ToBytes<'a> for u64

source§

type Bytes = [u8; 8]

source§

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

source§

impl<'a> ToBytes<'a> for ()

source§

type Bytes = [u8; 0]

source§

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

source§

impl<'a> ToBytes<'a> for String

source§

impl<'a> ToBytes<'a> for Vec<u8>

source§

type Bytes = Vec<u8>

source§

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

source§

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

source§

type Bytes = Vec<u8>

source§

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

source§

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

source§

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

source§

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

Implementors§

source§

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

source§

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

source§

type Bytes = &'a [u8]

source§

impl<'a, T: Message> ToBytes<'a> for Prost<T>

source§

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

source§

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