diff --git a/Cargo.toml b/Cargo.toml index 6c13844..8cb6868 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ categories = ["embedded", "no-std", "data-structures"] [features] default = [] -# This is a no-op feature only provided for compatibility in case it is -# explicitly selected by a user. This crate works without explicit indication -# both on std and no_std systems. +# The std feature only toggles whether the Error trait is implemented for error +# types. Apart from that, this crate works without explicit indication both on +# std and no_std systems. std = [] diff --git a/src/conversion.rs b/src/conversion.rs index 5ef3a26..1d758d2 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -16,6 +16,15 @@ impl From for TryFromIntError { } } +impl Display for TryFromIntError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + write!(f, "out of range integral type conversion attempted") + } +} + +#[cfg(feature = "std")] +impl std::error::Error for TryFromIntError {} + // Only implement if $from can be converted into $name lossless macro_rules! implement_from { {[$($name:ident),*], [$($from:ident),*] } => {$(implement_from!($name, $from);)*}; @@ -1829,4 +1838,13 @@ mod tests { assert!(i6::try_from(i7(64)).is_err()); assert!(i6::try_from(i7(-64)).is_err()); } + + #[test] + #[cfg(feature = "std")] + fn error_trait() { + assert_eq!( + (&TryFromIntError(()) as &dyn std::error::Error).to_string(), + "out of range integral type conversion attempted" + ); + } } diff --git a/src/lib.rs b/src/lib.rs index de735d9..23ac8fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ //! and thus does not use it: //! an `Option` still takes up two bytes. -#![no_std] +#![cfg_attr(not(feature = "std"), no_std)] mod lib { pub use core;