use core::mem;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[derive(Hash)]
#[repr(transparent)]
pub struct BStr {
pub(crate) bytes: [u8],
}
impl BStr {
#[inline]
pub fn new<'a, B: ?Sized + AsRef<[u8]>>(bytes: &'a B) -> &'a BStr {
BStr::from_bytes(bytes.as_ref())
}
#[inline]
pub(crate) fn new_mut<B: ?Sized + AsMut<[u8]>>(
bytes: &mut B,
) -> &mut BStr {
BStr::from_bytes_mut(bytes.as_mut())
}
#[inline]
pub(crate) fn from_bytes(slice: &[u8]) -> &BStr {
unsafe { mem::transmute(slice) }
}
#[inline]
pub(crate) fn from_bytes_mut(slice: &mut [u8]) -> &mut BStr {
unsafe { mem::transmute(slice) }
}
#[inline]
#[cfg(feature = "alloc")]
pub(crate) fn from_boxed_bytes(slice: Box<[u8]>) -> Box<BStr> {
unsafe { Box::from_raw(Box::into_raw(slice) as _) }
}
#[inline]
#[cfg(feature = "alloc")]
pub(crate) fn into_boxed_bytes(slice: Box<BStr>) -> Box<[u8]> {
unsafe { Box::from_raw(Box::into_raw(slice) as _) }
}
#[inline]
pub(crate) fn as_bytes(&self) -> &[u8] {
&self.bytes
}
}