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 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 mutable slice to already written data.
Returns mutable slice with unwritten parts of the buffer.
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
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
.
Trait Implementations
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
Writes a string slice into this writer, returning whether the write succeeded. Read more