use bytes::Bytes;
use std::fmt;
use std::ops::Deref;
#[derive(Clone, PartialEq, Eq, Hash, Default)]
pub struct Blob {
inner: Bytes,
}
impl Blob {
pub fn new() -> Self {
Self { inner: Bytes::new() }
}
pub fn from_bytes(bytes: Bytes) -> Self {
Self { inner: bytes }
}
pub fn from_static(data: &'static [u8]) -> Self {
Self {
inner: Bytes::from_static(data),
}
}
pub fn size(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
}
impl Deref for Blob {
type Target = [u8];
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl AsRef<[u8]> for Blob {
fn as_ref(&self) -> &[u8] {
&self.inner
}
}
impl From<Vec<u8>> for Blob {
fn from(vec: Vec<u8>) -> Self {
Self {
inner: Bytes::from(vec),
}
}
}
impl From<&'static [u8]> for Blob {
fn from(data: &'static [u8]) -> Self {
Self::from_static(data)
}
}
impl fmt::Debug for Blob {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Blob").field("len", &self.inner.len()).finish()
}
}