#[repr(transparent)]
pub struct AsciiString { /* private fields */ }
Expand description

A growable string stored as an ASCII encoded buffer.

Implementations

Creates a new, empty ASCII string buffer without allocating.

Examples
let mut s = AsciiString::new();

Creates a new ASCII string buffer with the given capacity. The string will be able to hold exactly capacity bytes without reallocating. If capacity is 0, the ASCII string will not allocate.

Examples
let mut s = AsciiString::with_capacity(10);

Creates a new AsciiString from a length, capacity and pointer.

Safety

This is highly unsafe, due to the number of invariants that aren’t checked:

  • The memory at buf need to have been previously allocated by the same allocator this library uses, with an alignment of 1.
  • length needs to be less than or equal to capacity.
  • capacity needs to be the correct value.
  • buf must have length valid ascii elements and contain a total of capacity total, possibly, uninitialized, elements.
  • Nothing else must be using the memory buf points to.

Violating these may cause problems like corrupting the allocator’s internal data structures.

Examples

Basic usage:

use std::mem;

unsafe {
   let mut s = AsciiString::from_ascii("hello").unwrap();
   let ptr = s.as_mut_ptr();
   let len = s.len();
   let capacity = s.capacity();

   mem::forget(s);

   let s = AsciiString::from_raw_parts(ptr, len, capacity);

   assert_eq!(AsciiString::from_ascii("hello").unwrap(), s);
}

Converts a vector of bytes to an AsciiString without checking for non-ASCII characters.

Safety

This function is unsafe because it does not check that the bytes passed to it are valid ASCII characters. If this constraint is violated, it may cause memory unsafety issues with future of the AsciiString, as the rest of this library assumes that AsciiStrings are ASCII encoded.

Converts anything that can represent a byte buffer into an AsciiString.

Errors

Returns the byte buffer if not all of the bytes are ASCII characters.

Examples
let foo = AsciiString::from_ascii("foo".to_string()).unwrap();
let err = AsciiString::from_ascii("Ŋ".to_string()).unwrap_err();
assert_eq!(foo.as_str(), "foo");
assert_eq!(err.into_source(), "Ŋ");

Pushes the given ASCII string onto this ASCII string buffer.

Examples
use std::str::FromStr;
let mut s = AsciiString::from_str("foo").unwrap();
s.push_str("bar".as_ascii_str().unwrap());
assert_eq!(s, "foobar".as_ascii_str().unwrap());

Inserts the given ASCII string at the given place in this ASCII string buffer.

Panics

Panics if idx is larger than the AsciiString’s length.

Examples
use std::str::FromStr;
let mut s = AsciiString::from_str("abc").unwrap();
s.insert_str(1, "def".as_ascii_str().unwrap());
assert_eq!(&*s, "adefbc");

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

Examples
let s = String::with_capacity(10);
assert!(s.capacity() >= 10);

Reserves capacity for at least additional more bytes to be inserted in the given AsciiString. The collection may reserve more space to avoid frequent reallocations.

Panics

Panics if the new capacity overflows usize.

Examples
let mut s = AsciiString::new();
s.reserve(10);
assert!(s.capacity() >= 10);

Reserves the minimum capacity for exactly additional more bytes to be inserted in the given AsciiString. Does nothing if the capacity is already sufficient.

Note that the allocator may give the collection more space than it requests. Therefore capacity can not be relied upon to be precisely minimal. Prefer reserve if future insertions are expected.

Panics

Panics if the new capacity overflows usize.

Examples
let mut s = AsciiString::new();
s.reserve_exact(10);
assert!(s.capacity() >= 10);

Shrinks the capacity of this ASCII string buffer to match it’s length.

Examples
use std::str::FromStr;
let mut s = AsciiString::from_str("foo").unwrap();
s.reserve(100);
assert!(s.capacity() >= 100);
s.shrink_to_fit();
assert_eq!(s.capacity(), 3);

Adds the given ASCII character to the end of the ASCII string.

Examples
let mut s = AsciiString::from_ascii("abc").unwrap();
s.push(AsciiChar::from_ascii('1').unwrap());
s.push(AsciiChar::from_ascii('2').unwrap());
s.push(AsciiChar::from_ascii('3').unwrap());
assert_eq!(s, "abc123");

Shortens a ASCII string to the specified length.

Panics

Panics if new_len > current length.

Examples
let mut s = AsciiString::from_ascii("hello").unwrap();
s.truncate(2);
assert_eq!(s, "he");

Removes the last character from the ASCII string buffer and returns it. Returns None if this string buffer is empty.

Examples
let mut s = AsciiString::from_ascii("foo").unwrap();
assert_eq!(s.pop().map(|c| c.as_char()), Some('o'));
assert_eq!(s.pop().map(|c| c.as_char()), Some('o'));
assert_eq!(s.pop().map(|c| c.as_char()), Some('f'));
assert_eq!(s.pop(), None);

