I figure this is a good place to have a discussion about this API. Here's the current code, for reference:
|
pub trait DataSource { |
|
// constructors are left to each implementation, once you have one, you can: |
|
fn read(&self, offset: usize, length: usize, buffer: &mut Vec<u8>) -> Result<(), &str>; |
|
fn write(&self, offset: usize, length: usize, buffer: &mut Vec<u8>) -> Result<(), &str>; |
|
fn flush(&self, offset: usize, length: usize) -> Result<(), &str>; |
|
fn add_map( |
|
&self, |
|
with_flag: Flags, |
|
into_address_space: &mut AddressSpace, |
|
offset: usize, |
|
length: usize, |
|
) -> Result<usize, &str>; |
|
fn del_map( |
|
&self, |
|
from_address_space: &mut AddressSpace, |
|
offset: usize, |
|
length: usize, |
|
) -> Result<(), &str>; |
|
} |
Some questions I have:
- Do we want to force the error type to be
&str, or do we want to allow each implementer to provide their own error type? The latter is maybe slightly more complicated, but much, much more idiomatic.
- Why does
write need mutable access to the buffer? Very possible I'm misunderstanding, but my assumption would be it just needs to read out of the buffer so it can write that data into whatever its backing memory is.
- Is there a reason we need to take a
&mut Vec<u8> instead of &mut [u8]? My instinct is the latter is more general (since a &mut Vec can be downcast into a mutable slice, but not vice versa), but open to other considerations.
Happy to write a PR to resolve any/all of these, but wanted to raise them first, since they're more fluid design choices then my other PRs.
I figure this is a good place to have a discussion about this API. Here's the current code, for reference:
cs393_vm_api/src/data_source.rs
Lines 4 to 22 in 2d1299b
Some questions I have:
&str, or do we want to allow each implementer to provide their own error type? The latter is maybe slightly more complicated, but much, much more idiomatic.writeneed mutable access to the buffer? Very possible I'm misunderstanding, but my assumption would be it just needs to read out of the buffer so it can write that data into whatever its backing memory is.&mut Vec<u8>instead of&mut [u8]? My instinct is the latter is more general (since a&mut Veccan be downcast into a mutable slice, but not vice versa), but open to other considerations.Happy to write a PR to resolve any/all of these, but wanted to raise them first, since they're more fluid design choices then my other PRs.