Struct str_buf::StrBuf[][src]

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

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 T is always interpreted as array of bytes.

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

impl<const N: usize> StrBuf<N>[src]

pub const fn new() -> Self[src]

Creates new instance

pub const unsafe fn from_storage(
    storage: [MaybeUninit<u8>; N],
    cursor: u8
) -> Self
[src]

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.

pub const fn from_str(text: &str) -> Self[src]

Creates new instance from existing slice with panic on overflow

pub const fn from_str_checked(text: &str) -> Result<Self, StrBufError>[src]

Creates new instance from existing slice which returns error on overflow

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

Returns pointer to the beginning of underlying buffer

pub const fn remaining(&self) -> usize[src]

Returns number of bytes left (not written yet)

pub fn as_slice(&self) -> &[u8][src]

Returns slice to already written data.

pub fn as_mut_slice(&mut self) -> &mut [u8][src]

Returns mutable slice to already written data.

pub fn as_write_slice(&mut self) -> &mut [MaybeUninit<u8>][src]

Returns mutable slice with unwritten parts of the buffer.

pub fn clear(&mut self)[src]

Clears the content of buffer.

pub const fn empty(self) -> Self[src]

Returns empty self.

pub unsafe fn truncate(&mut self, cursor: u8)[src]

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

pub const fn capacity() -> usize[src]

Returns buffer overall capacity.

pub const fn len(&self) -> usize[src]

Returns number of bytes written.

pub unsafe fn set_len(&mut self, len: u8)[src]

Sets new length of the string.

pub unsafe fn push_str_unchecked(&mut self, text: &str)[src]

Appends given string without any size checks

pub fn push_str(&mut self, text: &str) -> usize[src]

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

pub const fn and(self, text: &str) -> Self[src]

Appends given string, assuming it fits.

On overflow panics with index out of bounds.

pub const unsafe fn and_unsafe(self, bytes: &[u8]) -> Self[src]

Unsafely appends given bytes, assuming valid utf-8.

On overflow panics with index out of bounds as and.

pub fn as_str(&self) -> &str[src]

Access str from underlying storage

Returns empty if nothing has been written into buffer yet.

Trait Implementations

impl<const S: usize> AsMut<[u8]> for StrBuf<S>[src]

impl<const S: usize> AsRef<[u8]> for StrBuf<S>[src]

impl<const S: usize> AsRef<str> for StrBuf<S>[src]

impl<const S: usize> Borrow<str> for StrBuf<S>[src]

impl<const S: usize> Clone for StrBuf<S>[src]

impl<const S: usize> Debug for StrBuf<S>[src]

impl<const S: usize> Deref for StrBuf<S>[src]

type Target = str

The resulting type after dereferencing.

impl<const S: usize> Display for StrBuf<S>[src]

impl<const S: usize> Eq for StrBuf<S>[src]

impl<const S: usize> Hash for StrBuf<S>[src]

impl<const S: usize> Ord for StrBuf<S>[src]

impl<const S: usize> PartialEq<&'_ str> for StrBuf<S>[src]

impl<const S: usize> PartialEq<StrBuf<S>> for StrBuf<S>[src]

impl<const S: usize> PartialEq<StrBuf<S>> for &str[src]

impl<const S: usize> PartialEq<StrBuf<S>> for str[src]

impl<const S: usize> PartialEq<str> for StrBuf<S>[src]

impl<const S: usize> PartialOrd<StrBuf<S>> for StrBuf<S>[src]

impl<const S: usize> TryFrom<&'_ str> for StrBuf<S>[src]

type Error = StrBufError

The type returned in the event of a conversion error.

impl<const S: usize> Write for StrBuf<S>[src]

Auto Trait Implementations

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

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

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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.