Skip to content

Commit

Permalink
Merge #556
Browse files Browse the repository at this point in the history
556: Add impl for TryFromIntError r=jonas-schievink a=newAM

This implements `defmt::Format` for [`TryFromIntError`](https://doc.rust-lang.org/core/num/struct.TryFromIntError.html) which enables code like this:
```rs
use core::convert::{TryFrom, TryInto};

let foo: u64 = u64::MAX;
let bar: u32 = defmt::unwrap!(foo.try_into());
let eggs: u32 = defmt::unwrap!(u32::try_from(foo));
```

When it fails it looks like this:
```text
ERROR panicked at 'unwrap failed: foo.try_into()'
error: `TryFromIntError(())`
```

Which is similar to what it looks like on `std` targets with `.unwrap()`
```text
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: TryFromIntError(())', src/main.rs:5:35
```


Co-authored-by: Alex Martens <[email protected]>
  • Loading branch information
bors[bot] and newAM authored Aug 9, 2021
2 parents 7aa4f43 + ceb61ce commit f5abeac
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/impls/core_/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ non_zero! {num::NonZeroU32, "{=u32}"}
non_zero! {num::NonZeroU64, "{=u64}"}
non_zero! {num::NonZeroU128, "{=u128}"}
non_zero! {num::NonZeroUsize, "{=usize}"}

impl Format for num::TryFromIntError {
fn format(&self, fmt: Formatter) {
crate::write!(fmt, "TryFromIntError(())");
}
}

0 comments on commit f5abeac

Please sign in to comment.