#![allow(dead_code)]
#[cfg(feature = "codec")]
use serde::Serialize;
#[cfg(feature = "codec")]
use serde::de::DeserializeOwned;
#[cfg(feature = "codec")]
pub fn encoded_bytes<T>(value: &T) -> Vec<u8>
where
T: ?Sized + Serialize,
{
secure_types::encode(value)
.expect("encode failed")
.unlock_slice(<[u8]>::to_vec)
}
#[cfg(feature = "codec")]
pub fn decode_bytes<T>(bytes: &[u8]) -> Result<T, secure_types::DecodeError>
where
T: DeserializeOwned,
{
secure_types::decode_slice(bytes)
}
#[cfg(feature = "serde")]
pub struct OwnedString(pub String);
#[cfg(feature = "serde")]
pub struct OwnedBytes(pub Vec<u8>);
#[cfg(feature = "serde")]
impl<'de> serde::Deserializer<'de> for OwnedString {
type Error = serde::de::value::Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
visitor.visit_string(self.0)
}
serde::forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserializer<'de> for OwnedBytes {
type Error = serde::de::value::Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
visitor.visit_byte_buf(self.0)
}
serde::forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
bytes byte_buf option unit unit_struct newtype_struct seq tuple
tuple_struct map struct enum identifier ignored_any
}
}