Trait ToStr

Source
pub unsafe trait ToStr {
    const TEXT_SIZE: usize;

    // Required method
    fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str;

    // Provided method
    fn to_str_if<'a>(&self, buffer: &'a mut [u8]) -> Option<&'a str> { ... }
}
Expand description

Describes conversion to string

This trait is unsafe due to following requirements:

  • Implementation must never read buffer, unless it was already written by it;
  • It writes from the end of buffer (necessary only when you use Buffer).

Required Associated Constants§

Source

const TEXT_SIZE: usize

Max size in bytes to hold the string

Implementation MUST guarantee that this size of buffer is enough to fit any possible textual representation

Required Methods§

Source

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Writes textual representation to the buffer

Returns str stored in the provided buffer

Can panic, if buffer is not sufficient. Or write only partially

Implementation is allowed to write any part of the buffer. It is not allowed to read it, unless it was written already.

§Safety:

Debug builds must never invoke UB when calling this function.

UB in release mode is fine if one wants to write efficient code.

Provided Methods§

Source

fn to_str_if<'a>(&self, buffer: &'a mut [u8]) -> Option<&'a str>

Performs textual conversion by writing to the buffer, if possible.

If not possible MUST return None

By default returns None if buffer size is below TEXT_SIZE Otherwise calls to_str() while passing buffer as it is

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ToStr for i8

Source§

const TEXT_SIZE: usize = 4usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for i16

Source§

const TEXT_SIZE: usize = 6usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for i32

Source§

const TEXT_SIZE: usize = 11usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for i64

Source§

const TEXT_SIZE: usize = 21usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for i128

Source§

const TEXT_SIZE: usize = 40usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for isize

Source§

const TEXT_SIZE: usize = 11usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for u8

Source§

const TEXT_SIZE: usize = 3usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for u16

Source§

const TEXT_SIZE: usize = 5usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for u32

Source§

const TEXT_SIZE: usize = 10usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for u64

Source§

const TEXT_SIZE: usize = 20usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for u128

Source§

const TEXT_SIZE: usize = 39usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl ToStr for usize

Source§

const TEXT_SIZE: usize = 10usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl<'a, T: ?Sized + ToStr> ToStr for &'a T

Source§

const TEXT_SIZE: usize = T::TEXT_SIZE

Source§

fn to_str<'b>(&self, buffer: &'b mut [u8]) -> &'b str

Source§

impl<'a, T: ?Sized + ToStr> ToStr for &'a mut T

Source§

const TEXT_SIZE: usize = T::TEXT_SIZE

Source§

fn to_str<'b>(&self, buffer: &'b mut [u8]) -> &'b str

Source§

impl<T> ToStr for *const T

Source§

const TEXT_SIZE: usize = 12usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl<T> ToStr for *mut T

Source§

const TEXT_SIZE: usize = 12usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl<T> ToStr for NonNull<T>

Source§

const TEXT_SIZE: usize = 12usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Source§

impl<T> ToStr for AtomicPtr<T>

Source§

const TEXT_SIZE: usize = 12usize

Source§

fn to_str<'a>(&self, buffer: &'a mut [u8]) -> &'a str

Implementors§