aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2021-06-14 17:46:05 -0700
committeriximeow <me@iximeow.net>2021-06-14 17:46:05 -0700
commit354940536d4f700e7f441090435362e8f6ca219d (patch)
tree7b94def81dc290bd04fe5449418153b997727d60
parent49b64e3534a6e6c6dfeeb69bb2770ea7721663df (diff)
experiment
-rw-r--r--src/lib.rs27
1 files changed, 19 insertions, 8 deletions
diff --git a/src/lib.rs b/src/lib.rs
index e95513d..a5b8767 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -38,32 +38,43 @@ pub trait DecodeError {
fn bad_operand(&self) -> bool;
}
-pub trait Decoder<Inst> where Inst: Sized + Default {
+pub enum ReadError {
+ ExhaustedInput,
+ IOError(&'static str),
+}
+
+pub trait Reader<Item> {
+ fn next(&mut self) -> Result<Item, ReadError>;
+}
+
+pub trait Decoder<A: Arch + ?Sized> {
type Error: DecodeError + Debug + Display;
- fn decode<T: IntoIterator<Item=u8>>(&self, bytes: T) -> Result<Inst, Self::Error> {
- let mut inst = Inst::default();
+ fn decode<T: Reader<A::Word>>(&self, bytes: &mut T) -> Result<A::Instruction, Self::Error> {
+ let mut inst = A::Instruction::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>;
+ fn decode_into<T: Reader<A::Word>>(&self, inst: &mut A::Instruction, bytes: &mut T) -> Result<(), Self::Error>;
}
#[cfg(feature="use-serde")]
pub trait Arch {
+ type Word: Debug + Display + PartialEq + Eq;
type Address: Address + Debug + Hash + PartialEq + Eq + Serialize + for<'de> Deserialize<'de>;
- type Instruction: Instruction + LengthedInstruction<Unit=AddressDiff<Self::Address>> + Debug + Default;
+ type Instruction: Instruction + LengthedInstruction<Unit=AddressDiff<Self::Address>> + Debug + Default + Sized;
type DecodeError: DecodeError + Debug + Display;
- type Decoder: Decoder<Self::Instruction, Error=Self::DecodeError> + Default;
+ type Decoder: Decoder<Self, Error=Self::DecodeError> + Default;
type Operand;
}
#[cfg(not(feature="use-serde"))]
pub trait Arch {
+ type Word: Debug + Display + PartialEq + Eq;
type Address: Address + Debug + Hash + PartialEq + Eq;
- type Instruction: Instruction + LengthedInstruction<Unit=AddressDiff<Self::Address>> + Debug + Default;
+ type Instruction: Instruction + LengthedInstruction<Unit=AddressDiff<Self::Address>> + Debug + Default + Sized;
type DecodeError: DecodeError + Debug + Display;
- type Decoder: Decoder<Self::Instruction, Error=Self::DecodeError> + Default;
+ type Decoder: Decoder<Self, Error=Self::DecodeError> + Default;
type Operand;
}