Trait ByteVec
trait ByteVec: private::Sealed
A trait that extends Vec<u8> with string oriented methods.
Note that when using the constructor methods, such as
ByteVec::from_slice, one should actually call them using the concrete
type. For example:
use ;
let s = Vecfrom_slice; // NOT ByteVec::from_slice("...")
assert_eq!;
This trait is sealed and cannot be implemented outside of bstr.
Provided Methods
fn from_slice<B: AsRef<[u8]>>(bytes: B) -> Vec<u8>Create a new owned byte string from the given byte slice.
Examples
Basic usage:
use ; let s = Vecfrom_slice; assert_eq!;fn from_os_string(os_str: OsString) -> Result<Vec<u8>, OsString>Create a new byte string from an owned OS string.
When the underlying bytes of OS strings are accessible, then this always succeeds and is zero cost. Otherwise, this returns the given
OsStringif it is not valid UTF-8.Examples
Basic usage:
use OsString; use ; let os_str = from; let bs = Vecfrom_os_string.expect; assert_eq!;fn from_os_str_lossy(os_str: &OsStr) -> Cow<'_, [u8]>Lossily create a new byte string from an OS string slice.
When the underlying bytes of OS strings are accessible, then this is zero cost and always returns a slice. Otherwise, a UTF-8 check is performed and if the given OS string is not valid UTF-8, then it is lossily decoded into valid UTF-8 (with invalid bytes replaced by the Unicode replacement codepoint).
Examples
Basic usage:
use OsStr; use ; let os_str = new; let bs = Vecfrom_os_str_lossy; assert_eq!;fn from_path_buf(path: PathBuf) -> Result<Vec<u8>, PathBuf>Create a new byte string from an owned file path.
When the underlying bytes of paths are accessible, then this always succeeds and is zero cost. Otherwise, this returns the given
PathBufif it is not valid UTF-8.Examples
Basic usage:
use PathBuf; use ; let path = from; let bs = Vecfrom_path_buf.expect; assert_eq!;fn from_path_lossy(path: &Path) -> Cow<'_, [u8]>Lossily create a new byte string from a file path.
When the underlying bytes of paths are accessible, then this is zero cost and always returns a slice. Otherwise, a UTF-8 check is performed and if the given path is not valid UTF-8, then it is lossily decoded into valid UTF-8 (with invalid bytes replaced by the Unicode replacement codepoint).
Examples
Basic usage:
use Path; use ; let path = new; let bs = Vecfrom_path_lossy; assert_eq!;fn unescape_bytes<S: AsRef<str>>(escaped: S) -> Vec<u8>Unescapes the given string into its raw bytes.
This looks for the escape sequences
\xNN,\0,\r,\n,\tand\and translates them into their corresponding unescaped form.Incomplete escape sequences or things that look like escape sequences but are not (for example,
\ior\xYZ) are passed through literally.This is the dual of
ByteSlice::escape_bytes.Note that the zero or NUL byte may be represented as either
\0or\x00. Both will be unescaped into the zero byte.Examples
This shows basic usage:
#This shows some examples of how incomplete or "incorrect" escape sequences get passed through literally.
#fn push_byte(self: &mut Self, byte: u8)Appends the given byte to the end of this byte string.
Note that this is equivalent to the generic
Vec::pushmethod. This method is provided to permit callers to explicitly differentiate between pushing bytes, codepoints and strings.Examples
Basic usage:
use ByteVec; let mut s = from; s.push_byte; s.push_byte; s.push_byte; assert_eq!;fn push_char(self: &mut Self, ch: char)Appends the given
charto the end of this byte string.Examples
Basic usage:
use ByteVec; let mut s = from; s.push_char; s.push_char; s.push_char; assert_eq!;fn push_str<B: AsRef<[u8]>>(self: &mut Self, bytes: B)Appends the given slice to the end of this byte string. This accepts any type that be converted to a
&[u8]. This includes, but is not limited to,&str,&BStr, and of course,&[u8]itself.Examples
Basic usage:
use ByteVec; let mut s = from; s.push_str; assert_eq!;fn into_string(self: Self) -> Result<String, FromUtf8Error> where Self: SizedConverts a
Vec<u8>into aStringif and only if this byte string is valid UTF-8.If it is not valid UTF-8, then a
FromUtf8Erroris returned. (This error can be used to examine why UTF-8 validation failed, or to regain the original byte string.)Examples
Basic usage:
use ByteVec; let bytes = Vecfrom; let string = bytes.into_string.unwrap; assert_eq!;If this byte string is not valid UTF-8, then an error will be returned. That error can then be used to inspect the location at which invalid UTF-8 was found, or to regain the original byte string:
use ; let bytes = Vecfrom_slice; let err = bytes.into_string.unwrap_err; assert_eq!; assert_eq!; // At no point in this example is an allocation performed. let bytes = Vecfrom; assert_eq!;fn into_string_lossy(self: Self) -> String where Self: SizedLossily converts a
Vec<u8>into aString. If this byte string contains invalid UTF-8, then the invalid bytes are replaced with the Unicode replacement codepoint.Examples
Basic usage:
use ByteVec; let bytes = Vecfrom_slice; let string = bytes.into_string_lossy; assert_eq!;unsafe fn into_string_unchecked(self: Self) -> String where Self: SizedUnsafely convert this byte string into a
String, without checking for valid UTF-8.Safety
Callers must ensure that this byte string is valid UTF-8 before calling this method. Converting a byte string into a
Stringthat is not valid UTF-8 is considered undefined behavior.This routine is useful in performance sensitive contexts where the UTF-8 validity of the byte string is already known and it is undesirable to pay the cost of an additional UTF-8 validation check that
into_stringperforms.Examples
Basic usage:
use ByteVec; // SAFETY: This is safe because string literals are guaranteed to be // valid UTF-8 by the Rust compiler. let s = unsafe ; assert_eq!;fn into_os_string(self: Self) -> Result<OsString, FromUtf8Error> where Self: SizedConverts this byte string into an OS string, in place.
When OS strings can be constructed from arbitrary byte sequences, this always succeeds and is zero cost. Otherwise, if this byte string is not valid UTF-8, then an error (with the original byte string) is returned.
Examples
Basic usage:
use OsStr; use ByteVec; let bs = Vecfrom; let os_str = bs.into_os_string.expect; assert_eq!;fn into_os_string_lossy(self: Self) -> OsString where Self: SizedLossily converts this byte string into an OS string, in place.
When OS strings can be constructed from arbitrary byte sequences, this is zero cost and always returns a slice. Otherwise, this will perform a UTF-8 check and lossily convert this byte string into valid UTF-8 using the Unicode replacement codepoint.
Note that this can prevent the correct roundtripping of file paths when the representation of
OsStringis opaque.Examples
Basic usage:
use ByteVec; let bs = Vecfrom_slice; let os_str = bs.into_os_string_lossy; assert_eq!;fn into_path_buf(self: Self) -> Result<PathBuf, FromUtf8Error> where Self: SizedConverts this byte string into an owned file path, in place.
When paths can be constructed from arbitrary byte sequences, this always succeeds and is zero cost. Otherwise, if this byte string is not valid UTF-8, then an error (with the original byte string) is returned.
Examples
Basic usage:
use ByteVec; let bs = Vecfrom; let path = bs.into_path_buf.expect; assert_eq!;fn into_path_buf_lossy(self: Self) -> PathBuf where Self: SizedLossily converts this byte string into an owned file path, in place.
When paths can be constructed from arbitrary byte sequences, this is zero cost and always returns a slice. Otherwise, this will perform a UTF-8 check and lossily convert this byte string into valid UTF-8 using the Unicode replacement codepoint.
Note that this can prevent the correct roundtripping of file paths when the representation of
PathBufis opaque.Examples
Basic usage:
use ByteVec; let bs = Vecfrom_slice; let path = bs.into_path_buf_lossy; assert_eq!;fn pop_byte(self: &mut Self) -> Option<u8>Removes the last byte from this
Vec<u8>and returns it.If this byte string is empty, then
Noneis returned.If the last codepoint in this byte string is not ASCII, then removing the last byte could make this byte string contain invalid UTF-8.
Note that this is equivalent to the generic
Vec::popmethod. This method is provided to permit callers to explicitly differentiate between popping bytes and codepoints.Examples
Basic usage:
use ByteVec; let mut s = Vecfrom; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn pop_char(self: &mut Self) -> Option<char>Removes the last codepoint from this
Vec<u8>and returns it.If this byte string is empty, then
Noneis returned. If the last bytes of this byte string do not correspond to a valid UTF-8 code unit sequence, then the Unicode replacement codepoint is yielded instead in accordance with the replacement codepoint substitution policy.Examples
Basic usage:
use ByteVec; let mut s = Vecfrom; assert_eq!; assert_eq!; assert_eq!; assert_eq!;This shows the replacement codepoint substitution policy. Note that the first pop yields a replacement codepoint but actually removes two bytes. This is in contrast with subsequent pops when encountering
\xFFsince\xFFis never a valid prefix for any valid UTF-8 code unit sequence.use ByteVec; let mut s = Vecfrom_slice; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn remove_char(self: &mut Self, at: usize) -> charRemoves a
charfrom thisVec<u8>at the given byte position and returns it.If the bytes at the given position do not lead to a valid UTF-8 code unit sequence, then a replacement codepoint is returned instead.
Panics
Panics if
atis larger than or equal to this byte string's length.Examples
Basic usage:
use ByteVec; let mut s = Vecfrom; assert_eq!; assert_eq!;This example shows how the Unicode replacement codepoint policy is used:
use ByteVec; let mut s = Vecfrom_slice; assert_eq!; assert_eq!;fn insert_char(self: &mut Self, at: usize, ch: char)Inserts the given codepoint into this
Vec<u8>at a particular byte position.This is an
O(n)operation as it may copy a number of elements in this byte string proportional to its length.Panics
Panics if
atis larger than the byte string's length.Examples
Basic usage:
use ByteVec; let mut s = Vecfrom; s.insert_char; assert_eq!;fn insert_str<B: AsRef<[u8]>>(self: &mut Self, at: usize, bytes: B)Inserts the given byte string into this byte string at a particular byte position.
This is an
O(n)operation as it may copy a number of elements in this byte string proportional to its length.The given byte string may be any type that can be cheaply converted into a
&[u8]. This includes, but is not limited to,&strand&[u8].Panics
Panics if
atis larger than the byte string's length.Examples
Basic usage:
use ByteVec; let mut s = Vecfrom; s.insert_str; assert_eq!;fn replace_range<R, B>(self: &mut Self, range: R, replace_with: B) where R: RangeBounds<usize>, B: AsRef<[u8]>Removes the specified range in this byte string and replaces it with the given bytes. The given bytes do not need to have the same length as the range provided.
Panics
Panics if the given range is invalid.
Examples
Basic usage:
use ByteVec; let mut s = Vecfrom; s.replace_range; assert_eq!;fn drain_bytes<R>(self: &mut Self, range: R) -> DrainBytes<'_> where R: RangeBounds<usize>Creates a draining iterator that removes the specified range in this
Vec<u8>and yields each of the removed bytes.Note that the elements specified by the given range are removed regardless of whether the returned iterator is fully exhausted.
Also note that is is unspecified how many bytes are removed from the
Vec<u8>if theDrainBytesiterator is leaked.Panics
Panics if the given range is not valid.
Examples
Basic usage:
use ByteVec; let mut s = Vecfrom; assert_eq!;
Implementors
impl ByteVec for Vec<u8>