Struct TokenString

Source
#[repr(C)]
pub struct TokenString { /* private fields */ }
Expand description

A string which can hold at most MAX_LENGTH bytes (not Unicode scalar values).

This holds valid UTF-8 encoded strings only. Strings that are short enough, which need at most MAX_LENGTH_SMALL bytes, are stored in the struct itself, bigger ones use the heap.

§Invariant

Implementations§

Source§

impl TokenString

Source

pub const fn len(&self) -> usize

Return the length of the string in bytes.

This is the length of the string in bytes, not Unicode scalar values and not grapheme clusters.

Source

pub const fn is_small(&self) -> bool

Return true if the string is a “small string”, that is, it is saved in the TokenString struct itself.

If this returns false, the string is allocated on the heap.

Source

pub const fn is_empty(&self) -> bool

Return true, if this is the empty string.

Returns false else.

Source

pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self

Convert to a TokenString.

bytes must be valid UTF-8, use TokenString::try_from if you are not sure that it is valid. If the given byte slice is bigger than MAX_LENGTH, this panics.

Memory:

Allocates if and only if the length of bytes is bigger than MAX_LENGTH_SMALL.

§Panics

Panics if bytes is bigger than MAX_LENGTH.

§Safety

bytes must be valid UTF-8, if not, all bets are off - UB!

Source

pub fn from_str_unchecked(s: &str) -> Self

Convert to a TokenString.

If the given string s is bigger than MAX_LENGTH, this panics. Use TokenString::try_from for a function that does not panic. The string s must be valid UTF-8 too, but it has already been UB if it isn’t.

Memory:

Allocates if and only if the length of s is bigger than MAX_LENGTH_SMALL.

§Panics

Panics if s is bigger than MAX_LENGTH.

Source

pub fn from_string_unchecked(s: &String) -> Self

Convert to a TokenString.

If the given string s is bigger than MAX_LENGTH, this panics. Use TokenString::try_from for a function that does not panic. The string s must be valid UTF-8 too, but it has already been UB if it isn’t.

Memory:

Allocates if and only if the length of s is bigger than MAX_LENGTH_SMALL.

§Panics

Panics if s is bigger than MAX_LENGTH.

Source

pub fn as_str(&self) -> &str

Return the string as a &str.

Source

pub fn as_bytes(&self) -> &[u8]

Return the string as a byte slice.

Source

pub fn as_string(&self) -> String

Return the string as a new alloc::string::String.

Memory:

Allocates a new alloc::string::String.

Source

pub fn as_chars(&self) -> Vec<char>

Return the string as a new vector of chars.

Memory:

Allocates a new vec::Vec.

Source

pub fn get(&self, idx: u16) -> Result<u8, TkStrError>

Return the byte at index idx, check bounds.

Returns TkStrError::OutOfBounds if the index is bigger than the string’s length.

§Errors

TkStrError::OutOfBounds if idx is bigger than the string’s length.

Source

pub fn get_unchecked(&self, idx: u16) -> u8

Return the byte at index idx, don’t check bounds.

Panics if the index is bigger than the string’s length.

§Panics

if idx is bigger than the string’s length.

Source

pub fn chars(&self) -> Chars<'_>

Return an iterator over the [char]s of a string.

That is, an iterator over the Unicode scalar values of the TokenString.

Source

pub fn iter(&self) -> TokenStringIter<'_>

Get a reference iterator.

Source

pub const fn starts_ascii_uppercase(&self) -> bool

Return true, if the first byte is an uppercase ASCII character.

Source

pub const fn starts_ascii_lowercase(&self) -> bool

Return true, if the first byte is an lowercase ASCII character.

Source

pub fn is_ascii(&self) -> bool

Return true, if the string contains only ASCII characters.

Source

pub fn starts_with(&self, needle: &Self) -> bool

Return true, if the string starts with needle.

Returns true too if the string is needle.

Source

pub fn starts_with_bytes(&self, needle: &[u8]) -> bool

Return true, if the string starts with needle.

Returns true too if the string is needle.

Source

pub fn starts_with_str(&self, needle: &str) -> bool

Return true, if the string starts with needle.

Returns true too if the string is needle.

Source

pub fn ends_with(&self, needle: &Self) -> bool

Return true, if the string ends with needle.

Returns true too if the string is needle.

Source

pub fn ends_with_bytes(&self, needle: &[u8]) -> bool

Return true, if the string ends with needle.

Returns true too if the string is needle.

Source

pub fn ends_with_str(&self, needle: &str) -> bool

Return true, if the string ends with needle.

Returns true too if the string is needle.

Source

pub fn to_ascii_lowercase(&self) -> Self

Return a new string with all uppercase ASCII characters changed to lowercase.

Source

pub fn to_ascii_uppercase(&self) -> Self

Return a new string with all lowercase ASCII characters changed to uppercase.

Source

pub fn trim_ascii(&self) -> Self

Return a new string with all ASCII whitespace removed from the start and end.