Removes the ASCII character at position idx from the buffer and returns it.

Warning

This is an O(n) operation as it requires copying every element in the buffer.

Panics

If idx is out of bounds this function will panic.

Examples
let mut s = AsciiString::from_ascii("foo").unwrap();
assert_eq!(s.remove(0).as_char(), 'f');
assert_eq!(s.remove(1).as_char(), 'o');
assert_eq!(s.remove(0).as_char(), 'o');

Inserts an ASCII character into the buffer at position idx.

Warning

This is an O(n) operation as it requires copying every element in the buffer.

Panics

If idx is out of bounds this function will panic.

Examples
let mut s = AsciiString::from_ascii("foo").unwrap();
s.insert(2, AsciiChar::b);
assert_eq!(s, "fobo");

Returns the number of bytes in this ASCII string.

Examples
let s = AsciiString::from_ascii("foo").unwrap();
assert_eq!(s.len(), 3);

Returns true if the ASCII string contains zero bytes.

Examples
let mut s = AsciiString::new();
assert!(s.is_empty());
s.push(AsciiChar::from_ascii('a').unwrap());
assert!(!s.is_empty());

Truncates the ASCII string, setting length (but not capacity) to zero.

Examples
let mut s = AsciiString::from_ascii("foo").unwrap();
s.clear();
assert!(s.is_empty());

Converts this AsciiString into a Box<AsciiStr>.

This will drop any excess capacity

Methods from Deref<Target = AsciiStr>

Converts &self to a &str slice.

Converts &self into a byte slice.

Returns the entire string as slice of AsciiChars.

Returns the entire string as mutable slice of AsciiChars.

Returns a raw pointer to the AsciiStr’s buffer.

The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage. Modifying the AsciiStr may cause it’s buffer to be reallocated, which would also make any pointers to it invalid.

Returns an unsafe mutable pointer to the AsciiStr’s buffer.

The caller must ensure that the slice outlives the pointer this function returns, or else it will end up pointing to garbage. Modifying the AsciiStr may cause it’s buffer to be reallocated, which would also make any pointers to it invalid.

Copies the content of this AsciiStr into an owned AsciiString.

Returns the number of characters / bytes in this ASCII sequence.

Examples
let s = AsciiStr::from_ascii("foo").unwrap();
assert_eq!(s.len(), 3);

Returns true if the ASCII slice contains zero bytes.

Examples
let mut empty = AsciiStr::from_ascii("").unwrap();
let mut full = AsciiStr::from_ascii("foo").unwrap();
assert!(empty.is_empty());
assert!(!full.is_empty());

Returns an iterator over the characters of the AsciiStr.

Returns an iterator over the characters of the AsciiStr which allows you to modify the value of each AsciiChar.

Returns an iterator over parts of the AsciiStr separated by a character.

Examples
let words = AsciiStr::from_ascii("apple banana lemon").unwrap()
    .split(AsciiChar::Space)
    .map(|a| a.as_str())
    .collect::<Vec<_>>();
assert_eq!(words, ["apple", "banana", "lemon"]);

Returns an iterator over the lines of the AsciiStr, which are themselves AsciiStrs.

Lines are ended with either LineFeed (\n), or CarriageReturn then LineFeed (\r\n).

The final line ending is optional.

Returns an ASCII string slice with leading and trailing whitespace removed.

Examples
let example = AsciiStr::from_ascii("  \twhite \tspace  \t").unwrap();
assert_eq!("white \tspace", example.trim());

Returns an ASCII string slice with leading whitespace removed.

Examples
let example = AsciiStr::from_ascii("  \twhite \tspace  \t").unwrap();
assert_eq!("white \tspace  \t", example.trim_start());

Returns an ASCII string slice with trailing whitespace removed.

Examples
let example = AsciiStr::from_ascii("  \twhite \tspace  \t").unwrap();
assert_eq!("  \twhite \tspace", example.trim_end());

Compares two strings case-insensitively.

Replaces lowercase letters with their uppercase equivalent.

Replaces uppercase letters with their lowercase equivalent.

Returns a copy of this string where letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’.

Returns a copy of this string where letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’.

Returns the first character if the string is not empty.

Returns the last character if the string is not empty.

Trait Implementations

The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Formats the value using the given formatter. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Creates a value from an iterator. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Convert to AsciiString without checking for non-ASCII characters. Read more
Convert to AsciiString. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Please note that the std::fmt::Result returned by these methods does not support transmission of an error other than that an error occurred.

Writes a string slice into this writer, returning whether the write succeeded. Read more
Writes a char into this writer, returning whether the write succeeded. Read more
Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.