Skip to content

Commit

Permalink
no_std cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
h33p committed Nov 4, 2023
1 parent c56549b commit c5065f4
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 158 deletions.
31 changes: 16 additions & 15 deletions mfio-netfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ bench = false
required-features = ["bin-deps"]

[dependencies]
mfio = { version = "0.1", path = "../mfio" }
mfio-rt = { version = "0.1", path = "../mfio-rt" }
bincode = "1"
cglue = { version = "0.2", features = ["task_unstable"] }
flume = "0.10"
mfio = { version = "0.1", path = "../mfio", default-features = false }
mfio-rt = { version = "0.1", path = "../mfio-rt", default-features = false }
cglue = { version = "0.2", features = ["task_unstable"], default-features = false }

log = "0.4"
bytemuck = { version = "1", features = ["derive"] }
async-mutex = "1"
serde = { version = "1", features = ["derive"] }
parking_lot = "0.12"
futures = "0.3.28"
futures-io = "0.3"
slab = "0.4"
tracing = "0.1"
postcard = { version = "1", features = ["alloc"] }
serde = { version = "1", default-features = false, features = ["derive", "alloc"] }
futures = { version = "0.3", default-features = false, features = ["async-await"] }
debug-ignore = "1"
postcard = { version = "1", features = ["alloc"], default-features = false }
slab = { version = "0.4", default-features = false }

flume = { version = "0.10", optional = true }
async-mutex = { version = "1", optional = true }
parking_lot = { version = "0.12", optional = true }
tracing = { version = "0.1", optional = true }

clap = { version = "4", features = ["cargo", "derive"], optional = true }
anyhow = { version = "1", optional = true }
Expand All @@ -47,7 +47,7 @@ env_logger = { version = "0.10", optional = true }
tokio = { version = "1.24", features = ["rt", "net", "macros"], optional = true }

[dev-dependencies]
mfio-rt = { version = "0.1", path = "../mfio-rt", features = ["test_suite"] }
mfio-rt = { version = "0.1", path = "../mfio-rt", features = ["test_suite"], default-features = false }
env_logger = "0.10"
criterion = { version = "0.5", git = "https://github.com/h33p/criterion.rs", branch = "tput2", features = ["async_tokio", "async_smol", "async_futures"] }
rand = "0.8"
Expand All @@ -58,5 +58,6 @@ tracing-subscriber = "0.3"
tokio = { version = "1", features = ["rt", "macros"] }

[features]
default = []
default = ["std"]
std = ["mfio/std", "mfio-rt/std", "mfio-rt/native", "flume", "async-mutex", "parking_lot", "tracing", "futures/std"]
bin-deps = ["clap", "tokio", "anyhow", "env_logger", "mfio/tokio"]
7 changes: 7 additions & 0 deletions mfio-netfs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

#[cfg(feature = "std")]
mod net;

#[cfg(feature = "std")]
pub use net::client::*;
#[cfg(feature = "std")]
pub use net::server::*;
31 changes: 15 additions & 16 deletions mfio-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ harness = false
required-features = ["mfio/tokio", "mfio/async-io"]

[dependencies]
mfio = { version = "0.1", path = "../mfio" }
futures-io = "0.3"
async-channel = "1.8.0"
futures = "0.3.28"
once_cell = "1"
parking_lot = "0.12"
cfg-if = "1"
mfio = { version = "0.1", path = "../mfio", default-features = false }
futures = { version = "0.3", default-features = false }
once_cell = { version = "1", default-features = false, optional = true }
log = "0.4"
tracing = "0.1"
serde = { version = "1", features = ["derive"] }
flume = "0.10"
oneshot = "0.1"
serde = { version = "1", features = ["derive", "alloc"], default-features = false }

tracing = { version = "0.1", optional = true }
flume = { version = "0.10", optional = true }
parking_lot = { version = "0.12", optional = true }
oneshot = { version = "0.1", optional = true }
tempdir = { version = "0.3", optional = true }
pathdiff = { version = "0.2", optional = true }
async-semaphore = { version = "1", optional = true }
slab = "0.4"
slab = { version = "0.4", default-features = false }

[target.'cfg(windows)'.dependencies]
windows = { version = "0.51", features = ["Win32_System_IO", "Win32_Foundation", "Win32_System_WindowsProgramming", "Win32_Storage_FileSystem", "Win32_Networking_WinSock"] }
Expand All @@ -47,7 +45,7 @@ nix = "0.26"
io-uring = { version = "0.6", optional = true }

