Skip to content

Commit

Permalink
try to asyncify
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Dec 30, 2024
1 parent a7c4bf8 commit 4c2ea95
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 408 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ bitflags = "2"
shakmaty = "0.27.2"
byteorder = "1.2"
rustc-hash = "2"
once_cell = "1.12"
tracing = "0.1"
memmap2 = { version = "0.9", optional = true }
async-trait = "0.1.83"
async-lock = "3.4"

[target.'cfg(target_os = "linux")'.dependencies]
libc = "0.2" # for optional posix_fadvise
Expand All @@ -33,6 +33,7 @@ libc = "0.2" # for optional posix_fadvise
csv = "1"
bencher = "0.1"
clap = { version = "4", features = ["derive", "deprecated"] }
smol = "2"

[features]
default = []
Expand Down
124 changes: 0 additions & 124 deletions examples/fathom.rs

This file was deleted.

42 changes: 27 additions & 15 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//! Traits to provide a custom filesystem implementation.
use async_trait::async_trait;
use std::{
fs, io,
path::{Path, PathBuf},
};

/// An abstract filesystem.
pub trait Filesystem: Sync + Send {
#[async_trait]
pub trait Filesystem: Send + Sync {
/// Determines the size in bytes of the given file.
///
/// Follows symbolic links.
Expand All @@ -31,7 +33,7 @@ pub trait Filesystem: Sync + Send {
/// # Errors
///
/// See [`std::fs::File::open()`].
fn open(&self, path: &Path) -> io::Result<Box<dyn RandomAccessFile>>;
async fn open(&self, path: &Path) -> io::Result<Box<dyn RandomAccessFile>>;
}

/// The purpose of a read. Advisory only.
Expand All @@ -51,20 +53,26 @@ pub enum ReadHint {
}

/// An abstract randomly readable file.
pub trait RandomAccessFile: Sync + Send {
#[async_trait]
pub trait RandomAccessFile: Send + Sync {
/// Reads some bytes starting from a given offset.
///
/// See [`std::os::unix::fs::FileExt::read_at()`] for precise semantics.
fn read_at(&self, buf: &mut [u8], offset: u64, hint: ReadHint) -> io::Result<usize>;
async fn read_at(&self, buf: &mut [u8], offset: u64, hint: ReadHint) -> io::Result<usize>;

/// Reads the exact number of bytes required to fill `buf` from the given
/// offset.
///
/// See [`std::os::unix::fs::FileExt::read_exact_at()`] for
/// precise semantics.
fn read_exact_at(&self, mut buf: &mut [u8], mut offset: u64, hint: ReadHint) -> io::Result<()> {
async fn read_exact_at(
&self,
mut buf: &mut [u8],
mut offset: u64,
hint: ReadHint,
) -> io::Result<()> {
while !buf.is_empty() {
match self.read_at(buf, offset, hint) {
match self.read_at(buf, offset, hint).await {
Ok(0) => return Err(io::ErrorKind::UnexpectedEof.into()),
Ok(n) => {
let tmp = buf;
Expand All @@ -79,17 +87,17 @@ pub trait RandomAccessFile: Sync + Send {
}

/// Reads the single byte at a given offset.
fn read_u8_at(&self, offset: u64, hint: ReadHint) -> io::Result<u8> {
async fn read_u8_at(&self, offset: u64, hint: ReadHint) -> io::Result<u8> {
let mut buf = [0];
self.read_exact_at(&mut buf[..], offset, hint)?;
self.read_exact_at(&mut buf[..], offset, hint).await?;
Ok(buf[0])
}

/// Reads two bytes at a given offset, and interprets them as a
/// little endian integer.
fn read_u16_le_at(&self, offset: u64, hint: ReadHint) -> io::Result<u16> {
async fn read_u16_le_at(&self, offset: u64, hint: ReadHint) -> io::Result<u16> {
let mut buf = [0; 2];
self.read_exact_at(&mut buf[..], offset, hint)?;
self.read_exact_at(&mut buf[..], offset, hint).await?;
Ok(u16::from_le_bytes(buf))
}
}
Expand Down Expand Up @@ -132,6 +140,7 @@ mod os {
}
}

#[async_trait]
impl Filesystem for OsFilesystem {
fn regular_file_size(&self, path: &Path) -> io::Result<u64> {
regular_file_size_impl(path)
Expand All @@ -141,7 +150,7 @@ mod os {
read_dir_impl(path)
}

fn open(&self, path: &Path) -> io::Result<Box<dyn RandomAccessFile>> {
async fn open(&self, path: &Path) -> io::Result<Box<dyn RandomAccessFile>> {
let file = fs::File::open(path)?;

#[cfg(target_os = "linux")]
Expand All @@ -165,13 +174,14 @@ mod os {
file: fs::File,
}

#[async_trait]
impl RandomAccessFile for OsRandomAccessFile {
#[cfg(unix)]
fn read_at(&self, buf: &mut [u8], offset: u64, _hint: ReadHint) -> io::Result<usize> {
async fn read_at(&self, buf: &mut [u8], offset: u64, _hint: ReadHint) -> io::Result<usize> {
std::os::unix::fs::FileExt::read_at(&self.file, buf, offset)
}
#[cfg(windows)]
fn read_at(&self, buf: &mut [u8], offset: u64, _hint: ReadHint) -> io::Result<usize> {
async fn read_at(&self, buf: &mut [u8], offset: u64, _hint: ReadHint) -> io::Result<usize> {
std::os::windows::fs::FileExt::seek_read(&self.file, buf, offset)
}
}
Expand Down Expand Up @@ -234,6 +244,7 @@ mod mmap {
}
}

#[async_trait]
impl Filesystem for MmapFilesystem {
fn regular_file_size(&self, path: &Path) -> io::Result<u64> {
regular_file_size_impl(path)
Expand All @@ -243,7 +254,7 @@ mod mmap {
read_dir_impl(path)
}

fn open(&self, path: &Path) -> io::Result<Box<dyn RandomAccessFile>> {
async fn open(&self, path: &Path) -> io::Result<Box<dyn RandomAccessFile>> {
let file = fs::File::open(path)?;

// Safety: Contract forwarded to MmapFilesystem::new().
Expand All @@ -262,8 +273,9 @@ mod mmap {
mmap: Mmap,
}

#[async_trait]
impl RandomAccessFile for MmapRandomAccessFile {
fn read_at(&self, buf: &mut [u8], offset: u64, _hint: ReadHint) -> io::Result<usize> {
async fn read_at(&self, buf: &mut [u8], offset: u64, _hint: ReadHint) -> io::Result<usize> {
let offset = offset as usize;
let end = offset + buf.len();
buf.copy_from_slice(
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
//! use shakmaty::{CastlingMode, Chess, fen::Fen};
//! use shakmaty_syzygy::{Tablebase, MaybeRounded, Wdl, Dtz, Syzygy};
//!
//! # smol::block_on::<Result<(), Box<dyn std::error::Error>>>(async {
//! let mut tables = Tablebase::new();
//! tables.add_directory("tables/chess")?;
//!
//! let pos: Chess = "8/8/8/8/B7/N7/K2k4/8 b - - 0 1"
//! .parse::<Fen>()?
//! .into_position(CastlingMode::Standard)?;
//!
//! let wdl = tables.probe_wdl_after_zeroing(&pos)?;
//! let wdl = tables.probe_wdl_after_zeroing(&pos).await?;
//! assert_eq!(wdl, Wdl::Loss);
//!
//! let dtz = tables.probe_dtz(&pos)?;
//! let dtz = tables.probe_dtz(&pos).await?;
//! assert!(matches!(dtz, MaybeRounded::Rounded(Dtz(-59))));
//! # Ok::<_, Box<dyn std::error::Error>>(())
//! # Ok(())
//! # });
//! ```
//!
//! # Errors
Expand Down
Loading

0 comments on commit 4c2ea95

Please sign in to comment.