Struct SmallStr

pub struct SmallStr<const ARRAY_CAPACITY_MAX: usize> { /* private fields */ }

A "small" string that usually lives in an array.

When the string is too big, it spills over into the heap. Generally speaking, this should only be used when spilling into the heap is exceptionally rare. For example, for representing pathological user data (like very long time zone abbreviations).

The ARRAY_CAPACITY_MAX parameter defines the maximum length of a string that will fit in an array backed storage. This number cannot be any bigger than 255.

In core-only environments, this always uses array backed storage or static data. Static data is only used when SmallStr::from("some static string") is used to construct a SmallStr. This is useful when constructing static data structures.

Implementations

impl<const ARRAY_CAPACITY_MAX: usize> SmallStr<ARRAY_CAPACITY_MAX>

fn new(s: &str) -> Option<SmallStr<ARRAY_CAPACITY_MAX>>

Creates a new array-or-heap backed string depending on the length.

In environments with dynamic memory allocation, this never returns None. In core-only environments, this may return None when the string length exceeds the maximum capacity.

fn new_or_heap(s: &str) -> SmallStr<ARRAY_CAPACITY_MAX>

Like new but always spills to the heap when the given string does not fit in this string's fixed capacity.

This is only available when the alloc crate feature is enabled.

const fn try_array(s: &str) -> Option<SmallStr<ARRAY_CAPACITY_MAX>>

Like SmallStr::new, but this never returns a string on the heap.

This is useful when you want a SmallStr that is guaranteed to never be on the heap. For example, when constructing static data.

If the string exceeds the maximum capacity, then None is returned.

const fn array(s: &str) -> SmallStr<ARRAY_CAPACITY_MAX>

Like SmallStr::new, but this never returns a string on the heap.

This is useful when you want a SmallStr that is guaranteed to never be on the heap. For example, when constructing static data.

Panics

If the string exceeds the maximum capacity, then this routine panics.

