smartstring

Struct Compact

Source
pub struct Compact;
Expand description

A compact string representation equal to String in size with guaranteed inlining.

This representation relies on pointer alignment to be able to store a discriminant bit in its inline form that will never be present in its String form, thus giving us 24 bytes on 64-bit architectures, and 12 bytes on 32-bit, minus one bit, to encode our inline string. It uses the rest of the discriminant bit’s byte to encode the string length, and the remaining bytes (23 or 11 depending on arch) to store the string data. When the available space is exceeded, it swaps itself out with a String containing its previous contents, relying on the discriminant bit in the String’s pointer to be unset, so we can store the String safely without taking up any extra space for a discriminant.

This performs generally as well as String on all ops on boxed strings, and better than Strings on inlined strings.

Trait Implementations§

Source§

impl Debug for Compact

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl SmartStringMode for Compact

Source§

const DEALLOC: bool = true

A constant to decide whether to turn a wrapped string back into an inlined string whenever possible (true) or leave it as a wrapped string once wrapping has occurred (false).
Source§

type InlineArray = [u8; 23]

The inline string type for this layout.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.