use std::borrow::Cow;
use std::ops::Deref;
pub struct Frame<'a> {
data: Cow<'a, [u8]>,
}
impl<'a> Frame<'a> {
pub fn new(data: &'a [u8]) -> Self {
Self {
data: Cow::Borrowed(data),
}
}
pub fn new_borrowed(data: &'a [u8]) -> Self {
Self::new(data)
}
pub fn new_owned(data: Vec<u8>) -> Self {
Self {
data: Cow::Owned(data),
}
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn payload(&self) -> &[u8] {
self.data.as_ref()
}
}
impl Deref for Frame<'_> {
type Target = [u8];
fn deref(&self) -> &Self::Target {
self.data.as_ref()
}
}
impl<'a> From<&'a [u8]> for Frame<'a> {
fn from(data: &'a [u8]) -> Self {
Self::new(data)
}
}