const fn statik(s: &'static str) -> SmallStr<ARRAY_CAPACITY_MAX>

Like SmallStr::new, but only accepts a static string.

This never fails.

const fn array_capacity_max() -> usize

Returns the maximum capacity for this small string's array storage.

const fn as_str(&self) -> &str

Returns this small string as a string slice.

Trait Implementations

impl<const ARRAY_CAPACITY_MAX: usize> AsRef<str> for SmallStr<ARRAY_CAPACITY_MAX>

fn as_ref(&self) -> &str

impl<const ARRAY_CAPACITY_MAX: usize> Clone for SmallStr<ARRAY_CAPACITY_MAX>

fn clone(&self) -> SmallStr<ARRAY_CAPACITY_MAX>

impl<const ARRAY_CAPACITY_MAX: usize> Debug for SmallStr<ARRAY_CAPACITY_MAX>

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

impl<const ARRAY_CAPACITY_MAX: usize> Deref for SmallStr<ARRAY_CAPACITY_MAX>

type Target = str;
fn deref(&self) -> &str

impl<const ARRAY_CAPACITY_MAX: usize> Display for SmallStr<ARRAY_CAPACITY_MAX>

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

impl<const ARRAY_CAPACITY_MAX: usize> Eq for SmallStr<ARRAY_CAPACITY_MAX>

impl<const ARRAY_CAPACITY_MAX: usize> From<&'static str> for SmallStr<ARRAY_CAPACITY_MAX>

fn from(s: &'static str) -> SmallStr<ARRAY_CAPACITY_MAX>

impl<const ARRAY_CAPACITY_MAX: usize> From<ArrayStr<ARRAY_CAPACITY_MAX>> for SmallStr<ARRAY_CAPACITY_MAX>

fn from(s: ArrayStr<ARRAY_CAPACITY_MAX>) -> SmallStr<ARRAY_CAPACITY_MAX>

impl<const ARRAY_CAPACITY_MAX: usize> From<Box<str>> for SmallStr<ARRAY_CAPACITY_MAX>

fn from(s: Box<str>) -> SmallStr<ARRAY_CAPACITY_MAX>

impl<const ARRAY_CAPACITY_MAX: usize> Hash for SmallStr<ARRAY_CAPACITY_MAX>

fn hash<__H: Hasher>(&self, state: &mut __H)

impl<const ARRAY_CAPACITY_MAX: usize> Ord for SmallStr<ARRAY_CAPACITY_MAX>

fn cmp(&self, other: &SmallStr<ARRAY_CAPACITY_MAX>) -> Ordering

impl<const ARRAY_CAPACITY_MAX: usize> PartialEq for SmallStr<ARRAY_CAPACITY_MAX>

fn eq(&self, other: &SmallStr<ARRAY_CAPACITY_MAX>) -> bool

impl<const ARRAY_CAPACITY_MAX: usize> PartialEq<&str> for SmallStr<ARRAY_CAPACITY_MAX>

fn eq(&self, rhs: &&str) -> bool

impl<const ARRAY_CAPACITY_MAX: usize> PartialEq<str> for SmallStr<ARRAY_CAPACITY_MAX>

fn eq(&self, rhs: &str) -> bool

impl<const ARRAY_CAPACITY_MAX: usize> PartialOrd for SmallStr<ARRAY_CAPACITY_MAX>

fn partial_cmp(&self, other: &SmallStr<ARRAY_CAPACITY_MAX>) -> Option<Ordering>

impl<const ARRAY_CAPACITY_MAX: usize> PartialOrd<&str> for SmallStr<ARRAY_CAPACITY_MAX>

fn partial_cmp(&self, rhs: &&str) -> Option<Ordering>

impl<const ARRAY_CAPACITY_MAX: usize> PartialOrd<str> for SmallStr<ARRAY_CAPACITY_MAX>

fn partial_cmp(&self, rhs: &str) -> Option<Ordering>

impl<const ARRAY_CAPACITY_MAX: usize> StructuralPartialEq for SmallStr<ARRAY_CAPACITY_MAX>

Auto Trait Implementations

impl<const ARRAY_CAPACITY_MAX: usize> Freeze for SmallStr<ARRAY_CAPACITY_MAX> where SmallStrKind<ARRAY_CAPACITY_MAX>: Freeze,

impl<const ARRAY_CAPACITY_MAX: usize> RefUnwindSafe for SmallStr<ARRAY_CAPACITY_MAX> where SmallStrKind<ARRAY_CAPACITY_MAX>: RefUnwindSafe,

impl<const ARRAY_CAPACITY_MAX: usize> Send for SmallStr<ARRAY_CAPACITY_MAX> where SmallStrKind<ARRAY_CAPACITY_MAX>: Send,

impl<const ARRAY_CAPACITY_MAX: usize> Sync for SmallStr<ARRAY_CAPACITY_MAX> where SmallStrKind<ARRAY_CAPACITY_MAX>: Sync,

impl<const ARRAY_CAPACITY_MAX: usize> Unpin for SmallStr<ARRAY_CAPACITY_MAX> where SmallStrKind<ARRAY_CAPACITY_MAX>: Unpin,

impl<const ARRAY_CAPACITY_MAX: usize> UnsafeUnpin for SmallStr<ARRAY_CAPACITY_MAX> where SmallStrKind<ARRAY_CAPACITY_MAX>: UnsafeUnpin,

impl<const ARRAY_CAPACITY_MAX: usize> UnwindSafe for SmallStr<ARRAY_CAPACITY_MAX> where SmallStrKind<ARRAY_CAPACITY_MAX>: UnwindSafe,

Blanket Implementations

impl<P, T> Receiver for SmallStr<ARRAY_CAPACITY_MAX> where P: Deref<Target = T> + ?Sized, T: ?Sized,

type Target = T;

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

impl<T> CloneToUninit for SmallStr<ARRAY_CAPACITY_MAX> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for SmallStr<ARRAY_CAPACITY_MAX>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for SmallStr<ARRAY_CAPACITY_MAX> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> ToString for SmallStr<ARRAY_CAPACITY_MAX> where T: Display + ?Sized,

fn to_string(&self) -> String

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

fn into(self) -> U

Calls U::from(self).

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

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

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

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

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