From 46df5f1d4dd5d5b493a463020a73ed2cfbd0feaf Mon Sep 17 00:00:00 2001 From: iximeow Date: Mon, 13 Jan 2020 19:57:30 -0800 Subject: default Decoder::decode() impl --- src/lib.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src') 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 where Inst: Sized { +pub trait Decoder where Inst: Sized + Default { type Error: DecodeError + Debug + Display; - fn decode>(&self, bytes: T) -> Result; - fn decode_into>(&self, &mut Inst, bytes: T) -> Result<(), Self::Error>; + + fn decode>(&self, bytes: T) -> Result { + let mut inst = Inst::default(); + self.decode_into(&mut inst, bytes).map(|_: ()| inst) + } + + fn decode_into>(&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 + Debug; + type Instruction: Instruction + LengthedInstruction + Debug + Default; type DecodeError: DecodeError + Debug + Display; type Decoder: Decoder + 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 + Debug; + type Instruction: Instruction + LengthedInstruction + Debug + Default; type DecodeError: DecodeError + Debug + Display; type Decoder: Decoder + Default; type Operand; -- cgit v1.1