#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct LabelBytes<'a>(&'a [u8]);
impl<'a> LabelBytes<'a> {
#[must_use]
pub const fn new(bytes: &'a [u8]) -> Self {
Self(bytes)
}
#[must_use]
pub const fn as_bytes(&self) -> &'a [u8] {
self.0
}
#[must_use]
pub const fn len(&self) -> usize {
self.0.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<'a> From<&'a [u8]> for LabelBytes<'a> {
fn from(b: &'a [u8]) -> Self {
Self(b)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn byte_for_byte_equality() {
assert_eq!(LabelBytes::new(b"img"), LabelBytes::from(&b"img"[..]));
assert_ne!(LabelBytes::new(b"img"), LabelBytes::new(b"IMG"));
assert!(LabelBytes::default().is_empty());
assert_eq!(LabelBytes::new(b"ab").len(), 2);
}
}