Struct BStr

#[repr(transparent)]
pub struct BStr { /* private fields */ }

A wrapper for &[u8] that provides convenient string oriented trait impls.

If you need ownership or a growable byte string buffer, then use BString.

Using a &BStr is just like using a &[u8], since BStr implements Deref to [u8]. So all methods available on [u8] are also available on BStr.

Representation

A &BStr has the same representation as a &str. That is, a &BStr is a fat pointer which consists of a pointer to some bytes and a length.

Trait implementations

The BStr type has a number of trait implementations, and in particular, defines equality and ordinal comparisons between &BStr, &str and &[u8] for convenience.

The Debug implementation for BStr shows its bytes as a normal string. For invalid UTF-8, hex escape sequences are used.

The Display implementation behaves as if BStr were first lossily converted to a str. Invalid UTF-8 bytes are substituted with the Unicode replacement codepoint, which looks like this: �.

Implementations

impl BStr

fn new<B: ?Sized + AsRef<[u8]>>(bytes: &B) -> &BStr

Directly creates a BStr slice from anything that can be converted to a byte slice.

This is very similar to the B function, except this returns a &BStr instead of a &[u8].

This is a cost-free conversion.

Example

You can create BStr's from byte arrays, byte slices or even string slices:

use bstr::BStr;

let a = BStr::new(b"abc");
let b = BStr::new(&b"abc"[..]);
let c = BStr::new("abc");

assert_eq!(a, b);
assert_eq!(a, c);

Trait Implementations

impl AsMut<[u8]> for BStr

fn as_mut(&mut self) -> &mut [u8]

impl AsRef<BStr> for BStr

fn as_ref(&self) -> &BStr

impl AsRef<[u8]> for BStr

fn as_ref(&self) -> &[u8]

impl Borrow<[u8]> for BStr

fn borrow(&self) -> &[u8]

impl BorrowMut<[u8]> for BStr

fn borrow_mut(&mut self) -> &mut [u8]

impl Debug for BStr

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Deref for BStr

type Target = [u8];
fn deref(&self) -> &[u8]

impl DerefMut for BStr

fn deref_mut(&mut self) -> &mut [u8]

impl Display for BStr

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Eq for BStr

impl Hash for BStr

fn hash<H: Hasher>(&self, state: &mut H)

impl Index<Range<usize>> for BStr

type Output = BStr;
fn index(&self, r: Range<usize>) -> &BStr

impl Index<RangeFrom<usize>> for BStr

type Output = BStr;
fn index(&self, r: RangeFrom<usize>) -> &BStr

impl Index<RangeFull> for BStr

type Output = BStr;
fn index(&self, RangeFull) -> &BStr

impl Index<RangeInclusive<usize>> for BStr

type Output = BStr;
fn index(&self, r: RangeInclusive<usize>) -> &BStr

impl Index<RangeTo<usize>> for BStr

type Output = BStr;
fn index(&self, r: RangeTo<usize>) -> &BStr

impl Index<RangeToInclusive<usize>> for BStr

type Output = BStr;
fn index(&self, r: RangeToInclusive<usize>) -> &BStr

impl Index<usize> for BStr

type Output = u8;
fn index(&self, idx: usize) -> &u8

impl IndexMut<Range<usize>> for BStr

fn index_mut(&mut self, r: Range<usize>) -> &mut BStr

impl IndexMut<RangeFrom<usize>> for BStr

fn index_mut(&mut self, r: RangeFrom<usize>) -> &mut BStr

impl IndexMut<RangeFull> for BStr

fn index_mut(&mut self, RangeFull) -> &mut BStr

impl IndexMut<RangeInclusive<usize>> for BStr

fn index_mut(&mut self, r: RangeInclusive<usize>) -> &mut BStr

impl IndexMut<RangeTo<usize>> for BStr

fn index_mut(&mut self, r: RangeTo<usize>) -> &mut BStr

impl IndexMut<RangeToInclusive<usize>> for BStr

fn index_mut(&mut self, r: RangeToInclusive<usize>) -> &mut BStr

impl IndexMut<usize> for BStr

fn index_mut(&mut self, idx: usize) -> &mut u8

impl Ord for BStr

fn cmp(&self, other: &BStr) -> Ordering

impl PartialEq for BStr

fn eq(&self, other: &BStr) -> bool

impl PartialOrd for BStr

fn partial_cmp(&self, other: &BStr) -> Option<Ordering>

impl ToOwned for BStr

type Owned = BString;
fn to_owned(&self) -> BString

impl<'a> PartialEq<&'a [u8]> for BStr

fn eq(&self, other: &&'a [u8]) -> bool

impl<'a> PartialEq<&'a str> for BStr

fn eq(&self, other: &&'a str) -> bool

impl<'a> PartialEq<BString> for BStr

fn eq(&self, other: &BString) -> bool

impl<'a> PartialEq<String> for BStr

fn eq(&self, other: &String) -> bool

impl<'a> PartialEq<Vec<u8>> for BStr

fn eq(&self, other: &Vec<u8>) -> bool

impl<'a> PartialEq<[u8]> for BStr

fn eq(&self, other: &[u8]) -> bool

impl<'a> PartialEq<str> for BStr

fn eq(&self, other: &str) -> bool

impl<'a> PartialOrd<&'a [u8]> for BStr

fn partial_cmp(&self, other: &&'a [u8]) -> Option<Ordering>

impl<'a> PartialOrd<&'a str> for BStr

fn partial_cmp(&self, other: &&'a str) -> Option<Ordering>

impl<'a> PartialOrd<BString> for BStr

fn partial_cmp(&self, other: &BString) -> Option<Ordering>

impl<'a> PartialOrd<String> for BStr

fn partial_cmp(&self, other: &String) -> Option<Ordering>

impl<'a> PartialOrd<Vec<u8>> for BStr

fn partial_cmp(&self, other: &Vec<u8>) -> Option<Ordering>

impl<'a> PartialOrd<[u8]> for BStr

fn partial_cmp(&self, other: &[u8]) -> Option<Ordering>

impl<'a> PartialOrd<str> for BStr

fn partial_cmp(&self, other: &str) -> Option<Ordering>

impl<'a, const N: usize> PartialEq<&'a [u8; N]> for BStr

fn eq(&self, other: &&'a [u8; N]) -> bool

impl<'a, const N: usize> PartialEq<[u8; N]> for BStr

fn eq(&self, other: &[u8; N]) -> bool

impl<'a, const N: usize> PartialOrd<&'a [u8; N]> for BStr

fn partial_cmp(&self, other: &&'a [u8; N]) -> Option<Ordering>

impl<'a, const N: usize> PartialOrd<[u8; N]> for BStr

fn partial_cmp(&self, other: &[u8; N]) -> Option<Ordering>

Auto Trait Implementations

impl !Sized for BStr

impl Freeze for BStr

impl RefUnwindSafe for BStr

impl Send for BStr

impl Sync for BStr

impl Unpin for BStr

impl UnsafeUnpin for BStr

impl UnwindSafe for BStr

Blanket Implementations

impl<P, T> Receiver for BStr where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

impl<T> Any for BStr where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for BStr where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for BStr where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> ToString for BStr where T: Display + ?Sized,

fn to_string(&self) -> String