Mercurial > lbo > hg > sstable
changeset 68:d4ce2f7154f9
Implement RandomAccess for File on Windows.
sstable did not compile for any non-Unix platform because
std::os::unix::fs::FileExt was used unconditionally.
This commit adds support for Windows by using the `seek_read` function
instead of `read_at` (https://doc.rust-lang.org/stable/std/os/windows/fs/trait.FileExt.html#tymethod.seek_read).
As far as I can tell, the RandomAccess interface does not give any
guarantees on how the cursor position is handled and thus it should not be
problematic that reading from the file at random positions also changes
the cursor.
In addition, it is now possible to use sstable on other non-Unix
platforms by implementing RandomAccess for a wrapper struct of File
using any platform-specific function.
author | Thomas Krause <thomaskrause@posteo.de> |
---|---|
date | Sun, 16 Feb 2020 11:17:38 +0100 |
parents | cf194f28e5ec |
children | b3aba9007285 |
files | src/types.rs |
diffstat | 1 files changed, 11 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/types.rs Mon Feb 17 10:39:34 2020 +0100 +++ b/src/types.rs Sun Feb 16 11:17:38 2020 +0100 @@ -4,7 +4,10 @@ use std::cell::RefCell; use std::fs::File; +#[cfg(unix)] use std::os::unix::fs::FileExt; +#[cfg(windows)] +use std::os::windows::fs::FileExt; use std::rc::Rc; pub trait RandomAccess { @@ -31,12 +34,20 @@ } } +#[cfg(unix)] impl RandomAccess for File { fn read_at(&self, off: usize, dst: &mut [u8]) -> Result<usize> { Ok((self as &dyn FileExt).read_at(dst, off as u64)?) } } +#[cfg(windows)] +impl RandomAccess for File { + fn read_at(&self, off: usize, dst: &mut [u8]) -> Result<usize> { + Ok((self as &dyn FileExt).seek_read(dst, off as u64)?) + } +} + /// A shared thingy with interior mutability. pub type Shared<T> = Rc<RefCell<T>>;