aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2021-07-04 15:59:25 -0700
committeriximeow <me@iximeow.net>2021-07-04 15:59:25 -0700
commit15311c6ce76205d0f0ec17358a242c057f84efba (patch)
tree3d9c75a79506e513f980cbccb8c901fc4c1527d9
parente7cb91d727bd9fbb332d09d8ac254b0bd6c46e88 (diff)
actually enforce DecodeError impl'ing std::error::Error in std builds
the previous test and code only tested one concrete archtecture, and it turns out never required `std::error::Error` on `DecodeError`.
-rw-r--r--src/lib.rs17
-rw-r--r--tests/lib.rs11
2 files changed, 24 insertions, 4 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 038f311..455b5e2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -30,7 +30,7 @@ pub use reader::{Reader, ReadError, U8Reader, U16le, U16be, U32le, U32be, U64le,
/// it is permissible for an implementor of `DecodeError` to have items that return `false` for
/// all these functions; decoders are permitted to error in way that `yaxpeax-arch` does not know
/// about.
-pub trait DecodeError: PartialEq {
+pub trait DecodeError: PartialEq + Display + Debug + Send + Sync + 'static {
/// did the decoder fail because it reached the end of input?
fn data_exhausted(&self) -> bool;
/// did the decoder error because the instruction's opcode is invalid?
@@ -157,9 +157,18 @@ impl<T> AddressBounds for T where T: Address + Debug + Hash + PartialEq + Eq + S
impl<T> AddressBounds for T where T: Address + Debug + Hash + PartialEq + Eq {}
#[cfg(feature = "std")]
-trait DecodeErrorBounds: DecodeError + std::error::Error + Debug + Display {}
+/// this is not a particularly interesting trait. it just exists to add a `std::error::Error`
+/// bound onto `DecodeError` for `std` builds.
+pub trait DecodeErrorBounds: std::error::Error + DecodeError {}
+#[cfg(feature = "std")]
+impl<T: std::error::Error + DecodeError> DecodeErrorBounds for T {}
+#[cfg(not(feature = "std"))]
+/// this is not a particularly interesting trait. it just exists to add a `std::error::Error`
+/// bound onto `DecodeError` for `std` builds.
+pub trait DecodeErrorBounds: DecodeError {}
#[cfg(not(feature = "std"))]
-trait DecodeErrorBounds: DecodeError + Debug + Display {}
+impl<T: DecodeError> DecodeErrorBounds for T {}
+
/// a collection of associated type parameters that constitute the definitions for an instruction
/// set. `Arch` provides an `Instruction` and its associated `Operand`s, which is guaranteed to be
@@ -180,7 +189,7 @@ pub trait Arch {
type Word: Debug + Display + PartialEq + Eq;
type Address: AddressBounds;
type Instruction: Instruction + LengthedInstruction<Unit=AddressDiff<Self::Address>> + Debug + Default + Sized;
- type DecodeError: DecodeError + Debug + Display;
+ type DecodeError: DecodeErrorBounds + Debug + Display;
type Decoder: Decoder<Self> + Default;
type Operand;
}
diff --git a/tests/lib.rs b/tests/lib.rs
index 8e097de..9add6e3 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -10,6 +10,17 @@ fn test_u16() {
}
#[test]
+fn generic_error_can_bail() {
+ use yaxpeax_arch::{Arch, Decoder, Reader};
+
+ #[allow(dead_code)]
+ fn decode<A: Arch, U: Into<impl Reader<A::Address, A::Word>>>(data: U, decoder: &A::Decoder) -> anyhow::Result<()> {
+ let mut reader = data.into();
+ decoder.decode(&mut reader)?;
+ Ok(())
+ }
+}
+#[test]
fn error_can_bail() {
use yaxpeax_arch::{Arch, AddressDiff, Decoder, Reader, LengthedInstruction, Instruction, StandardDecodeError, U8Reader};
struct TestIsa {}