Struct readable::str::Str

source ·
#[repr(C)]
pub struct Str<const N: usize> { /* private fields */ }
Expand description

A fixed sized stack string

Str is a generic stack-based string with a maximum byte length of u8::MAX.

The generic N is a usize and represents the maximum length of the string, however all constructor functions for Str will panic at compile time if N > 255.

Size

The internal length is stored as a u8, and as such will take minimal space, allowing for longer strings to be stored.

Due to #[repr(C)], N + 1 is how many bytes your Str will take up.

Using Str in powers of 2 is recommended.

// 64 bytes in total, 63 bytes available for the string.
// This will fit in a typical CPU cache-line.
assert_eq!(std::mem::size_of::<Str::<63>>(), 64);

// Maximum string length of 255 fits into 256 bytes.
assert_eq!(std::mem::size_of::<Str::<255>>(), 256);

// Beware, due to `#[repr(C)]`, `Str` is not
// automatically re-arranged and padded by Rust.
assert_eq!(std::mem::size_of::<Str::<6>>(), 7);

Compile-time panic

Any usage of Str will panic at compile time if N > 255:

/// These will all panic at _compile time_
Str::<256>::new();
Str::<256>::try_from("");
Str::<256>::from_static_str("");
Str::<256>::from_static_bytes(b"");

Usage

// Create a `Str` with a maximum capacity of `24` bytes.
const N: usize  = 24;
let mut string = Str::<N>::new();
assert!(string.is_empty());

// Copy the bytes from an actual `str`
let other_str = "this str is 24 bytes :-)";
assert_eq!(other_str.len(), N);
string.copy_str(other_str).unwrap();

// They're the same.
assert_eq!(string, other_str);

// Clear the string.
string.clear();
assert!(string.is_empty());
assert_eq!(string.len(), 0);

// `push_str()` should be the exact same.
string.push_str(other_str).unwrap();
assert_eq!(string, other_str);

// This string is full.
assert!(string.is_full());
assert_eq!(string.len(), N);

// Pushing new strings will error.
let err = string.push_str(other_str);
assert_eq!(err, Err(24));
// Still the same.
assert_eq!(string, other_str);

// Although, we can still overwrite it.
string.copy_str("hello-------------------");
assert_eq!(string, "hello-------------------");
assert_eq!(string.len(), 24);

Implementations§

source§

impl<const N: usize> Str<N>

source

pub const CAPACITY: u8 = _

The maximum length of this string as a u8.

This should == to N in valid cases.

Compile-time panic

This associated constant will cause Str constructor functions to panic at compile time is N > 255.

source

pub const fn new() -> Self

Returns an empty Str.

let string = Str::<4>::new();
assert!(string.is_empty());
assert_eq!(string.len(), 0);
assert!(string.as_str().is_empty());
assert_eq!(string.as_str().len(), 0);
source

