Skip to main content

Utf8Buf

Trait Utf8Buf 

Source
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§

Source

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.

Source

fn try_split_off(&mut self, at: usize) -> Result<Self, Utf8Error>

Tries to split the buffer at the given index, returning the tail.

§Errors

Returns an error if at is out of bounds or does not lie on a UTF-8 character boundary.

Source

fn try_slice(&self, range: impl RangeBounds<usize>) -> Result<Self, Utf8Error>

Tries to return a sub-slice of the buffer.

§Errors

Returns an error if the range is out of bounds or does not lie on UTF-8 character boundaries.

Provided Methods§

Source

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");
Source

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);
Source

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());
Source

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.

Source

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.

Source

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.

Source

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.

Source

fn validate_char_boundary(&self, at: usize) -> Result<(), Utf8Error>

Validates that an index is within bounds and on a character boundary.

This is used internally by split operations.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Utf8Buf for Utf8Buffer

Source§

impl Utf8Buf for Utf8BytesMut

Available on crate features alloc or std only.