Skip to content

Commit 9b82635

Browse files
bors[bot]newAM
andauthored
Merge #557
557: Add impl for TryFromSliceError r=justahero a=newAM This implements `defmt::Format` for [`TryFromSliceError`](https://doc.rust-lang.org/stable/core/array/struct.TryFromSliceError.html) which enables code like this: ```rs use core::convert::TryInto; let foo: [u8; 5] = [0, 1, 2, 3, 4]; let bar: u32 = u32::from_be_bytes(defmt::unwrap!(foo[1..2].try_into())); ``` When it fails it looks like this: ```text ERROR panicked at 'unwrap failed: foo [1 .. 2].try_into()' error: `TryFromSliceError(())` ``` 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 <alexmgit@protonmail.com>
2 parents f5abeac + 8053ac1 commit 9b82635

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

src/impls/core_/array.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use core::array;
2+
3+
use super::*;
4+
5+
impl Format for array::TryFromSliceError {
6+
fn format(&self, fmt: Formatter) {
7+
crate::write!(fmt, "TryFromSliceError(())");
8+
}
9+
}

src/impls/core_/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! We generally keep the type parameter trait bounds in case it becomes possible to use this
66
//! later, without making a backwards-incompatible change.
77
8+
mod array;
89
mod num;
910
mod ops;
1011
mod slice;

0 commit comments

Comments
 (0)