diff options
author | iximeow <me@iximeow.net> | 2020-01-13 19:57:30 -0800 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2020-01-13 19:57:30 -0800 |
commit | 46df5f1d4dd5d5b493a463020a73ed2cfbd0feaf (patch) | |
tree | 61916a7f233b1c81fa4aa43b6a2eee5cbfd289d5 | |
parent | e6e4ea055788811525b79b21c567066df5816dbb (diff) |
default Decoder::decode() impl
-rw-r--r-- | src/lib.rs | 15 |
1 files changed, 10 insertions, 5 deletions
@@ -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; |