pub const fn from_static_bytes(bytes: &'static [u8]) -> Self

Create a Self from static bytes.

The length of the input doesn’t need to be the same as N, it just needs to be equal or less.

Exact length:

const BYTES: [u8; 3] = *b"abc";
const STR: Str<3> = Str::from_static_bytes(&BYTES);

assert_eq!(STR, "abc");

Slightly less length is okay too:

const BYTES: [u8; 2] = *b"ab";
const STR: Str<3> = Str::from_static_bytes(&BYTES);

assert_eq!(STR.len(), 2);
assert_eq!(STR, "ab");
Compile-time panic

This function will panic at compile time if either:

  • The byte length is longer than N
  • The byte’s are not valid UTF-8 bytes
// This doesn't fit, will panic at compile time.
const STR: Str<3> = Str::from_static_bytes("abcd");
source

pub const fn from_static_str(s: &'static str) -> Self

Create a Self from a static str.

The length of the input doesn’t need to be the same as N, it just needs to be equal or less.

Exact length:

const S: &str = "abc";
const STR: Str<3> = Str::from_static_str(&S);

assert_eq!(STR, "abc");

Slightly less length is okay too:

const S: &str = "ab";
const STR: Str<3> = Str::from_static_str(&S);

assert_eq!(STR.len(), 2);
assert_eq!(STR, "ab");
Compile-time panic

This function will panic at compile time if the str length is longer than N.

// This doesn't fit, will panic at compile time.
const STR: Str<3> = Str::from_static_str("abcd");
source

pub const fn as_bytes_all(&self) -> &[u8]

Return all the bytes of this Str, whether valid UTF-8 or not.

let mut string = Str::<10>::new();
string.push_str("hello").unwrap();

// The string length is 5, but the slice
// returned is the full capacity, 10.
assert_eq!(string.as_bytes_all().len(), 10);
source

pub unsafe fn as_bytes_all_mut(&mut self) -> &mut [u8]

Return all the bytes of this Str (mutably), whether valid UTF-8 or not

Safety

The caller must ensure that the content of the slice is valid UTF-8 before the borrow ends and the underlying Str is used.

The caller must also ensure the len is correctly set with Str::set_len or Str::set_len_u8.

let mut string = Str::<5>::new();
string.push_str("hi").unwrap();
assert_eq!(string, "hi");
assert_eq!(string.len(), 2);

// Safety: We must ensure we leave
// leave the bytes as valid UTF-8 bytes
// and that we set the length correctly.
unsafe {
	// Mutate to valid UTF-8 bytes.
	let mut_ref = string.as_bytes_all_mut();
	mut_ref.copy_from_slice(&b"world"[..]);
	// Set the new length.
	string.set_len(5);
}

assert_eq!(string, "world");
assert_eq!(string.len(), 5);
source

pub const fn len(&self) -> usize

Return the length of the valid UTF-8 bytes of this Str

let mut s = Str::<5>::new();
s.push_str("h").unwrap();
assert_eq!(s.len(), 1_usize);

s.push_str("ello").unwrap();
assert_eq!(s.len(), 5_usize);
source

pub const fn len_u8(&self) -> u8

Return the length of the valid UTF-8 bytes of this Str as a u8

let mut s = Str::<5>::new();
s.push_str("h").unwrap();
assert_eq!(s.len_u8(), 1_u8);

s.push_str("ello").unwrap();
assert_eq!(s.len_u8(), 5_u8);
source

pub const fn capacity(&self) -> u8

Returns the maximum capacity (Self::CAPACITY) of this Str.

Should be exactly equal to N.

//        This is N (usize)    This is CAPACITY (u8)
//               |           /       |
//               |           |       |
//               v           v       v
assert_eq!(Str::<10>::CAPACITY,  10_u8);

let s = Str::<10>::new();
assert_eq!(s.capacity(), 10_u8);
source

pub unsafe fn set_len(&mut self, len: usize)

Set the length of the valid UTF-8 bytes of this Str

This will usually be used when manually mutating Str with Str::as_bytes_all_mut().

let mut s = Str::<3>::new();
assert_eq!(s.len(), 0);

unsafe { s.set_len(3); } // <- Using the `Str`
assert_eq!(s.len(), 3);  //    beyond this point
                         //    is a bad idea.

// This wouldn't be undefined behavior,
// but the inner buffer is all zeros.
assert_eq!(s.as_str(), "\0\0\0");

// Overwrite the bytes.
unsafe {
	let mut_ref = s.as_bytes_all_mut();
	mut_ref[0] = b'a';
	mut_ref[1] = b'b';
	mut_ref[2] = b'c';
}
// Should be safe from this point.
assert_eq!(s.as_str(), "abc");
assert_eq!(s.len(),    3);
Safety

Other functions will rely on the internal length to be correct, so the caller must ensure this length is actually correct.

source

pub unsafe fn set_len_u8(&mut self, len: u8)

Set the length of the valid UTF-8 bytes of this Str

This will usually be used when manually mutating Str with Str::as_bytes_all_mut().

Safety

Other functions will rely on the internal length to be correct, so the caller must ensure this length is actually correct.

source

pub const fn remaining(&self) -> usize

How many available bytes are left in this Str before the Self::CAPACITY is completely filled.

let mut s = Str::<5>::new();
s.push_str("hi");
assert_eq!(s.remaining(), 3);
source

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

Returns only the valid UTF-8 bytes of this Str as a byte slice.

let s = Str::<10>::from_static_str("hello");
assert_eq!(s.as_bytes().len(), 5);
source

pub const fn as_ptr(&self) -> *const u8

Returns a pointer to the first byte in the string array.

source

pub fn into_vec(self) -> Vec<u8>
where Self: Sized,

Returns only the valid UTF-8 bytes of this Str as a Vec<u8>

let s = Str::<10>::from_static_str("hello");
let v = s.into_vec();
assert_eq!(v.len(), 5);

let s = unsafe { String::from_utf8_unchecked(v) };
assert_eq!(s, "hello");
source

pub fn invalid(&self) -> bool

Check this Str for correctness.

When constructing/receiving a Str outside of its constructors, it may not be guaranteed that the invariants are upheld.

This function will return true if:

  • Internal length is greater than the internal byte array
  • Inner byte array is longer than 255
  • .as_str() would return invalid UTF-8
// Create `Str` with maximum 5 length.
let mut string = Str::<5>::new();
assert_eq!(string.invalid(), false);

// Unsafely set the length to 10.
unsafe { string.set_len(10); }
// This string is now invalid.
assert_eq!(string.invalid(), true);
source

pub fn clear(&mut self)

Clears all bytes of this Str.

// Create a string.
let mut s = Str::<5>::from_static_str("hello");
assert_eq!(s, "hello");

// Clear the string.
s.clear();
assert_eq!(s, "");
assert!(s.is_empty());
Safety

This does not actually mutate any bytes, it simply sets the internal length to 0.

Do not rely on this to clear the actual bytes.

source

pub fn zero(&mut self)

Zeros all bytes of this Str and sets the length to 0

Unlike Str::clear(), this actually sets all the bytes in the internal array to 0.

// Create a string.
let mut s = Str::<5>::from_static_str("hello");
assert_eq!(s, "hello");

// Zero the string.
s.zero();
assert_eq!(s, "");
assert!(s.is_empty());
source

pub const fn is_empty(&self) -> bool

If this Str is empty.

let mut s = Str::<10>::new();
assert_eq!(s, "");
assert!(s.is_empty());

s.push_str("a").unwrap();
assert!(!s.is_empty());
source

pub const fn is_full(&self) -> bool

If this Str is full (no more capacity left).

let mut s = Str::<3>::new();
assert_eq!(s.len(), 0);
assert!(!s.is_full());

s.push_str("123").unwrap();
assert_eq!(s.len(), 3);
assert!(s.is_full());
source

pub const fn as_str(&self) -> &str

This Str, as a valid UTF-8 str.

let s = Str::<5>::from_static_str("hello");
assert_eq!(s.as_str(), "hello");
source

pub fn into_string(self) -> String
where Self: Sized,

Consumes self into a String

let s = Str::<5>::from_static_str("hello");

let s: String = s.into_string();
assert_eq!(s, "hello");
source

pub fn copy_str(&mut self, s: impl AsRef<str>) -> Result<usize, usize>

Overwrites self with the str s.

The input s must be the exact same length as N or this function will error.

If the copy was successful, Result::Ok is returned with the new length of the string.

If the copy failed because s.len() > N, Result::Err is returned with how many extra bytes couldn’t fit.

If the copy failed because s.len() != N, Result::Err is returned as Err(0).

let mut string = Str::<3>::new();

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(string.copy_str("abcd"), Err(1));

// Input string is 2 in length, not exactly 3.
// `Err(0)` will be returned to indicate this.
assert_eq!(string.copy_str("ab"), Err(0));
source

pub fn copy_str_unchecked(&mut self, s: impl AsRef<str>) -> usize

Performs the same operation as Self::copy_str() except this function does not check if the input str s is too long.

If the copy was successful, the new length of the string is returned.

If the copy failed, this function will panic.

let mut string = Str::<3>::new();

// Input string is 3 in length, we can copy it.
assert_eq!(string.copy_str_unchecked("abc"), 3);
Panics

Instead of erroring, this function will panic if the input s.len() != N.

Input too long:

let mut string = Str::<3>::new();

// Input string is 5 in length, this will panic.
string.copy_str_unchecked("abcd");

Input not long enough:

let mut string = Str::<3>::new();

// Input string is 2 in length, this will panic.
string.copy_str_unchecked("ab");

Input is just right:

let mut string = Str::<3>::new();
string.copy_str_unchecked("abc");
assert_eq!(string, "abc")
source

pub fn push_str(&mut self, s: impl AsRef<str>) -> Result<usize, usize>

Appends self with the str s.

If the push was successful (or s was empty), Result::Ok is returned with the new length of the string.

If the push failed, Result::Err is returned with how many extra bytes couldn’t fit.

let mut string = Str::<3>::new();

// Input string is 4 in length.
// We can't push it.
let err = string.push_str("abcd");
assert_eq!(err, Err(1));

// The string is still empty.
assert!(string.is_empty());

// This 2 length string will fit.
string.push_str("ab").unwrap();
assert_eq!(string, "ab");
// This 1 length string will fit.
string.push_str("c").unwrap();
assert_eq!(string, "abc");

// But not anymore.
let err = string.push_str("d");
assert_eq!(err, Err(1));
assert_eq!(string, "abc");
source

pub fn push_str_unchecked(&mut self, s: impl AsRef<str>) -> usize

Appends self with the str s.

If the push was successful (or s was empty), a usize is returned, representing the new length of the string.

let mut s = Str::<5>::new();
assert_eq!(s.push_str_unchecked("wow"), 3);
Panics

If the push failed, this function panics.

Input string is > than capacity:

let mut s = Str::<3>::new();
s.push_str_unchecked("abcd");

Str has no more remaining capacity:

let mut s = Str::<4>::from_static_str("wow");
assert_eq!(s.len(),       3);
assert_eq!(s.remaining(), 1);

// This won't fit, will panic.
s.push_str_unchecked("wow");
source

pub const fn into_raw(self) -> ([u8; N], u8)

Decomposes a Str into its raw components

Returns the byte array buffer and the valid UTF-8 length of the Str.

let s = Str::<5>::from_static_str("hi");
let (buf, len) = s.into_raw();

assert_eq!(buf, [b'h', b'i', 0, 0, 0]);
assert_eq!(len, 2);
source

pub const unsafe fn from_raw(buf: [u8; N], len: u8) -> Self

Creates a new Str from a byte array buffer and a length

let buf = [b'h', b'i', 0, 0, 0];
let len = 2;

// SAFETY: The length covers valid
// UTF-8 bytes in the provided buffer.
let s = unsafe { Str::<5>::from_raw(buf, len) };
assert_eq!(s, "hi");
Safety

The caller needs to make sure the bytes covered by the len are actual valid UTF-8 bytes.

source

pub fn from_str_exact(string: &str) -> Self

Create a Str directly from a str

let s = Str::<5>::from_str_exact("12345");
assert_eq!(s, "12345");
Panics

The input input str string’s length must be exactly equal to Self::CAPACITY or this function will panic.

// 1 too many characters, will panic.
let s = Str::<4>::from_str_exact("12345");
source

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

Create a Str directly from bytes

let s = Str::<5>::from_bytes_exact(b"12345");
assert_eq!(s, "12345");
Safety

The bytes must be valid UTF-8.

Panics

The input bytes bytes’s length must be exactly equal to Self::CAPACITY or this function will panic.

// 1 too many characters, will panic.
let s = Str::<4>::from_bytes_exact(b"12345");

Trait Implementations§

source§

impl<const N: usize> AsRef<str> for Str<N>

source§

fn as_ref(&self) -> &str

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

impl<const N: usize> Borrow<str> for Str<N>

source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
source§

impl<'__de, const N: usize> BorrowDecode<'__de> for Str<N>

source§

fn borrow_decode<__D: BorrowDecoder<'__de>>( decoder: &mut __D ) -> Result<Self, DecodeError>

Attempt to decode this type with the given BorrowDecode.
source§

impl<const N: usize> Clone for Str<N>

source§

fn clone(&self) -> Str<N>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<const N: usize> Debug for Str<N>

source§

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

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

impl<const N: usize> Decode for Str<N>

source§

fn decode<__D: Decoder>(decoder: &mut __D) -> Result<Self, DecodeError>

Attempt to decode this type with the given Decode.
source§

impl<const N: usize> Default for Str<N>

source§

fn default() -> Self

Calls Self::new

source§

impl<'de, const N: usize> Deserialize<'de> for Str<N>

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<const N: usize> Display for Str<N>

source§

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

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

impl<const N: usize> Encode for Str<N>

source§

fn encode<__E: Encoder>(&self, encoder: &mut __E) -> Result<(), EncodeError>

Encode a given type.
source§

impl<const N: usize> Hash for Str<N>

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<const N: usize> Ord for Str<N>

source§

fn cmp(&self, other: &Str<N>) -> Ordering

This method returns an Ordering between self and other. Read more
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 + PartialOrd,

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

impl<const N: usize> PartialEq<&str> for Str<N>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const N: usize> PartialEq<str> for Str<N>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const N: usize> PartialEq for Str<N>

source§

fn eq(&self, other: &Str<N>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<const N: usize> PartialOrd for Str<N>

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<const N: usize> Serialize for Str<N>

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<const N: usize> TryFrom<&Arc<str>> for Str<N>

source§

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

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<&Box<str>> for Str<N>

source§

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

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<&Cow<'_, str>> for Str<N>

source§

fn try_from(string: &Cow<'_, str>) -> Result<Self, Self::Error>

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<&Rc<str>> for Str<N>

source§

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

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<&String> for Str<N>

source§

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

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<&str> for Str<N>

source§

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

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<Arc<str>> for Str<N>

source§

fn try_from(string: Arc<str>) -> Result<Self, Self::Error>

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<Box<str>> for Str<N>

source§

fn try_from(string: Box<str>) -> Result<Self, Self::Error>

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<Cow<'_, str>> for Str<N>

source§

fn try_from(string: Cow<'_, str>) -> Result<Self, Self::Error>

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<Rc<str>> for Str<N>

source§

fn try_from(string: Rc<str>) -> Result<Self, Self::Error>

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> TryFrom<String> for Str<N>

source§

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

This takes in a &str of any length (equal to or less than N) and will return a Str with that same string.

If this function fails, Result::Err is returned with how many extra bytes couldn’t fit.

// Input string is 4 in length, we can't copy it.
// There is 1 extra byte that can't fit.
assert_eq!(Str::<3>::try_from("abcd"), Err(1));
Compile-time panic

This function will panic at compile time if N > 255.

// Compile error!
Str::<256>::try_from("");
§

type Error = usize

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

impl<const N: usize> Copy for Str<N>

source§

impl<const N: usize> Eq for Str<N>

source§

impl<const N: usize> StructuralEq for Str<N>

source§

impl<const N: usize> StructuralPartialEq for Str<N>

Auto Trait Implementations§

§

impl<const N: usize> RefUnwindSafe for Str<N>

§

impl<const N: usize> Send for Str<N>

§

impl<const N: usize> Sync for Str<N>

§

impl<const N: usize> Unpin for Str<N>

§

impl<const N: usize> UnwindSafe for Str<N>

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> HeadTail for T
where T: Borrow<str>,

source§

fn as_str(&self) -> &str

Turn self into a str. Read more
source§

fn head(&self, head: usize) -> Head<'_>

Return the first head UTF-8 characters of this str. Read more
source§

fn head_dot(&self, head: usize) -> HeadDot<'_>

Same as HeadTail::head() but this will allocate a new String ending with .... Read more
source§

fn tail(&self, tail: usize) -> Tail<'_>

Return the last tail UTF-8 characters of this str. Read more
source§

fn tail_dot(&self, tail: usize) -> TailDot<'_>

Same as HeadTail::tail() but this allocated a new String ending with .... Read more
source§

fn head_tail(&self, head: usize, tail: usize) -> HeadTailStr<'_>

Return the first head UTF-8 characters and last tail UTF-8 characters of this str. Read more
source§

fn head_tail_dot(&self, head: usize, tail: usize) -> HeadTailDot<'_>

Return the first head UTF-8 characters and last tail UTF-8 characters of this str separated with .... Read more
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> ToCompactString for T
where T: Display,

source§

fn to_compact_string(&self) -> CompactString

Converts the given value to a CompactString. Read more
source§

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

§

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§

default 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>,

§

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>,

§

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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,