aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2020-01-13 19:57:30 -0800
committeriximeow <me@iximeow.net>2020-01-13 19:57:30 -0800
commit46df5f1d4dd5d5b493a463020a73ed2cfbd0feaf (patch)
tree61916a7f233b1c81fa4aa43b6a2eee5cbfd289d5
parente6e4ea055788811525b79b21c567066df5816dbb (diff)
default Decoder::decode() impl
-rw-r--r--src/lib.rs15
1 files changed, 10 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 1ae01fb..c5b0610 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -165,16 +165,21 @@ pub trait DecodeError {
fn bad_operand(&self) -> bool;
}
-pub trait Decoder<Inst> where Inst: Sized {
+pub trait Decoder<Inst> where Inst: Sized + Default {
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>;
+
+ fn decode<T: IntoIterator<Item=u8>>(&self, bytes: T) -> Result<Inst, Self::Error> {
+ let mut inst = Inst::default();
+ self.decode_into(&mut inst, bytes).map(|_: ()| inst)
+ }
+
+ fn decode_into<T: IntoIterator<Item=u8>>(&self, inst: &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: Instruction + LengthedInstruction<Unit=Self::Address> + Debug;
+ type Instruction: Instruction + LengthedInstruction<Unit=Self::Address> + Debug + Default;
type DecodeError: DecodeError + Debug + Display;
type Decoder: Decoder<Self::Instruction, Error=Self::DecodeError> + Default;
type Operand;
@@ -183,7 +188,7 @@ pub trait Arch {
#[cfg(not(feature="use-serde"))]
pub trait Arch {
type Address: Address + Debug + Hash + PartialEq + Eq;
- type Instruction: Instruction + LengthedInstruction<Unit=Self::Address> + Debug;
+ type Instruction: Instruction + LengthedInstruction<Unit=Self::Address> + Debug + Default;
type DecodeError: DecodeError + Debug + Display;
type Decoder: Decoder<Self::Instruction, Error=Self::DecodeError> + Default;
type Operand;