Trait brainfoamkit_lib::VMReader
source · pub trait VMReader {
// Provided methods
fn read(&mut self) -> Result<u8> { ... }
fn get_vmreader_type(&self) -> VMReaderType { ... }
}
Expand description
The VMReader
trait
This trait is used to implement a Reader
for the VirtualMachine
. This
allows us to abstract over several different types of Reader
s, including
StdIn
and File
. This trait is also implemented for the MockReader
struct, which is used for testing.
This is a restricted trait, meaning that it will only be implemented for
specific types. This is done to ensure that the VMReader
is only
implemented for types that are valid for the VirtualMachine
. The valid
types for VMReader
are listed in the
VMReaderType
enum.
Examples
use brainfoamkit_lib::{
VMReader,
VMReaderType,
};
use tempfile::NamedTempFile;
let stdin = std::io::stdin();
let temp_file = NamedTempFile::new().unwrap();
let file = temp_file.reopen().unwrap();
let mock = brainfoamkit_lib::MockReader {
data: std::io::Cursor::new("A".as_bytes().to_vec()),
};
assert_eq!(
stdin.get_vmreader_type(),
brainfoamkit_lib::VMReaderType::Stdin
);
assert_eq!(
file.get_vmreader_type(),
brainfoamkit_lib::VMReaderType::File
);
assert_eq!(
mock.get_vmreader_type(),
brainfoamkit_lib::VMReaderType::Mock
);
See Also
Provided Methods§
sourcefn read(&mut self) -> Result<u8>
fn read(&mut self) -> Result<u8>
Read a single byte from the reader
This function reads a single byte from the reader and returns it as a
u8
for use by the VirtualMachine
.
Errors
This function will return an error if the byte read from the reader is not within the ASCII range.
sourcefn get_vmreader_type(&self) -> VMReaderType
fn get_vmreader_type(&self) -> VMReaderType
Get the type of the reader
This function returns the type of the reader as a VMReaderType
enum.
The currently supported types are:
- Stdin - The standard input device as implemented by the std::io::Stdin struct
- File - A file as implemented by the std::fs::File struct
- Mock - A mock reader as implemented by the
MockReader
struct - Unknown - The default type of
VMReader
The default type of VMReader
is Unknown
, and is used when the type
of the reader is not set.
Implementations on Foreign Types§
source§impl VMReader for File
impl VMReader for File
The implementation of the VMReader
trait for the File
struct
source§fn read(&mut self) -> Result<u8>
fn read(&mut self) -> Result<u8>
Read a single byte from a file
This function reads a single byte from a file and returns it as a u8
for use by the VirtualMachine
.
Errors
This function will return an error if the byte read from the file is not within the ASCII range.
fn get_vmreader_type(&self) -> VMReaderType
source§impl VMReader for Stdin
impl VMReader for Stdin
The implementation of the VMReader
trait for the Stdin
struct
source§fn read(&mut self) -> Result<u8>
fn read(&mut self) -> Result<u8>
Read a single byte from STDIN
This function reads a single byte from STDIN and returns it as a u8
for use by the VirtualMachine
.
Errors
This function will return an error if the byte read from STDIN is not within the ASCII range.
fn get_vmreader_type(&self) -> VMReaderType
Implementors§
impl VMReader for MockReader
The implementation of the VMReader
trait for the MockReader
struct