[][src]Struct str_buf::StrBuf

pub struct StrBuf<T: Sized> { /* 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;

type MyStr = StrBuf::<String>;

assert_eq!(MyStr::capacity(), mem::size_of::<String>());
assert_ne!(mem::size_of::<MyStr>(), mem::size_of::<String>());
assert_eq!(mem::size_of::<StrBuf::<[u8; mem::size_of::<String>() - 1]>>(), mem::size_of::<String>());

let text: MyStr = "test".into();
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<S: Sized> StrBuf<S>[src]

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

Creates new instance

pub const unsafe fn from_storage(storage: S, 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 fn from_str(text: &str) -> Self[src]

Creates new instance from existing slice with panic 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 clear(&mut self)[src]

Clears the content of buffer.

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 fn as_str(&self) -> &str[src]

Access str from underlying storage

Returns empty if nothing has been written into buffer yet.

Trait Implementations

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

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

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

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

impl<S: Sized> Clone for StrBuf<S>[src]

impl<S: Sized> Debug for StrBuf<S>[src]

impl<S: Sized> Deref for StrBuf<S>[src]

type Target = str

The resulting type after dereferencing.

impl<S: Sized> Display for StrBuf<S>[src]

impl<S: Sized> Eq for StrBuf<S>[src]

impl<S: Sized, '_> From<&'_ str> for StrBuf<S>[src]

impl<S: Sized> Hash for StrBuf<S>[src]

impl<S: Sized> Ord for StrBuf<S>[src]

impl<S: Sized, '_> PartialEq<&'_ str> for StrBuf<S>[src]

impl<S: Sized> PartialEq<StrBuf<S>> for StrBuf<S>[src]

impl<S: Sized, '_> PartialEq<StrBuf<S>> for &'_ str[src]

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

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

impl<S: Sized> PartialOrd<StrBuf<S>> for StrBuf<S>[src]

impl<S: Sized> Write for StrBuf<S>[src]

Auto Trait Implementations

impl<T> Send for StrBuf<T> where
    T: Send

impl<T> Sync for StrBuf<T> where
    T: Sync

impl<T> Unpin for StrBuf<T> where
    T: Unpin

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.