Current section

Files

Jump to
veidrodelis native vdr_ts_nif src storage bytes.rs
Raw

native/vdr_ts_nif/src/storage/bytes.rs

use std::borrow::Borrow;
use std::cmp::Ordering;
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct Bytes(Arc<Vec<u8>>);
impl Bytes {
pub fn new(data: &[u8]) -> Self {
Bytes(Arc::new(data.to_vec()))
}
pub fn as_slice(&self) -> &[u8] {
&self.0
}
pub fn len(&self) -> usize {
self.0.len()
}
#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl Borrow<[u8]> for Bytes {
fn borrow(&self) -> &[u8] {
&self.0
}
}
impl PartialEq for Bytes {
fn eq(&self, other: &Self) -> bool {
self.0.as_slice() == other.0.as_slice()
}
}
impl Eq for Bytes {}
impl PartialOrd for Bytes {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Bytes {
fn cmp(&self, other: &Self) -> Ordering {
self.0.as_slice().cmp(other.0.as_slice())
}
}
impl std::hash::Hash for Bytes {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.as_slice().hash(state);
}
}