pub trait Utf8Buf: AsRef<str> + Sized {
// Required methods
fn try_split_to(&mut self, at: usize) -> Result<Self, Utf8Error>;
fn try_split_off(&mut self, at: usize) -> Result<Self, Utf8Error>;
fn try_slice(
&self,
range: impl RangeBounds<usize>,
) -> Result<Self, Utf8Error>;
// Provided methods
fn as_str(&self) -> &str { ... }
fn len(&self) -> usize { ... }
fn is_empty(&self) -> bool { ... }
fn split_to(&mut self, at: usize) -> Self { ... }
fn split_off(&mut self, at: usize) -> Self { ... }
fn slice(&self, range: impl RangeBounds<usize>) -> Self { ... }
fn range_bounds_to_start_end(
&self,
range: impl RangeBounds<usize>,
) -> Result<(usize, usize), Utf8Error> { ... }
fn validate_char_boundary(&self, at: usize) -> Result<(), Utf8Error> { ... }
}Expand description
Extension trait for UTF-8 validated buffer types.
This trait provides common functionality for buffer types that guarantee
valid UTF-8 content, such as Utf8Buffer, Utf8Bytes, and Utf8BytesMut.
Required Methods§
Sourcefn try_split_to(&mut self, at: usize) -> Result<Self, Utf8Error>
fn try_split_to(&mut self, at: usize) -> Result<Self, Utf8Error>
Tries to split the buffer at the given index.
§Errors
Returns an error if at is out of bounds or does not lie on a UTF-8 character boundary.
Provided Methods§
Sourcefn as_str(&self) -> &str
fn as_str(&self) -> &str
Returns the contents as a string slice.
§Examples
use smol_bytes::{Utf8Buf, Utf8Bytes};
let buf = Utf8Bytes::from("hello");
assert_eq!(buf.as_str(), "hello");Sourcefn len(&self) -> usize
fn len(&self) -> usize
Returns the length of the buffer in bytes.
§Examples
use smol_bytes::{Utf8Buf, Utf8Bytes};
let buf = Utf8Bytes::from("hello");
assert_eq!(buf.len(), 5);Sourcefn is_empty(&self) -> bool
fn is_empty(&self) -> bool
Returns true if the buffer has a length of 0.
§Examples
use smol_bytes::{Utf8Buf, Utf8Bytes};
let buf = Utf8Bytes::new();
assert!(buf.is_empty());Sourcefn split_to(&mut self, at: usize) -> Self
fn split_to(&mut self, at: usize) -> Self
Splits the buffer at the given index, returning the content before the split point.
§Panics
Panics if at does not lie on a UTF-8 character boundary or is out of bounds.
Sourcefn split_off(&mut self, at: usize) -> Self
fn split_off(&mut self, at: usize) -> Self
Splits the buffer at the given index, returning the content after the split point.
§Panics
Panics if at does not lie on a UTF-8 character boundary or is out of bounds.
Sourcefn slice(&self, range: impl RangeBounds<usize>) -> Self
fn slice(&self, range: impl RangeBounds<usize>) -> Self
Returns a sub-slice of the buffer.
§Panics
Panics if the range does not lie on UTF-8 character boundaries or is out of bounds.
Sourcefn range_bounds_to_start_end(
&self,
range: impl RangeBounds<usize>,
) -> Result<(usize, usize), Utf8Error>
fn range_bounds_to_start_end( &self, range: impl RangeBounds<usize>, ) -> Result<(usize, usize), Utf8Error>
Helper function to convert range bounds to start and end indices.
This is used internally by slice operations to validate and normalize ranges.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl Utf8Buf for Utf8Buffer
impl Utf8Buf for Utf8BytesMut
alloc or std only.