Struct construe::StrConstrue

source ·
pub struct StrConstrue<const N: usize>(_);
Expand description

Alternative to String for const

This is just a Construe<u8, N> with a few additional methods, see the Construe documentation for a little more info.

use construe::{StrConstrue, construe};

const fn hello_world<const N: usize>() -> StrConstrue<N> {
    let strc = StrConstrue::new();
    strc.push_str("Hello")
        .push_str(" ")
        .push_str("World")
        .push_str("!")
}

construe!(const HELLO_WORLD: &str = hello_world());

// or manually:
const HELLO_WORLD_2: &str = {
    const LEN: usize = hello_world().needs_len();
    const ARRAY: [u8; LEN] = hello_world().store_bytes();
    StrConstrue::borrow_str(&ARRAY)
};

assert_eq!(HELLO_WORLD, "Hello World!");
assert_eq!(HELLO_WORLD, HELLO_WORLD_2);

Implementations§

source§

impl<const N: usize> StrConstrue<N>

source

pub const fn new() -> Self

Create a StrConstrue instance (with or without the buffer)

source

pub const fn len(&self) -> usize

Amount of bytes stored (or supposed to be stored) in the buffer

source

pub const fn is_empty(&self) -> bool

Whether there are no bytes (supposed to be) stored in the buffer

source

pub const fn push_str(self, s: &str) -> Self

Push a &str into the buffer

Note that this takes self by-value: since &mut references aren’t yet stable in const contexts, we have to keep reassigning the output of the function to the variable.

// `mut` because we reassign to it
let mut sc = construe::StrConstrue::<0>::new();
sc = sc.push_str("Test");
// method chaining can save space
sc = sc.push_str("123").push_str(" ").push_str(":^)");
source

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

Return the assembled byte buffer, consuming the struct

Panics if the buffer is not filled or if this is a zero-length buffer that was supposed to store bytes.

Use StrConstrue::borrow_str to obtain a &str from the buffer.

source§

impl StrConstrue<0>

source

pub const fn needs_len(&self) -> usize

Amount of bytes needed to store the string

This is the same as StrConstrue::len, but it exists as a separate method that’s only implemented for N = 0 so that the const N: usize generic parameter on functions returning this can be inferred as 0 for the first run.

source

pub const fn borrow_str(byte_slice: &[u8]) -> &str

Borrow a &str from a byte slice

See the example at the top for how to use this, the buffer returned by StrConstrue::store_bytes needs to be stored in a constant to be able to borrow a &'static str from it.

This just calls core::str::from_utf8 and panics if the byte slice contains invalid UTF-8.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere 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 Twhere 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.