From f5f789540ad7d3f7f4f855c9db69d65cfc190ee0 Mon Sep 17 00:00:00 2001 From: Sophie Forrest Date: Fri, 30 Aug 2024 23:35:45 +1200 Subject: feat(engine): allow choosing engine per executor call --- src/engine.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/engine.rs') 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 { +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; + fn read_byte() -> Result; /// Write the provided byte to stdout. /// @@ -22,10 +27,12 @@ pub trait Engine { /// /// 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 for Executor { +impl Engine for executor::U8 { + type TapeInner = u8; + fn read_byte() -> Result { let mut input: [u8; 1] = [0; 1]; @@ -41,7 +48,9 @@ impl Engine for Executor { } } -impl Engine for Executor { +impl Engine for executor::U16 { + type TapeInner = u16; + fn read_byte() -> Result { let mut input: [u8; 2] = [0; 2]; -- cgit 1.4.1