pub struct StrBuf<'a> { /* private fields */ }Expand description
๐งถ A borrowed UTF-8 string buffer over caller-provided bytes.
๐ text/str
It stores a valid UTF-8 prefix inside a mutable byte slice, leaving the remaining bytes as spare capacity.
This is the borrowed counterpart to fixed-capacity owned strings.
Implementationsยง
Sourceยงimpl<'a> StrBuf<'a>
impl<'a> StrBuf<'a>
Sourcepub const fn from_bytes(
buf: &'a mut [u8],
len: usize,
) -> Result<Self, InvalidText> โ
pub const fn from_bytes( buf: &'a mut [u8], len: usize, ) -> Result<Self, InvalidText> โ
Creates a buffer from the first len bytes of buf.
ยงErrors
Returns InvalidText::MismatchedCapacity if len > buf.len(),
or InvalidText::Utf8 if buf[..len] is not valid UTF-8.
Sourcepub const unsafe fn from_bytes_unchecked(buf: &'a mut [u8], len: usize) -> Self
Available on crate feature unsafe_str only.
pub const unsafe fn from_bytes_unchecked(buf: &'a mut [u8], len: usize) -> Self
unsafe_str only.Creates a buffer from the first len bytes of buf without validation.
ยงSafety
len must be <= buf.len() and buf[..len] must be valid UTF-8.
Sourcepub const fn as_mut_str(&mut self) -> &mut str โ
pub const fn as_mut_str(&mut self) -> &mut str โ
Returns the initialized UTF-8 string mutably.
Sourcepub const unsafe fn as_bytes_mut(&mut self) -> &mut [u8] โ
Available on crate feature unsafe_str only.
pub const unsafe fn as_bytes_mut(&mut self) -> &mut [u8] โ
unsafe_str only.Returns the initialized bytes mutably.
ยงSafety
The caller must preserve valid UTF-8 in self.as_bytes().
Sourcepub const fn spare_capacity_mut(&mut self) -> &mut [u8] โ
pub const fn spare_capacity_mut(&mut self) -> &mut [u8] โ
Returns the spare byte capacity.
Writing here does not change the string length. Use
set_len_checked afterwards to extend safely.
Sourcepub const fn into_str(self) -> &'a str โ
pub const fn into_str(self) -> &'a str โ
Consumes the buffer and returns the initialized UTF-8 string.
Sourcepub const fn remaining_capacity(&self) -> usize
pub const fn remaining_capacity(&self) -> usize
Returns the remaining byte capacity.
Sourcepub const fn truncate(&mut self, new_len: usize) -> Result<(), InvalidText> โ
pub const fn truncate(&mut self, new_len: usize) -> Result<(), InvalidText> โ
Truncates the string to new_len bytes.
Does nothing if new_len >= self.len().
ยงErrors
Returns InvalidText::Utf8 if new_len is not a UTF-8 boundary.
Sourcepub const fn set_len_checked(&mut self, len: usize) -> Result<(), InvalidText> โ
pub const fn set_len_checked(&mut self, len: usize) -> Result<(), InvalidText> โ
Sets the initialized length after validating buf[..len].
ยงErrors
Returns InvalidText::MismatchedCapacity if len > self.capacity(),
or InvalidText::Utf8 if buf[..len] is not valid UTF-8.
Sourcepub const unsafe fn set_len_unchecked(&mut self, len: usize)
Available on crate feature unsafe_str only.
pub const unsafe fn set_len_unchecked(&mut self, len: usize)
unsafe_str only.Sets the initialized length without validation.
ยงSafety
len must be <= self.capacity() and self.buf[..len] must be valid UTF-8.
Sourcepub const fn push(&mut self, ch: char) -> usize
pub const fn push(&mut self, ch: char) -> usize
Appends ch if it fits, returning the number of bytes written.
Returns 0 if there is not enough remaining capacity.
Sourcepub const fn try_push(&mut self, ch: char) -> Result<usize, NotEnoughSpace> โ
pub const fn try_push(&mut self, ch: char) -> Result<usize, NotEnoughSpace> โ
Appends ch, returning its UTF-8 length.
ยงErrors
Returns NotEnoughSpace if the character does not fit.
Sourcepub const fn push_str(&mut self, s: &str) -> usize
pub const fn push_str(&mut self, s: &str) -> usize
Appends the largest UTF-8 prefix of s that fits.
Returns the number of bytes written.
Sourcepub const fn try_push_str(&mut self, s: &str) -> Result<usize, usize> โ
pub const fn try_push_str(&mut self, s: &str) -> Result<usize, usize> โ
Appends the largest UTF-8 prefix of s that fits.
Returns Ok(written) if the complete string fit, otherwise
Err(written) with the valid prefix length that was written.
Sourcepub const fn try_push_str_complete(
&mut self,
s: &str,
) -> Result<usize, NotEnoughSpace> โ
pub const fn try_push_str_complete( &mut self, s: &str, ) -> Result<usize, NotEnoughSpace> โ
Appends s only if it fits completely.
ยงErrors
Returns NotEnoughSpace without modifying the buffer if s does not fit.
Trait Implementationsยง
Auto Trait Implementationsยง
impl<'a> !UnwindSafe for StrBuf<'a>
impl<'a> Freeze for StrBuf<'a>
impl<'a> RefUnwindSafe for StrBuf<'a>
impl<'a> Send for StrBuf<'a>
impl<'a> Sync for StrBuf<'a>
impl<'a> Unpin for StrBuf<'a>
impl<'a> UnsafeUnpin for StrBuf<'a>
Blanket Implementationsยง
Sourceยงimpl<T> AnyExt for T
impl<T> AnyExt for T
Sourceยงfn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId of Self using a custom hasher.Sourceยงfn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Sourceยงfn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
fn as_any_box(self: Box<Self>) -> Box<dyn Any>where
Self: Sized,
alloc only.Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> ByteSized for T
impl<T> ByteSized for T
Sourceยงconst BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Sourceยงfn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Sourceยงfn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Sourceยงimpl<T> MemExt for Twhere
T: ?Sized,
impl<T> MemExt for Twhere
T: ?Sized,
Sourceยงconst NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Sourceยงfn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Sourceยงfn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Sourceยงfn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Sourceยงfn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Sourceยงfn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true if dropping values of this type matters. Read moreSourceยงfn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self without running its destructor. Read moreSourceยงfn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Sourceยงunsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSourceยงunsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout only.T represented by the all-zero byte-pattern. Read moreSourceยงfn mem_as_bytes(&self) -> &[u8] โ
fn mem_as_bytes(&self) -> &[u8] โ
unsafe_slice only.