restless-data 0.1.0

Helper types to encode and decode request and response bodies for the restless crate.
Documentation
use crate::{Decodable, Encodable};
use postcard::{from_bytes, to_allocvec, Error};
use serde::{de::DeserializeOwned, Serialize};

/// Encode and decode data as postcard using `postcard`.
#[cfg(any(doc, feature = "postcard"))]
#[derive(Clone, Debug)]
pub struct Postcard<T>(pub T);

impl<T: Serialize> Encodable for super::Postcard<T> {
    fn encode(&self) -> Vec<u8> {
        to_allocvec(&self.0).unwrap()
    }
}

impl<T: DeserializeOwned> Decodable for Postcard<T> {
    type Target = T;
    type Error = Error;

    fn decode(data: &[u8]) -> Result<Self::Target, Self::Error> {
        from_bytes(data)
    }
}