[][src]Struct smallstr::SmallString

pub struct SmallString<A: Array<Item = u8>> { /* fields omitted */ }

A String-like container that can store a small number of bytes inline.

SmallString uses a SmallVec<[u8; N]> as its internal storage.

Methods

impl<A: Array<Item = u8>> SmallString<A>[src]

pub fn new() -> SmallString<A>[src]

Construct an empty string.

pub fn with_capacity(n: usize) -> SmallString<A>[src]

Construct an empty string with enough capacity pre-allocated to store at least n bytes.

Will create a heap allocation only if n is larger than the inline capacity.

pub fn from_str(s: &str) -> SmallString<A>[src]

Construct a SmallString by copying data from a &str.

pub fn from_string(s: String) -> SmallString<A>[src]

Construct a SmallString by using an existing allocation.

pub fn from_buf(buf: A) -> Result<SmallString<A>, FromUtf8Error<A>>[src]

Constructs a new SmallString on the stack using UTF-8 bytes.

If the provided byte array is not valid UTF-8, an error is returned.

pub unsafe fn from_buf_unchecked(buf: A) -> SmallString<A>[src]

Constructs a new SmallString on the stack using the provided byte array without checking that the array contains valid UTF-8.

Safety

This function is unsafe because it does not check that the bytes passed to it are valid UTF-8. If this constraint is violated, it may cause memory unsafety issues, as the Rust standard library functions assume that &strs are valid UTF-8.

pub fn inline_size(&self) -> usize[src]

The maximum number of bytes this string can hold inline.

pub fn len(&self) -> usize[src]

Returns the length of this string, in bytes.

pub fn is_empty(&self) -> bool[src]

Returns true if this string is empty.

pub fn capacity(&self) -> usize[src]

Returns the number of bytes this string can hold without reallocating.

pub fn spilled(&self) -> bool[src]

Returns true if the data has spilled into a separate heap-allocated buffer.

Important traits for Drain<'a>
pub fn drain(&mut self) -> Drain[src]

Empties the string and returns an iterator over its former contents.

pub fn push(&mut self, ch: char)[src]

Appends the given char to the end of this string.

Examples

use smallstr::SmallString;

let mut s: SmallString<[u8; 8]> = SmallString::from("foo");

s.push('x');

assert_eq!(s, "foox");

pub fn push_str(&mut self, s: &str)[src]

Appends the given string slice to the end of this string.

Examples

use smallstr::SmallString;

let mut s: SmallString<[u8; 8]> = SmallString::from("foo");

s.push_str("bar");

assert_eq!(s, "foobar");

pub fn pop(&mut self) -> Option<char>[src]

Removes the last character from this string and returns it.

Returns None if the string is empty.

pub fn grow(&mut self, new_cap: usize)[src]

Reallocates to set the new capacity to new_cap.

Panics

If new_cap is less than the current length.

pub fn reserve(&mut self, additional: usize)[src]

Ensures that this string's capacity is at least additional bytes larger than its length.

The capacity may be increased by more than additional bytes in order to prevent frequent reallocations.

pub fn reserve_exact(&mut self, additional: usize)[src]

Ensures that this string's capacity is additional bytes larger than its length.

pub fn shrink_to_fit(&mut self)[src]

Shrink the capacity of the string as much as possible.

When possible, this will move the data from an external heap buffer to the string's inline storage.

pub fn truncate(&mut self, len: usize)[src]

Shorten the string, keeping the first len bytes.

This does not reallocate. If you want to shrink the string's capacity, use shrink_to_fit after truncating.

Panics

If len does not lie on a char boundary.

pub fn as_str(&self) -> &str[src]

Extracts a string slice containing the entire string.

pub fn as_mut_str(&mut self) -> &mut str[src]

Extracts a string slice containing the entire string.

pub fn clear(&mut self)[src]

Removes all contents of the string.

pub fn remove(&mut self, idx: usize) -> char[src]

Removes a char from this string at a byte position and returns it.

Panics

If idx does not lie on a char boundary.

pub fn insert(&mut self, idx: usize, ch: char)[src]

Inserts a char into this string at the given byte position.

Panics

If idx does not lie on char boundaries.

pub fn insert_str(&mut self, idx: usize, s: &str)[src]

Inserts a &str into this string at the given byte position.

Panics

If idx does not lie on char boundaries.

pub unsafe fn as_mut_vec(&mut self) -> &mut SmallVec<A>[src]

Returns a mutable reference to the contents of the SmallString.

