use std::borrow::Cow; use ::bstr::{BStr, BString}; /// Provide a borrowed byte-string view of common string and byte containers. /// /// Unlike [`AsRef`], this conversion deliberately bridges representations such /// as [`String`] and [`Vec`] to [`BStr`] without allocating. pub trait AsBStr { /// Return this value as a borrowed byte string. fn as_bstr(&self) -> &BStr; } /// Provide an optional borrowed byte-string view of common string and byte containers. pub trait AsBStrOpt { /// Return this value as a borrowed byte string, or `None` if it is absent. fn as_bstr_opt(&self) -> Option<&BStr>; } impl AsBStrOpt for T { fn as_bstr_opt(&self) -> Option<&BStr> { Some(self.as_bstr()) } } impl AsBStrOpt for Option<&BStr> { fn as_bstr_opt(&self) -> Option<&BStr> { *self } } impl AsBStr for &T { fn as_bstr(&self) -> &BStr { T::as_bstr(self) } } impl AsBStr for &mut T { fn as_bstr(&self) -> &BStr { T::as_bstr(self) } } impl AsBStr for BStr { fn as_bstr(&self) -> &BStr { self } } impl AsBStr for BString { fn as_bstr(&self) -> &BStr { BStr::new(self.as_slice()) } } impl AsBStr for str { fn as_bstr(&self) -> &BStr { BStr::new(self) } } impl AsBStr for String { fn as_bstr(&self) -> &BStr { BStr::new(self) } } impl AsBStr for [u8] { fn as_bstr(&self) -> &BStr { BStr::new(self) } } impl AsBStr for Vec { fn as_bstr(&self) -> &BStr { BStr::new(self) } } impl AsBStr for [u8; N] { fn as_bstr(&self) -> &BStr { BStr::new(self) } } impl AsBStr for Cow<'_, T> where T: AsBStr + ToOwned + ?Sized, { fn as_bstr(&self) -> &BStr { self.as_ref().as_bstr() } }