@@ -576,40 +576,55 @@ impl<const N: usize> Ord for String<N> {
576
576
///
577
577
/// Please note that using [`format!`] might be preferable.
578
578
///
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
+ ///
579
588
/// [`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 > {
582
591
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)
588
594
}
589
595
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
+ )
592
600
}
593
601
594
602
/// Macro that creates a fixed capacity [`String`]. Equivalent to [`format!`](https://doc.rust-lang.org/std/macro.format.html).
595
603
///
596
604
/// The first argument is the capacity of the `String`. The following arguments work in the same way as the regular macro.
597
605
///
598
- /// # Panics
606
+ /// # Errors
599
607
///
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.
602
614
///
603
615
/// # Examples
604
616
///
605
617
/// ```
618
+ /// # fn main() -> Result<(), core::fmt::Error> {
606
619
/// use heapless::format;
607
620
///
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)? ;
611
624
/// let (x, y) = (1, 2);
612
- /// format!(12, "{x} + {y} = 3");
625
+ /// format!(12, "{x} + {y} = 3")?;
626
+ /// # Ok(())
627
+ /// # }
613
628
/// ```
614
629
#[ macro_export]
615
630
macro_rules! format {
@@ -885,20 +900,20 @@ mod tests {
885
900
fn format ( ) {
886
901
let number = 5 ;
887
902
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 ( ) ;
889
904
assert_eq ! ( formatted, "005 plus 3.12" )
890
905
}
891
906
892
907
#[ test]
893
- #[ should_panic]
894
908
fn format_overflow ( ) {
895
909
let i = 1234567 ;
896
- format ! ( 4 , "13{}" , i) ;
910
+ let formatted = format ! ( 4 , "13{}" , i) ;
911
+ assert_eq ! ( formatted, Err ( core:: fmt:: Error ) )
897
912
}
898
913
899
914
#[ test]
900
- #[ should_panic]
901
915
fn format_plain_string_overflow ( ) {
902
- format ! ( 2 , "123" ) ;
916
+ let formatted = format ! ( 2 , "123" ) ;
917
+ assert_eq ! ( formatted, Err ( core:: fmt:: Error ) )
903
918
}
904
919
}
0 commit comments