Struct ByteStr

#[repr(transparent)]
pub struct ByteStr(pub [u8]);

A wrapper for &[u8] representing a human-readable string that's conventionally, but not always, UTF-8.

Unlike &str, this type permits non-UTF-8 contents, making it suitable for user input, non-native filenames (as Path only supports native filenames), and other applications that need to round-trip whatever data the user provides.

For an owned, growable byte string buffer, use ByteString.

ByteStr implements Deref to [u8], so all methods available on [u8] are available on ByteStr.

Representation

A &ByteStr has the same representation as a &str. That is, a &ByteStr is a wide pointer which includes a pointer to some bytes and a length.

Trait implementations

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

The Debug implementation for ByteStr shows its bytes as a normal string, with invalid UTF-8 presented as hex escape sequences.

The Display implementation behaves as if the ByteStr were first lossily converted to a str, with invalid UTF-8 presented as the Unicode replacement character (�).

Fields

0: [u8]

Implementations

impl ByteStr

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

Creates a ByteStr slice from anything that can be converted to a byte slice.

This is a zero-cost conversion.

Example

You can create a ByteStr from a byte array, a byte slice or a string slice:

# #![feature(bstr)]
# use std::bstr::ByteStr;
let a = ByteStr::new(b"abc");
let b = ByteStr::new(&b"abc"[..]);
let c = ByteStr::new("abc");

assert_eq!(a, b);
assert_eq!(a, c);
const fn as_byte_str(&self) -> &ByteStr

Returns the same string as &ByteStr.

This method is redundant when used directly on &ByteStr, but it helps dereferencing other "container" types, for example Box<ByteStr> or Arc<ByteStr>.

const fn as_mut_byte_str(&mut self) -> &mut ByteStr

Returns the same string as &mut ByteStr.

This method is redundant when used directly on &mut ByteStr, but it helps dereferencing other "container" types, for example Box<ByteStr> or MutexGuard<ByteStr>.

const fn from_bytes(slice: &[u8]) -> &Self
const fn from_bytes_mut(slice: &mut [u8]) -> &mut Self
const fn as_bytes(&self) -> &[u8]
const fn as_bytes_mut(&mut self) -> &mut [u8]

Trait Implementations

impl AsMut<[u8]> for ByteStr

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

impl AsRef<ByteStr> for ByteStr

fn as_ref(&self) -> &ByteStr

impl AsRef<[u8]> for ByteStr

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

impl Borrow<[u8]> for ByteStr

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

impl BorrowMut<[u8]> for ByteStr

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

impl CloneToUninit for ByteStr

unsafe fn clone_to_uninit(&self, dst: *mut u8)

impl Debug for ByteStr

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

impl Deref for ByteStr

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

impl DerefMut for ByteStr

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

impl DerefPure for ByteStr

impl Display for ByteStr

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

impl Eq for ByteStr

impl Hash for ByteStr

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

impl Ord for ByteStr

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

impl PartialEq for ByteStr

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

impl PartialEq<&[u8]> for ByteStr

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

impl PartialEq<&str> for ByteStr

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

impl PartialEq<[u8]> for ByteStr

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

impl PartialEq<str> for ByteStr

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

impl PartialOrd for ByteStr

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

impl<I> Index<I> for ByteStr where I: SliceIndex<ByteStr>,

type Output = <I as SliceIndex<ByteStr>>::Output;
fn index(&self, index: I) -> &I::Output

impl<I> IndexMut<I> for ByteStr where I: SliceIndex<ByteStr>,

fn index_mut(&mut self, index: I) -> &mut I::Output

impl<const N: usize> PartialEq<&[u8; N]> for ByteStr

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

impl<const N: usize> PartialEq<[u8; N]> for ByteStr

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

Auto Trait Implementations

impl !Sized for ByteStr

impl Freeze for ByteStr

impl RefUnwindSafe for ByteStr

impl Send for ByteStr

impl Sync for ByteStr

impl Unpin for ByteStr

impl UnsafeUnpin for ByteStr

impl UnwindSafe for ByteStr

Blanket Implementations

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

type Target = T;

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

impl<T> SizeHint for ByteStr where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>