Safety

This function is unsafe because it does not check that the bytes passed to it are valid UTF-8. If this constraint is violated, it may cause memory unsafety issues, as the Rust standard library functions assume that &strs are valid UTF-8.

pub fn into_string(self) -> String[src]

Converts the SmallString into a String, without reallocating if the SmallString has already spilled onto the heap.

pub fn retain<F: FnMut(char) -> bool>(&mut self, f: F)[src]

Retains only the characters specified by the predicate.

In other words, removes all characters c such that f(c) returns false. This method operates in place and preserves the order of retained characters.

Examples

use smallstr::SmallString;

let mut s: SmallString<[u8; 16]> = SmallString::from("f_o_ob_ar");

s.retain(|c| c != '_');

assert_eq!(s, "foobar");

Trait Implementations

impl<A: Array<Item = u8>> AsMut<str> for SmallString<A>[src]

impl<A: Array<Item = u8>> AsRef<str> for SmallString<A>[src]

impl<A: Array<Item = u8>> Borrow<str> for SmallString<A>[src]

impl<A: Array<Item = u8>> BorrowMut<str> for SmallString<A>[src]

impl<A: Clone + Array<Item = u8>> Clone for SmallString<A>[src]

impl<A: Array<Item = u8>> Debug for SmallString<A>[src]

impl<A: Default + Array<Item = u8>> Default for SmallString<A>[src]

impl<A: Array<Item = u8>> Deref for SmallString<A>[src]

type Target = str

The resulting type after dereferencing.

impl<A: Array<Item = u8>> DerefMut for SmallString<A>[src]

impl<A: Array<Item = u8>> Display for SmallString<A>[src]

impl<A: Array<Item = u8>> Eq for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> Extend<&'a char> for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> Extend<&'a str> for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> Extend<Cow<'a, str>> for SmallString<A>[src]

impl<A: Array<Item = u8>> Extend<String> for SmallString<A>[src]

impl<A: Array<Item = u8>> Extend<char> for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> From<&'a str> for SmallString<A>[src]

impl<A: Array<Item = u8>> From<Box<str>> for SmallString<A>[src]

impl<A: Array<Item = u8>> From<String> for SmallString<A>[src]

impl<A: Array<Item = u8>> From<char> for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> FromIterator<&'a char> for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> FromIterator<&'a str> for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> FromIterator<Cow<'a, str>> for SmallString<A>[src]

impl<A: Array<Item = u8>> FromIterator<String> for SmallString<A>[src]

impl<A: Array<Item = u8>> FromIterator<char> for SmallString<A>[src]

impl<A: Array<Item = u8>> Hash for SmallString<A>[src]

impl<A: Array<Item = u8>> Index<Range<usize>> for SmallString<A>[src]

type Output = str

The returned type after indexing.

impl<A: Array<Item = u8>> Index<RangeFrom<usize>> for SmallString<A>[src]

type Output = str

The returned type after indexing.

impl<A: Array<Item = u8>> Index<RangeFull> for SmallString<A>[src]

type Output = str

The returned type after indexing.

impl<A: Array<Item = u8>> Index<RangeTo<usize>> for SmallString<A>[src]

type Output = str

The returned type after indexing.

impl<A: Array<Item = u8>> IndexMut<Range<usize>> for SmallString<A>[src]

impl<A: Array<Item = u8>> IndexMut<RangeFrom<usize>> for SmallString<A>[src]

impl<A: Array<Item = u8>> IndexMut<RangeFull> for SmallString<A>[src]

impl<A: Array<Item = u8>> IndexMut<RangeTo<usize>> for SmallString<A>[src]

impl<A: Array<Item = u8>> Ord for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> PartialEq<&'a str> for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> PartialEq<Cow<'a, str>> for SmallString<A>[src]

impl<A, B> PartialEq<SmallString<B>> for SmallString<A> where
    A: Array<Item = u8>,
    B: Array<Item = u8>, 
[src]

impl<'a, A: Array<Item = u8>> PartialEq<String> for SmallString<A>[src]

impl<'a, A: Array<Item = u8>> PartialEq<str> for SmallString<A>[src]

impl<A: Array<Item = u8>> PartialOrd<SmallString<A>> for SmallString<A>[src]

impl<A: Array<Item = u8>> Write for SmallString<A>[src]

Auto Trait Implementations

impl<A> Send for SmallString<A>

impl<A> Sync for SmallString<A> where
    A: Sync

impl<A> Unpin for SmallString<A> where
    A: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.