From 014b7146f1f0b8ca70056fa45dd44dd70713e7a2 Mon Sep 17 00:00:00 2001 From: iximeow Date: Fri, 3 Jan 2020 22:53:09 -0800 Subject: allow for granular and customizable errors when decoding instructions --- src/lib.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 53807ac..1ae01fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -159,24 +159,33 @@ impl Address for usize { fn to_linear(&self) -> usize { *self } } +pub trait DecodeError { + fn data_exhausted(&self) -> bool; + fn bad_opcode(&self) -> bool; + fn bad_operand(&self) -> bool; +} + pub trait Decoder where Inst: Sized { - fn decode>(&self, bytes: T) -> Option; - fn decode_into>(&self, &mut Inst, bytes: T) -> Option<()>; + type Error: DecodeError + Debug + Display; + fn decode>(&self, bytes: T) -> Result; + fn decode_into>(&self, &mut Inst, bytes: T) -> Result<(), Self::Error>; } #[cfg(feature="use-serde")] pub trait Arch { type Address: Address + Debug + Hash + PartialEq + Eq + Serialize + for<'de> Deserialize<'de>; - type Instruction: LengthedInstruction + Debug; - type Decoder: Decoder + Default; + type Instruction: Instruction + LengthedInstruction + Debug; + type DecodeError: DecodeError + Debug + Display; + type Decoder: Decoder + Default; type Operand; } #[cfg(not(feature="use-serde"))] pub trait Arch { type Address: Address + Debug + Hash + PartialEq + Eq; - type Instruction: LengthedInstruction + Debug; - type Decoder: Decoder + Default; + type Instruction: Instruction + LengthedInstruction + Debug; + type DecodeError: DecodeError + Debug + Display; + type Decoder: Decoder + Default; type Operand; } @@ -186,6 +195,10 @@ pub trait LengthedInstruction { fn min_size() -> Self::Unit; } +pub trait Instruction { + fn well_defined(&self) -> bool; +} + #[cfg(feature="use-serde")] impl Serialize for ColorSettings { fn serialize(&self, serializer: S) -> Result { -- cgit v1.1