aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2020-01-03 22:53:09 -0800
committeriximeow <me@iximeow.net>2020-01-12 16:26:39 -0800
commit014b7146f1f0b8ca70056fa45dd44dd70713e7a2 (patch)
tree72b28d527f00fa0794c63a6e59d33df384d55da4
parenta6c2fba0ffe00e2d7a86d9ca7f4dae1611c151dc (diff)
allow for granular and customizable errors when decoding instructions
-rw-r--r--src/lib.rs25
1 files 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<Inst> where Inst: Sized {
- fn decode<T: IntoIterator<Item=u8>>(&self, bytes: T) -> Option<Inst>;
- fn decode_into<T: IntoIterator<Item=u8>>(&self, &mut Inst, bytes: T) -> Option<()>;
+ type Error: DecodeError + Debug + Display;
+ fn decode<T: IntoIterator<Item=u8>>(&self, bytes: T) -> Result<Inst, Self::Error>;
+ fn decode_into<T: IntoIterator<Item=u8>>(&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<Unit=Self::Address> + Debug;
- type Decoder: Decoder<Self::Instruction> + Default;
+ type Instruction: Instruction + LengthedInstruction<Unit=Self::Address> + Debug;
+ type DecodeError: DecodeError + Debug + Display;
+ type Decoder: Decoder<Self::Instruction, Error=Self::DecodeError> + Default;
type Operand;
}
#[cfg(not(feature="use-serde"))]
pub trait Arch {
type Address: Address + Debug + Hash + PartialEq + Eq;
- type Instruction: LengthedInstruction<Unit=Self::Address> + Debug;
- type Decoder: Decoder<Self::Instruction> + Default;
+ type Instruction: Instruction + LengthedInstruction<Unit=Self::Address> + Debug;
+ type DecodeError: DecodeError + Debug + Display;
+ type Decoder: Decoder<Self::Instruction, Error=Self::DecodeError> + 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<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {