use bytes::Bytes;
use std::borrow::Cow;
pub trait IntoBytes {
fn as_slice(&self) -> &[u8];
fn into_bytes(self) -> Bytes;
}
impl IntoBytes for &[u8] {
fn as_slice(&self) -> &[u8] {
self
}
fn into_bytes(self) -> Bytes {
Bytes::copy_from_slice(self)
}
}
impl IntoBytes for Vec<u8> {
fn as_slice(&self) -> &[u8] {
self.as_slice()
}
fn into_bytes(self) -> Bytes {
Bytes::from(self)
}
}
impl IntoBytes for &Vec<u8> {
fn as_slice(&self) -> &[u8] {
&self[..]
}
fn into_bytes(self) -> Bytes {
Bytes::copy_from_slice(&self[..])
}
}
impl IntoBytes for Bytes {
fn as_slice(&self) -> &[u8] {
self.as_ref()
}
fn into_bytes(self) -> Bytes {
self
}
}
impl IntoBytes for &Bytes {
fn as_slice(&self) -> &[u8] {
self.as_ref()
}
fn into_bytes(self) -> Bytes {
self.clone()
}
}
impl IntoBytes for &str {
fn as_slice(&self) -> &[u8] {
self.as_bytes()
}
fn into_bytes(self) -> Bytes {
Bytes::copy_from_slice(self.as_bytes())
}
}
impl IntoBytes for String {
fn as_slice(&self) -> &[u8] {
self.as_bytes()
}
fn into_bytes(self) -> Bytes {
Bytes::from(self.into_bytes())
}
}
impl IntoBytes for &String {
fn as_slice(&self) -> &[u8] {
self.as_bytes()
}
fn into_bytes(self) -> Bytes {
Bytes::copy_from_slice(self.as_bytes())
}
}
impl IntoBytes for Box<[u8]> {
fn as_slice(&self) -> &[u8] {
self.as_ref()
}
fn into_bytes(self) -> Bytes {
Bytes::from(self)
}
}
impl<'a> IntoBytes for Cow<'a, [u8]> {
fn as_slice(&self) -> &[u8] {
self.as_ref()
}
fn into_bytes(self) -> Bytes {
match self {
Cow::Borrowed(s) => Bytes::copy_from_slice(s),
Cow::Owned(v) => Bytes::from(v),
}
}
}