string_more

Trait EncodeUtf8

Source
pub trait EncodeUtf8: Sailed {
    type Buf: Default;

    // Required method
    fn encode_utf8<'a>(&'a self, buf: &'a mut Self::Buf) -> &'a str;
}
Expand description

The EncodeUtf8 trait provides a consistent interface for encoding different text-like types, making them easily interchangeable as inputs for functions requiring UTF-8 encoded data.

This trait is designed to be implemented by types that can produce a UTF-8 string representation, enabling functions in other traits (e.g., StrExt and StringExt) to accept a variety of text-like inputs, thus enhancing usability by abstracting over &str, String, and char values seamlessly.

§Examples

use string_more::EncodeUtf8;

let input = 'x';
let mut buffer: [u8; 4] = Default::default();
let encoded = input.encode_utf8(&mut buffer);
assert_eq!(encoded, "x");

Required Associated Types§

Source

type Buf: Default

A buffer type used to hold the intermediate UTF-8 encoded data.

Required Methods§

Source

fn encode_utf8<'a>(&'a self, buf: &'a mut Self::Buf) -> &'a str

Encodes self as a UTF-8 string and writes it into the provided mutable buf. Returns a &str slice from the buffer that represents the encoded UTF-8 data.

Implementations on Foreign Types§

Source§

impl EncodeUtf8 for &str

Source§

type Buf = ()

Source§

fn encode_utf8<'a>(&'a self, _: &'a mut Self::Buf) -> &'a str

Source§

impl EncodeUtf8 for char

Source§

type Buf = [u8; 4]

Source§

fn encode_utf8<'a>(&'a self, buf: &'a mut Self::Buf) -> &'a str

Source§

impl EncodeUtf8 for String

Source§

type Buf = ()

Source§

fn encode_utf8<'a>(&'a self, _: &'a mut Self::Buf) -> &'a str

Implementors§