aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;