[dev-dependencies]
mfio = { version = "0.1", path = "../mfio" }
mfio = { version = "0.1", path = "../mfio", default-features = false }
pollster = { version = "0.3.0", features = ["macro"] }
criterion = { version = "0.5", git = "https://github.com/h33p/criterion.rs", branch = "tput2", features = ["async_tokio", "async_smol", "async_futures"] }
rand = "0.8"
Expand All @@ -59,7 +57,7 @@ tempdir = "0.3"
pathdiff = "0.2"
async-semaphore = "1"

[target.'cfg(not(miri))'.dependencies]
[target.'cfg(not(miri))'.dev-dependencies]
tokio = { version = "1.24", features = ["rt", "rt-multi-thread", "fs", "io-util"] }

#[target.'cfg(unix)'.dev-dependencies]
Expand All @@ -71,8 +69,9 @@ rio = "0.9"
glommio = { version = "0.8", git = "https://github.com/DataDog/glommio", rev = "517326bb2b63b6f6ddcf5deec7a283ee510f44df" }

[features]
default = ["mio", "io-uring", "iocp", "native"]
native = []
default = ["mio", "io-uring", "iocp", "native", "std"]
native = ["once_cell", "oneshot", "parking_lot", "flume", "tracing", "std"]
std = ["mfio/std", "once_cell/std", "once_cell/parking_lot"]
# technically iocp depends on native, but let's be in-line with other backends
iocp = []
test_suite = ["tempdir", "pathdiff", "async-semaphore"]
Expand Down
4 changes: 4 additions & 0 deletions mfio-rt/src/__doctest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#[cfg(feature = "native")]
use crate::NativeRt;
#[cfg(feature = "native")]
use core::future::Future;
#[cfg(feature = "native")]
use mfio::backend::IoBackend;

