Skip to main content

Utf8BufMut

Trait Utf8BufMut 

Source
pub trait Utf8BufMut: Utf8Buf + AsMut<str> {
    type CapacityError: Error;

    // Required methods
    fn try_truncate(&mut self, new_len: usize) -> Result<(), Utf8IndexError>;
    fn try_push(&mut self, ch: char) -> Result<(), Self::CapacityError>;
    fn try_push_str(&mut self, s: &str) -> Result<(), Self::CapacityError>;
    fn clear(&mut self);

    // Provided methods
    fn as_mut_str(&mut self) -> &mut str { ... }
    fn push(&mut self, ch: char) { ... }
    fn push_str(&mut self, s: &str) { ... }
}
Expand description

Extension trait for mutable UTF-8 validated buffer types.

This trait provides common mutable operations for buffer types that can be modified while maintaining UTF-8 validity.

Required Associated Types§

Source

type CapacityError: Error

The error returned when an append cannot fit.

Required Methods§

Source

fn try_truncate(&mut self, new_len: usize) -> Result<(), Utf8IndexError>

Truncates at a UTF-8 character boundary.

A length at or beyond the current byte length is a no-op. On failure the buffer is unchanged.

Source

fn try_push(&mut self, ch: char) -> Result<(), Self::CapacityError>

Attempts to append a character.

Source

fn try_push_str(&mut self, s: &str) -> Result<(), Self::CapacityError>

Attempts to append a string slice.

Source

fn clear(&mut self)

Clears the buffer, removing all contents.

§Examples
use smol_bytes::{Utf8BufMut, Utf8BytesMut};

let mut buf = Utf8BytesMut::from("hello");
buf.clear();
assert!(buf.is_empty());

Provided Methods§

Source

fn as_mut_str(&mut self) -> &mut str

Returns the contents as a mutable string slice.

Source

fn push(&mut self, ch: char)

Appends a character to the buffer.

§Examples
use smol_bytes::{Utf8BufMut, Utf8BytesMut};

let mut buf = Utf8BytesMut::new();
buf.push('a');
buf.push('b');
assert_eq!(buf.as_str(), "ab");
Source

fn push_str(&mut self, s: &str)

Appends a string slice to the buffer.

§Examples
use smol_bytes::{Utf8BufMut, Utf8BytesMut};

let mut buf = Utf8BytesMut::new();
buf.push_str("hello");
assert_eq!(buf.as_str(), "hello");

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl Utf8BufMut for Utf8Buffer

Source§

impl Utf8BufMut for Utf8BytesMut

Available on crate features alloc or std only.