Struct str_buf::StrBuf[][src]

pub struct StrBuf<const N: usize> { /* fields omitted */ }
Expand description

Stack based string.

It’s size is mem::size_of::<T>() + mem::size_of::<u8>(), but remember that it can be padded. Can store up to u8::max_value() as anything bigger makes a little sense.

Storage is always capped at u8::max_value(), once panic are allowed inside const fn, creating buffer with invalid storage will panic.

When attempting to create new instance from &str it panics on overflow in debug mode.

use str_buf::StrBuf;
use core::mem;
use core::fmt::Write;
use core::convert::TryInto;

type MyStr = StrBuf::<{mem::size_of::<String>()}>;

const CONST_STR: MyStr = MyStr::new().and("hello").and(" ").and("world");

assert_eq!(CONST_STR, "hello world");

assert_eq!(MyStr::capacity(), mem::size_of::<String>());
assert_ne!(mem::size_of::<MyStr>(), mem::size_of::<String>());
assert_eq!(mem::size_of::<StrBuf::<{mem::size_of::<String>() - 1}>>(), mem::size_of::<String>());

let text: MyStr = "test".try_into().expect("To fit string");
assert_eq!("test", text);
assert_eq!(text, "test");
let mut text = MyStr::new();
let _ = write!(text, "test {}", "hello world");
assert_eq!(text.as_str(), "test hello world");
assert_eq!(text.remaining(), MyStr::capacity() - "test hello world".len());

assert_eq!(text.push_str(" or maybe not"), 8); //Overflow!
assert_eq!(text.as_str(), "test hello world or mayb");
assert_eq!(text.push_str(" or maybe not"), 0); //Overflow, damn

text.clear();
assert_eq!(text.push_str(" or maybe not"), 13); //noice
assert_eq!(text.as_str(), " or maybe not");

assert_eq!(text.clone().as_str(), text.as_str());
assert_eq!(text.clone(), text);

Implementations

Creates new instance

Creates new instance from supplied storage and written size.

It is unsafe, because there is no guarantee that storage is correctly initialized with UTF-8 bytes.

Creates new instance from existing slice with panic on overflow

Creates new instance from existing slice which returns error on overflow

Returns pointer to the beginning of underlying buffer

Returns number of bytes left (not written yet)

Returns slice to already written data.

Returns mutable slice to already written data.

Returns mutable slice with unwritten parts of the buffer.

Clears the content of buffer.

Returns empty self.

Shortens the buffer, keeping the first cursor elements.

Does nothing if new cursor is after current position.

Unsafe as it is up to user to consider character boundary

Returns buffer overall capacity.

Returns number of bytes written.

Sets new length of the string.

Appends given string without any size checks

Appends given string, truncating on overflow, returning number of written bytes

Appends given string, assuming it fits.

On overflow panics with index out of bounds.

Unsafely appends given bytes, assuming valid utf-8.

On overflow panics with index out of bounds as and.

Access str from underlying storage

Returns empty if nothing has been written into buffer yet.

Trait Implementations

Performs the conversion.

Performs the conversion.

Performs the conversion.

Immutably 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

The resulting type after dereferencing.

Dereferences the value.

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. 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 !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

The type returned in the event of a conversion error.

Performs the conversion.

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

Performs the conversion.

Performs the conversion.

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.