From 8e8e27376c060f5e0e8beb8c0827fe752d021890 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 8 Nov 2024 12:18:53 -0500 Subject: [PATCH 1/2] Add test of dynamically sized error type error[E0599]: the method `as_display` exists for reference `&str`, but its trait bounds were not satisfied --> tests/test_path.rs:26:9 | 26 | #[error("{tail}")] | ^^^^^^^^ method cannot be called on `&str` due to unsatisfied trait bounds | = note: the following trait bounds were not satisfied: `str: Sized` which is required by `&str: AsDisplay<'_>` --- tests/test_path.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_path.rs b/tests/test_path.rs index f054077..16881ae 100644 --- a/tests/test_path.rs +++ b/tests/test_path.rs @@ -22,6 +22,13 @@ enum EnumPathBuf { Read(PathBuf), } +#[derive(Error, Debug)] +#[error("{tail}")] +pub struct UnsizedError { + pub head: i32, + pub tail: str, +} + fn assert(expected: &str, value: T) { assert_eq!(expected, value.to_string()); } From 24c04daea901b7a426db8fca2c743d0253986ab3 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 8 Nov 2024 12:19:52 -0500 Subject: [PATCH 2/2] Support dynamically sized field in error --- src/display.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/display.rs b/src/display.rs index 7ccdd81..7b2bf1c 100644 --- a/src/display.rs +++ b/src/display.rs @@ -13,7 +13,7 @@ pub trait AsDisplay<'a>: Sealed { impl<'a, T> AsDisplay<'a> for &T where - T: Display + 'a, + T: Display + ?Sized + 'a, { type Target = &'a T; @@ -44,7 +44,7 @@ impl<'a> AsDisplay<'a> for PathBuf { #[doc(hidden)] pub trait Sealed {} -impl Sealed for &T {} +impl Sealed for &T {} #[cfg(feature = "std")] impl Sealed for Path {} #[cfg(feature = "std")]