diff options
| author | Sophie Forrest <git@sophieforrest.com> | 2024-08-30 23:35:45 +1200 |
|---|---|---|
| committer | Sophie Forrest <git@sophieforrest.com> | 2024-08-30 23:35:45 +1200 |
| commit | f5f789540ad7d3f7f4f855c9db69d65cfc190ee0 (patch) | |
| tree | f532988e9a35a0d2c58efbad9daf6e66288f4a1f /src/engine.rs | |
| parent | c9ab8d38765c7c80f2ea9083ce8d326f407110ac (diff) | |
feat(engine): allow choosing engine per executor call
Diffstat (limited to '')
| -rw-r--r-- | src/engine.rs | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/engine.rs b/src/engine.rs index 508178a..8cf2726 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -4,17 +4,22 @@ use std::io::Read; -use crate::executor::{Error, Executor}; +use num_traits::{One, Unsigned, WrappingAdd, WrappingSub, Zero}; + +use crate::executor::{self, Error}; /// Generic engine implementation for the Brainfuck interpreter. -pub trait Engine<T> { +pub trait Engine { + /// Inner type of the Tape. + type TapeInner: Clone + Copy + Unsigned + WrappingAdd + WrappingSub + One + Zero; + /// Read one byte from stdin. /// /// # Errors /// /// This function will return an error if it is unable to read from stdin, /// or if it indexes out of bounds. - fn read_byte() -> Result<T, Error>; + fn read_byte() -> Result<Self::TapeInner, Error>; /// Write the provided byte to stdout. /// @@ -22,10 +27,12 @@ pub trait Engine<T> { /// /// This function will return an error if it is unable to write a byte to /// stdout. - fn write_byte(byte: T) -> Result<(), Error>; + fn write_byte(byte: Self::TapeInner) -> Result<(), Error>; } -impl Engine<u8> for Executor { +impl Engine for executor::U8 { + type TapeInner = u8; + fn read_byte() -> Result<u8, Error> { let mut input: [u8; 1] = [0; 1]; @@ -41,7 +48,9 @@ impl Engine<u8> for Executor { } } -impl Engine<u16> for Executor { +impl Engine for executor::U16 { + type TapeInner = u16; + fn read_byte() -> Result<u16, Error> { let mut input: [u8; 2] = [0; 2]; |