Source

pub fn trim_ascii_start(&self) -> Self

Return a new string with all ASCII whitespace removed from the start.

Source

pub fn trim_ascii_end(&self) -> Self

Return a new string with all ASCII whitespace removed from the end.

Source

pub fn strip_prefix<P: Pattern>(&self, prefix: P) -> Option<Self>

Available on pattern only.

Return a new string with prefix removed from the start.

Source

pub fn strip_suffix<P>(&self, suffix: P) -> Option<Self>
where P: Pattern, for<'a> P::Searcher<'a>: ReverseSearcher<'a>,

Available on pattern only.

Return a new string with suffix removed from the end.

Source

pub fn contains<P: Pattern>(&self, pat: P) -> bool

Available on pattern only.

Return true if the string contains the pattern pat.

Returns false else.

The feature

Trait Implementations§

Source§

impl AsRef<str> for TokenString

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<str> for TokenString

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl Clone for TokenString

Source§

fn clone(&self) -> Self

Return a clone of the TokenString.

Memory:

Allocates if and only if the length of value is bigger than MAX_LENGTH_SMALL.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, const N: usize> Concat<&'a TokenString> for &'a mut Builder<'a, N>

Source§

type Output = &'a mut Builder<'a, N>

Source§

fn concat(self, s: &'a TokenString) -> Result<Self::Output, TkStrError>

Concatenate another string to the end of the builder. Read more
Source§

impl<'a, const N: usize> Concat<&'a TokenString> for Result<&'a mut Builder<'a, N>, TkStrError>

Source§

type Output = &'a mut Builder<'a, N>

Source§

fn concat(self, s: &'a TokenString) -> Result<Self::Output, TkStrError>

Concatenate another string to the end of the builder. Read more
Source§

impl Debug for TokenString

Source§

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

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

impl Default for TokenString

Source§

fn default() -> Self

Return the empty string.

Source§

impl Display for TokenString

Source§

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

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

impl Drop for TokenString

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Hash for TokenString

Source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<Idx> Index<Idx> for TokenString
where Idx: SliceIndex<str>,

Source§

type Output = <Idx as SliceIndex<str>>::Output

The returned type after indexing.
Source§

fn index(&self, index: Idx) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a TokenString

Source§

type IntoIter = TokenStringIter<'a>

Which kind of iterator are we turning this into?
Source§

type Item = u8

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for TokenString

Source§

type IntoIter = TokenStringIterOwn

Which kind of iterator are we turning this into?
Source§

type Item = u8

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Ord for TokenString

Source§

fn cmp(&self, other: &Self) -> Ordering

Compare two TokenStrings byte-wise.

This is not a sensible alphabetical comparison for anything that isn’t ASCII.

1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq<[u8]> for TokenString

Source§

fn eq(&self, other: &[u8]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<String> for TokenString

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<str> for TokenString

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for TokenString

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for TokenString

Source§

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

Compare two TokenStrings byte-wise.

This is not a sensible alphabetical comparison for anything that isn’t ASCII.

1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<&[char]> for TokenString

Source§

fn try_from(value: &[char]) -> Result<Self, Self::Error>

Try to create a TokenString from the given slice.

Return TkStrError::TooBig if the given slice is too big, greater than MAX_LENGTH.

Memory

Allocates and deallocates a temporary alloc::string::String collecting the converted bytes.

Source§

type Error = TkStrError

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

impl TryFrom<&[u8]> for TokenString

Source§

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Try to create a TokenString from the given slice.

Return TkStrError::TooBig if the given slice is too big, greater than MAX_LENGTH. Return TkStrError::UnicodeError

Memory:

Allocates if and only if the length of value is bigger than MAX_LENGTH_SMALL.

Source§

type Error = TkStrError

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

impl TryFrom<&String> for TokenString

Source§

fn try_from(value: &String) -> Result<Self, Self::Error>

Create a TokenString from a &alloc::string::String.

Return TkStrError::TooBig if the argument is greater than MAX_LENGTH.

Memory:

Allocates if and only if the length of value is bigger than MAX_LENGTH_SMALL.

Source§

type Error = TkStrError

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

impl TryFrom<&str> for TokenString

Source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Create a TokenString from a &str.

Return TkStrError::TooBig if the argument is greater than MAX_LENGTH.

Memory:

Allocates if and only if the length of value is bigger than MAX_LENGTH_SMALL.

Source§

type Error = TkStrError

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

impl TryFrom<String> for TokenString

Source§

fn try_from(value: String) -> Result<Self, Self::Error>

Create a TokenString from a alloc::string::String.

Return TkStrError::TooBig if the argument is greater than MAX_LENGTH.

Memory:

Allocates if and only if the length of value is bigger than MAX_LENGTH_SMALL.

Source§

type Error = TkStrError

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

impl Eq for TokenString

Source§

impl Send for TokenString

Source§

impl Sync for TokenString

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> CloneToUninit for T
where T: Clone,

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.