[−][src]Struct const_format::fmt::StrWriter
A wrapper over an array usable to build up a &str
at compile-time.
Construction
This type is constructed with an array,
and then a reference to it must be coerced to point to StrWriter<[u8]>
to call
certain methods
Example of coercing it:
let writer: &mut StrWriter<[u8; 8]> = &mut StrWriter::new([0; 8]); // Coerces the `&mut StrWriter<[u8; 8]>` to `&mut StrWriter<[u8]>` let writer: &mut StrWriter = writer;
StrWriter
's type parameter defaults to [u8]
,
so every instance of a StrWriter
as a concrete type is a StrWriter<[u8]>
.
StrWriterMut
StrWriter
can be borrowed into a StrWriterMut
,
which provides methods for writing a formatted string..
Example:
use const_format::StrWriter; let mut buffer: &mut StrWriter = &mut StrWriter::new([0; 100]); let mut writer = buffer.as_mut(); writer.write_str("Your password is: "); writer.write_str_debug("PASSWORD"); assert_eq!(writer.as_str(), r#"Your password is: "PASSWORD""#);
Examples
Formatting into associated constant
This example shows how you can construct a formatted &'static str
from associated constants.
#![feature(const_mut_refs)] use const_format::{StrWriter, strwriter_as_str, writec, unwrap}; trait Num { const V: u32; } struct Two; impl Num for Two { const V: u32 = 2; } struct Three; impl Num for Three { const V: u32 = 3; } struct Mul<L, R>(L, R); const fn compute_str(l: u32, r: u32) -> StrWriter<[u8; 128]> { let mut writer = StrWriter::new([0; 128]); unwrap!(writec!(writer, "{} * {} == {}", l, r, l * r )); writer } impl<L: Num, R: Num> Mul<L, R> { const STR: &'static str = strwriter_as_str!(&compute_str(L::V, R::V)); } assert_eq!(Mul::<Two,Three>::STR, "2 * 3 == 6"); assert_eq!(Mul::<Three,Three>::STR, "3 * 3 == 9");
Implementations
impl<A> StrWriter<A>
[src]
impl<A: ?Sized> StrWriter<A>
[src]
pub const fn buffer(&self) -> &A
[src]
Accesses the underlying buffer immutably.
Example
use const_format::StrWriter; let buffer: &mut StrWriter = &mut StrWriter::new([0; 7]); assert_eq!(buffer.buffer(), &[0; 7]); buffer.as_mut().write_str("foo")?; assert_eq!(buffer.buffer(), b"foo\0\0\0\0"); buffer.as_mut().write_str("bar")?; assert_eq!(buffer.buffer(), b"foobar\0");
pub const fn len(&self) -> usize
[src]
How long the string this wrote is.
Example
use const_format::StrWriter; let buffer: &mut StrWriter = &mut StrWriter::new([0; 64]); assert_eq!(buffer.len(), 0); buffer.as_mut().write_str("foo")?; assert_eq!(buffer.len(), 3); buffer.as_mut().write_str("bar")?; assert_eq!(buffer.len(), 6);
pub const fn is_empty(&self) -> bool
[src]
Checks whether the string this wrote is empty.
Example
use const_format::StrWriter; let buffer: &mut StrWriter = &mut StrWriter::new([0; 64]); assert!( buffer.is_empty() ); buffer.as_mut().write_str("foo")?; assert!( !buffer.is_empty() );
impl StrWriter
[src]
pub const fn capacity(&self) -> usize
[src]
Gets how the maximum length for a string written into this.
Trying to write more that the capacity causes is an error,
returning back an Err(Error::NotEnoughSpace)
Example
use const_format::{Error, StrWriter}; let buffer: &mut StrWriter = &mut StrWriter::new([0; 64]); assert_eq!(buffer.capacity(), 64); buffer.as_mut().write_ascii_repeated(b'A', 64)?; assert_eq!(buffer.capacity(), 64); assert_eq!(buffer.as_mut().write_str("-").unwrap_err(), Error::NotEnoughSpace); assert_eq!(buffer.capacity(), 64);
pub const fn remaining_capacity(&self) -> usize
[src]
Checks how many more bytes can be written.
Example
use const_format::{Error, StrWriter}; let buffer: &mut StrWriter = &mut StrWriter::new([0; 64]); assert_eq!(buffer.remaining_capacity(), 64); buffer.as_mut().write_str("foo")?; assert_eq!(buffer.remaining_capacity(), 61); buffer.as_mut().write_ascii_repeated(b'a', 61)?; assert_eq!(buffer.remaining_capacity(), 0); assert_eq!(buffer.as_mut().write_str(" ").unwrap_err(), Error::NotEnoughSpace);
pub const fn truncate(&mut self, length: usize) -> Result<(), Error>
[src]
Truncates this StrWriter
to length
.
If length
is greater than the current length, this does nothing.
Errors
Returns an Error::NotOnCharBoundary
if length
is not on a char boundary.
Example
use const_format::{Error, StrWriter}; let buffer: &mut StrWriter = &mut StrWriter::new([0; 64]); buffer.as_mut().write_str("foo bâr baz"); assert_eq!(buffer.as_str(), "foo bâr baz"); assert_eq!(buffer.truncate(6).unwrap_err(), Error::NotOnCharBoundary); buffer.truncate(3)?; assert_eq!(buffer.as_str(), "foo"); buffer.as_mut().write_str("ooooooo"); assert_eq!(buffer.as_str(), "fooooooooo");
pub const fn clear(&mut self)
[src]
Truncates this StrWriter
to length 0.
Example
use const_format::{Error, StrWriter}; let buffer: &mut StrWriter = &mut StrWriter::new([0; 64]); buffer.as_mut().write_str("foo")?; assert_eq!(buffer.as_str(), "foo"); buffer.clear(); assert_eq!(buffer.as_str(), ""); assert!(buffer.is_empty()); buffer.as_mut().write_str("bar"); assert_eq!(buffer.as_str(), "bar");
pub const fn as_bytes_alt(&self) -> &[u8]
[src]
Gets the written part of this StrWriter
as a &[u8]
The slice is guaranteed to be valid utf8, so this is mostly for convenience.
Runtime
If the "constant_time_as_str" feature is disabled,
thich takes time proportional to self.capacity() - self.len()
.
If the "constant_time_as_str" feature is enabled, it takes constant time to run, but uses a few additional nightly features.
Example
#![feature(const_mut_refs)] use const_format::{StrWriter, StrWriterMut}; const fn slice() -> StrWriter<[u8; 64]> { let mut buffer = StrWriter::new([0; 64]); let mut writer = StrWriterMut::new(&mut buffer); writer.write_str("Hello, World!"); buffer } const SLICE: &[u8] = { let promoted: &'static StrWriter = &slice(); promoted.as_bytes_alt() }; assert_eq!(SLICE, "Hello, World!".as_bytes());
pub const fn as_str(&self) -> &str
[src]
Gets the written part of this StrWriter
as a &str
Constness
This can be called in const contexts by enabling the "constant_time_as_str" feature, which requires nightly Rust versions after 2020-08-15.
Alternative
For converting &'static StrWriter
constants to &'static str
constants,
you can also use the strwriter_as_str
macro.
Examples
You can look at the type-level docs for examples of using this method.
pub const fn as_bytes(&self) -> &[u8]
[src]
Gets the written part of this StrWriter
as a &[u8]
The slice is guaranteed to be valid utf8, so this is mostly for convenience.
Constness
This can be called in const contexts by enabling the "constant_time_as_str" feature, which requires nightly Rust versions after 2020-08-15.
Example
use const_format::StrWriter; let buffer: &mut StrWriter = &mut StrWriter::new([0; 64]); buffer.as_mut().write_str("Hello, World!"); assert_eq!(buffer.as_bytes(), "Hello, World!".as_bytes());
pub const fn as_mut(&mut self) -> StrWriterMut<'_>
[src]
Borrows this StrWriter<[u8]>
into a StrWriterMut
,
most useful for calling the write_*
methods.
use const_format::StrWriter; let buffer: &mut StrWriter = &mut StrWriter::new([0; 64]); buffer.as_mut().write_str_range("trust", 1..usize::MAX); assert_eq!(buffer.as_str(), "rust");
pub const fn make_formatter(&mut self, flags: FormattingFlags) -> Formatter<'_>
[src]
Constructs a Formatter
that writes into this StrWriter
,
which can be passed to debug and display formatting methods.
Example
#![feature(const_mut_refs)] use const_format::{Error, Formatter, FormattingFlags, StrWriter, call_debug_fmt}; use std::ops::Range; const fn range_debug_fmt( slice: &[Range<usize>], f: &mut Formatter<'_> ) -> Result<(), Error> { // We need this macro to debug format arrays of non-primitive types // Also, it implicitly returns a `const_format::Error` on error. call_debug_fmt!(array, slice, f); Ok(()) } fn main() -> Result<(), Error> { let buffer: &mut StrWriter = &mut StrWriter::new([0; 64]); range_debug_fmt( &[0..14, 14..31, 31..48], &mut buffer.make_formatter(FormattingFlags::new().set_binary()) )?; assert_eq!(buffer.as_str(), "[0..1110, 1110..11111, 11111..110000]"); Ok(()) }
impl<A: ?Sized> StrWriter<A>
[src]
pub const fn borrow_mutably(&mut self) -> &mut Self
[src]
For borrowing this mutably in macros, without getting nested mutable references.
Trait Implementations
impl<A: Clone + ?Sized> Clone for StrWriter<A>
[src]
impl<A: Copy + ?Sized> Copy for StrWriter<A>
[src]
impl<A: Debug + ?Sized> Debug for StrWriter<A>
[src]
impl<T: ?Sized> WriteMarker for StrWriter<T>
[src]
type Kind = IsAStrWriter
Whether this is a StrWriter or not, this can be either of
IsAStrWriter
or IsNotAStrWriter
Read more
type This = Self
The type after dereferencing,
implemented as type This = Self;
for all non-reference types Read more
Auto Trait Implementations
impl<A: ?Sized> RefUnwindSafe for StrWriter<A> where
A: RefUnwindSafe,
A: RefUnwindSafe,
impl<A: ?Sized> Send for StrWriter<A> where
A: Send,
A: Send,
impl<A: ?Sized> Sync for StrWriter<A> where
A: Sync,
A: Sync,
impl<A: ?Sized> Unpin for StrWriter<A> where
A: Unpin,
A: Unpin,
impl<A: ?Sized> UnwindSafe for StrWriter<A> where
A: UnwindSafe,
A: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,