#[cfg(feature = "native")]
pub fn run_each<'a, Func: Fn(&'a NativeRt) -> F, F: Future>(func: Func) {
for (_, fs) in NativeRt::builder().enable_all().build_each() {
if let Ok(fs) = fs {
Expand Down
54 changes: 49 additions & 5 deletions mfio-rt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

use alloc::string::String;

use core::future::Future;

use core::time::Duration;
use futures::Stream;
use mfio::backend::*;
use mfio::error::Result as MfioResult;
use mfio::io::NoPos;
use mfio::stdeq::{AsyncRead, AsyncWrite};
use serde::{Deserialize, Serialize};

#[cfg(feature = "std")]
use std::net::{SocketAddr, ToSocketAddrs};

#[cfg(feature = "std")]
use std::path::{Path, PathBuf};
use std::time::SystemTime;
#[cfg(not(feature = "std"))]
pub type Path = str;
#[cfg(not(feature = "std"))]
pub type PathBuf = String;

#[cfg(feature = "native")]
pub mod native;
Expand All @@ -17,7 +31,7 @@ mod util;
#[doc(hidden)]
pub mod __doctest;

#[cfg(any(feature = "test_suite", test))]
#[cfg(all(any(feature = "test_suite", test), feature = "std"))]
pub mod test_suite;

#[cfg(feature = "native")]
Expand All @@ -44,8 +58,10 @@ pub enum Shutdown {
Both,
}

#[cfg(feature = "std")]
use std::net;

#[cfg(feature = "std")]
impl From<net::Shutdown> for Shutdown {
fn from(o: net::Shutdown) -> Self {
match o {
Expand All @@ -56,6 +72,7 @@ impl From<net::Shutdown> for Shutdown {
}
}

#[cfg(feature = "std")]
impl From<Shutdown> for net::Shutdown {
fn from(o: Shutdown) -> Self {
match o {
Expand Down Expand Up @@ -149,6 +166,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// use mfio_rt::{DirHandle, Fs};
/// use std::path::Path;
Expand All @@ -170,6 +188,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// use futures::StreamExt;
/// use mfio::error::Error;
Expand Down Expand Up @@ -207,6 +226,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// use mfio::traits::IoRead;
/// use mfio_rt::{DirHandle, Fs, OpenOptions};
Expand Down Expand Up @@ -237,6 +257,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// use futures::StreamExt;
/// use mfio::traits::IoRead;
Expand Down Expand Up @@ -270,6 +291,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// # });
/// ```
Expand All @@ -282,6 +304,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// # });
/// ```
Expand All @@ -291,6 +314,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// # });
/// ```
Expand All @@ -306,6 +330,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// # });
/// ```
Expand All @@ -317,6 +342,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// # });
/// ```
Expand All @@ -328,6 +354,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// # });
/// ```
Expand All @@ -339,6 +366,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// # });
/// ```
Expand All @@ -350,6 +378,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// # });
/// ```
Expand All @@ -362,6 +391,7 @@ pub trait DirHandle: Sized {
/// # Examples
///
/// ```
/// # #[cfg(feature = "std")]
/// # mfio_rt::__doctest::run_each(|fs| async {
/// # });
/// ```
Expand Down Expand Up @@ -449,6 +479,12 @@ impl<P: AsRef<Path> + Copy> DirOp<P> {
}
}

#[cfg(not(feature = "std"))]
pub fn into_string(self) -> DirOp<String> {
self.into_pathbuf()
}

#[cfg(feature = "std")]
pub fn into_string(self) -> DirOp<String> {
match self {
Self::SetPermissions { path, permissions } => DirOp::SetPermissions {
Expand Down Expand Up @@ -486,6 +522,7 @@ pub struct DirEntry {
pub ty: FileType,
}

#[cfg(feature = "std")]
impl From<std::fs::DirEntry> for DirEntry {
fn from(d: std::fs::DirEntry) -> Self {
let ty = d
Expand Down Expand Up @@ -521,6 +558,7 @@ pub enum FileType {
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Debug, Serialize, Deserialize)]
pub struct Permissions {}

#[cfg(feature = "std")]
impl From<std::fs::Permissions> for Permissions {
fn from(_: std::fs::Permissions) -> Self {
Self {}
Expand All @@ -531,9 +569,12 @@ impl From<std::fs::Permissions> for Permissions {
pub struct Metadata {
pub permissions: Permissions,
pub len: u64,
pub modified: Option<SystemTime>,
pub accessed: Option<SystemTime>,
pub created: Option<SystemTime>,
/// Modified time (since unix epoch)
pub modified: Option<Duration>,
/// Accessed time (since unix epoch)
pub accessed: Option<Duration>,
/// Created time (since unix epoch)
pub created: Option<Duration>,
}

pub trait FileHandle: AsyncRead<u64> + AsyncWrite<u64> {}
Expand All @@ -542,6 +583,7 @@ impl<T: AsyncRead<u64> + AsyncWrite<u64>> FileHandle for T {}
pub trait StreamHandle: AsyncRead<NoPos> + AsyncWrite<NoPos> {}
impl<T: AsyncRead<NoPos> + AsyncWrite<NoPos>> StreamHandle for T {}

#[cfg(feature = "std")]
pub trait Tcp: IoBackend {
type StreamHandle: TcpStreamHandle;
type ListenerHandle: TcpListenerHandle<StreamHandle = Self::StreamHandle>;
Expand All @@ -559,6 +601,7 @@ pub trait Tcp: IoBackend {
fn bind<'a, A: ToSocketAddrs + Send + 'a>(&'a self, addrs: A) -> Self::BindFuture<'a, A>;
}

#[cfg(feature = "std")]
pub trait TcpStreamHandle: StreamHandle {
fn local_addr(&self) -> MfioResult<SocketAddr>;
fn peer_addr(&self) -> MfioResult<SocketAddr>;
Expand All @@ -571,6 +614,7 @@ pub trait TcpStreamHandle: StreamHandle {
//fn nodelay(&self) -> MfioResult<bool>;
}

#[cfg(feature = "std")]
pub trait TcpListenerHandle: Stream<Item = (Self::StreamHandle, SocketAddr)> {
type StreamHandle: TcpStreamHandle;

Expand Down
10 changes: 7 additions & 3 deletions mfio-rt/src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,14 +779,18 @@ impl RtBgOp {
let _ = completion.send(ret.map_err(from_io_error));
}
Self::Metadata { path, completion } => {
let to_epoch_duration = |v: std::time::SystemTime| {
v.duration_since(std::time::SystemTime::UNIX_EPOCH).ok()
};

let res = path
.metadata()
.map(|m| Metadata {
permissions: m.permissions().into(),
len: m.len(),
modified: m.modified().ok(),
accessed: m.accessed().ok(),
created: m.created().ok(),
modified: m.modified().ok().and_then(to_epoch_duration),
accessed: m.accessed().ok().and_then(to_epoch_duration),
created: m.created().ok().and_then(to_epoch_duration),
})
.map_err(from_io_error);
let _ = completion.send(res);
Expand Down
Loading

0 comments on commit c5065f4

Please sign in to comment.