Skip to content

Commit 5e1aff3

Browse files
ede1998Dirbaio
authored andcommitted
return Result from format
1 parent 1b18dd5 commit 5e1aff3

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

src/string.rs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -576,40 +576,55 @@ impl<const N: usize> Ord for String<N> {
576576
///
577577
/// Please note that using [`format!`] might be preferable.
578578
///
579+
/// # Errors
580+
///
581+
/// There are two possible error cases. Both return the unit type [`core::fmt::Error`].
582+
///
583+
/// - In case the formatting exceeds the string's capacity. This error does not exist in
584+
/// the standard library as the string would just grow.
585+
/// - If a formatting trait implementation returns an error. The standard library panics
586+
/// in this case.
587+
///
579588
/// [`format!`]: crate::format!
580-
pub fn format<const N: usize>(args: Arguments<'_>) -> String<N> {
581-
fn format_inner<const N: usize>(args: Arguments<'_>) -> String<N> {
589+
pub fn format<const N: usize>(args: Arguments<'_>) -> Result<String<N>, fmt::Error> {
590+
fn format_inner<const N: usize>(args: Arguments<'_>) -> Result<String<N>, fmt::Error> {
582591
let mut output = String::new();
583-
output
584-
.write_fmt(args)
585-
// cannot differentiate between these error cases because fmt::Error is empty
586-
.expect("capacity exceeded or a formatting trait implementation returned an error");
587-
output
592+
output.write_fmt(args)?;
593+
Ok(output)
588594
}
589595

590-
args.as_str()
591-
.map_or_else(|| format_inner(args), |s| s.try_into().expect("capacity exceeded"))
596+
args.as_str().map_or_else(
597+
|| format_inner(args),
598+
|s| s.try_into().map_err(|_| fmt::Error),
599+
)
592600
}
593601

594602
/// Macro that creates a fixed capacity [`String`]. Equivalent to [`format!`](https://doc.rust-lang.org/std/macro.format.html).
595603
///
596604
/// The first argument is the capacity of the `String`. The following arguments work in the same way as the regular macro.
597605
///
598-
/// # Panics
606+
/// # Errors
599607
///
600-
/// `format!` panics if the formatted `String` would exceeded its capacity.
601-
/// `format!` also panics if a formatting trait implementation returns an error (same as the regular macro).
608+
/// There are two possible error cases. Both return the unit type [`core::fmt::Error`].
609+
///
610+
/// - In case the formatting exceeds the string's capacity. This error does not exist in
611+
/// the standard library as the string would just grow.
612+
/// - If a formatting trait implementation returns an error. The standard library panics
613+
/// in this case.
602614
///
603615
/// # Examples
604616
///
605617
/// ```
618+
/// # fn main() -> Result<(), core::fmt::Error> {
606619
/// use heapless::format;
607620
///
608-
/// format!(4, "test");
609-
/// format!(15, "hello {}", "world!");
610-
/// format!(20, "x = {}, y = {y}", 10, y = 30);
621+
/// format!(4, "test")?;
622+
/// format!(15, "hello {}", "world!")?;
623+
/// format!(20, "x = {}, y = {y}", 10, y = 30)?;
611624
/// let (x, y) = (1, 2);
612-
/// format!(12, "{x} + {y} = 3");
625+
/// format!(12, "{x} + {y} = 3")?;
626+
/// # Ok(())
627+
/// # }
613628
/// ```
614629
#[macro_export]
615630
macro_rules! format {
@@ -885,20 +900,20 @@ mod tests {
885900
fn format() {
886901
let number = 5;
887902
let float = 3.12;
888-
let formatted = format!(15, "{:0>3} plus {float}", number);
903+
let formatted = format!(15, "{:0>3} plus {float}", number).unwrap();
889904
assert_eq!(formatted, "005 plus 3.12")
890905
}
891906

892907
#[test]
893-
#[should_panic]
894908
fn format_overflow() {
895909
let i = 1234567;
896-
format!(4, "13{}", i);
910+
let formatted = format!(4, "13{}", i);
911+
assert_eq!(formatted, Err(core::fmt::Error))
897912
}
898913

899914
#[test]
900-
#[should_panic]
901915
fn format_plain_string_overflow() {
902-
format!(2, "123");
916+
let formatted = format!(2, "123");
917+
assert_eq!(formatted, Err(core::fmt::Error))
903918
}
904919
}

0 commit comments

Comments
 (0)