kvarn_utils::prelude

Trait ToCompactString

pub trait ToCompactString {
    // Required method
    fn try_to_compact_string(
        &self,
    ) -> Result<CompactString, ToCompactStringError>;

    // Provided method
    fn to_compact_string(&self) -> CompactString { ... }
}
Expand description

A trait for converting a value to a CompactString.

This trait is automatically implemented for any type which implements the fmt::Display trait. As such, ToCompactString shouldn’t be implemented directly: fmt::Display should be implemented instead, and you get the ToCompactString implementation for free.

Required Methods§

fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>

Fallible version of ToCompactString::to_compact_string()

This method won’t panic if the system is out-of-memory, but return a [ReserveError][crate::ReserveError]. Otherwise it behaves the same as ToCompactString::to_compact_string().

Provided Methods§

fn to_compact_string(&self) -> CompactString

Converts the given value to a CompactString.

§Panics

Panics if the system runs out of memory and it cannot hold the whole string, or if Display::fmt() returns an error.

§Examples

Basic usage:

use compact_str::ToCompactString;

let i = 5;
let five = CompactString::new("5");

assert_eq!(i.to_compact_string(), five);

Implementors§

§

impl<T> ToCompactString for T
where T: Display,