//! A try_format! macro replacing format! usesuper::FallibleVec; usecrate::TryReserveError; use alloc::fmt::{Arguments, Write}; use alloc::string::String;
/// Take a max capacity a try allocating a string with it. /// /// # Warning: /// /// the max capacity must be > to the formating of the /// arguments. If writing the argument on the string exceed the /// capacity, no error is return and an allocation can occurs which /// can lead to a panic pubfn try_format(max_capacity: usize, args: Arguments<'_>) -> Result<String, TryReserveError> { let v = FallibleVec::try_with_capacity(max_capacity)?; letmut s = String::from_utf8(v).expect("wtf an empty vec should be valid utf8");
s.write_fmt(args)
.expect("a formatting trait implementation returned an error");
Ok(s)
}
#[macro_export] /// Take a max capacity a try allocating a string with it. /// /// # Warning: /// /// the max capacity must be > to the formating of the /// arguments. If writing the argument on the string exceed the /// capacity, no error is return and an allocation can occurs which /// can lead to a panic
macro_rules! tryformat {
($max_capacity:tt, $($arg:tt)*) => (
$crate::format::try_format($max_capacity, format_args!($($arg)*))
)
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.