use crate::ByteLamination;
use std::borrow::Cow;
use std::error::Error;
impl<'a> ByteLamination<'a> for Cow<'a, [u8]> {
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.as_ref())
}
fn into_bytes(self) -> Vec<u8> {
self.into_owned()
}
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
Ok(bytes)
}
}
impl<'a> ByteLamination<'a> for Vec<u8> {
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.as_ref())
}
fn into_bytes(self) -> Vec<u8> {
self
}
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
Ok(bytes.into_owned())
}
}
impl<'a> ByteLamination<'a> for String {
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.as_bytes())
}
fn into_bytes(self) -> Vec<u8> {
self.into_bytes()
}
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
Ok(match bytes {
Cow::Borrowed(bslice) => std::str::from_utf8(bslice)?.to_owned(),
Cow::Owned(bvec) => String::from_utf8(bvec)?,
})
}
}
impl<'a> ByteLamination<'a> for Cow<'a, str> {
fn as_cow_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.as_bytes())
}
fn into_bytes(self) -> Vec<u8> {
self.into_owned().into_bytes()
}
fn try_from_bytes(bytes: Cow<'a, [u8]>) -> Result<Self, Box<dyn Error>> {
Ok(match bytes {
Cow::Borrowed(bslice) => Cow::Borrowed(std::str::from_utf8(bslice)?),
Cow::Owned(bvec) => Cow::Owned(String::from_utf8(bvec)?),
})
}
}