-
Notifications
You must be signed in to change notification settings - Fork 121
feat(compat): runtime compatibility layers #920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
70beb4a
feat(driver): method to flush the operations
Berrysoft d2972f3
docs(driver): fix typo and outdated comments
Berrysoft 3770133
fix(driver,polling): call set_awake more
Berrysoft 9e05530
feat(compat): runtime compat layer
Berrysoft 80583ae
feat: add compat to monocrate
Berrysoft 63101c1
feat(bench): compat
Berrysoft bd48a77
docs(compat): fill docs
Berrysoft 75706ea
ci: test compat layers
Berrysoft ef52b00
fix(compat): nit
Berrysoft 71b6085
docs(compat): package metadata
Berrysoft 5550087
feat(compat): use futures-channel
Berrysoft 44afbb0
Merge branch 'master' into compio-compat
Berrysoft 32cf4e7
fix(compat,iour): mute EAGAIN & EINTR
Berrysoft 4d33892
docs(compat): comment on read after shutdown
Berrysoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| [package] | ||
| name = "compio-compat" | ||
| version = "0.1.0-rc.1" | ||
| description = "Compatibility layer for compio to work with various async runtimes." | ||
| categories = ["asynchronous", "filesystem", "network-programming"] | ||
| keywords = ["async", "fs", "iocp", "io-uring", "net"] | ||
| readme = "README.md" | ||
| edition = { workspace = true } | ||
| license = { workspace = true } | ||
| repository = { workspace = true } | ||
|
|
||
| [dependencies] | ||
| compio-runtime = { workspace = true } | ||
| compio-log = { workspace = true } | ||
|
|
||
| cfg-if = { workspace = true } | ||
| mod_use = { workspace = true } | ||
|
|
||
| [target.'cfg(windows)'.dependencies] | ||
| compio-driver = { workspace = true } | ||
|
|
||
| futures-channel = { workspace = true } | ||
| windows-sys = { workspace = true, features = ["Win32_System_Threading"] } | ||
| windows-threading = "0.2.1" | ||
|
|
||
| [target.'cfg(target_os = "linux")'.dependencies] | ||
| rustix = { workspace = true, features = ["event", "io_uring"] } | ||
|
|
||
| [target.'cfg(unix)'.dependencies] | ||
| tokio = { workspace = true, optional = true, features = ["net", "time"] } | ||
|
|
||
| async-io = { workspace = true, optional = true } | ||
| futures-util = { workspace = true, optional = true } | ||
|
|
||
| [dev-dependencies] | ||
| compio-fs = { workspace = true } | ||
| compio-net = { workspace = true } | ||
| compio-io = { workspace = true } | ||
|
|
||
| tokio = { workspace = true, features = [ | ||
| "rt", | ||
| "macros", | ||
| "fs", | ||
| "net", | ||
| "io-util", | ||
| ] } | ||
| futures-executor = { workspace = true } | ||
|
|
||
| futures-util = { workspace = true } | ||
|
|
||
| [features] | ||
| tokio = ["dep:tokio"] | ||
| futures = ["dep:async-io", "dep:futures-util"] | ||
|
|
||
| [[test]] | ||
| name = "net" | ||
| required-features = ["tokio"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <div align="center"> | ||
| <a href='https://compio.rs'> | ||
| <img height="150" src="https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-with-text.svg"> | ||
| </a> | ||
| </div> | ||
|
|
||
| --- | ||
|
|
||
| # compio-compat | ||
|
|
||
| [](https://github.com/compio-rs/compio/blob/master/LICENSE) | ||
| [](https://crates.io/crates/compio-compat) | ||
| [](https://docs.rs/compio-compat) | ||
| [](https://github.com/compio-rs/compio/actions/workflows/ci_check.yml) | ||
| [](https://github.com/compio-rs/compio/actions/workflows/ci_test.yml) | ||
|
|
||
| Run compio in other async runtimes. | ||
|
|
||
| ## Usage | ||
|
|
||
| Use `compio::compat` re-exported from `compio` crate. | ||
|
|
||
| ```rust | ||
| use compio::compat::{RuntimeCompat, TokioAdapter}; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| // Create a compio runtime: | ||
| let runtime = compio::runtime::Runtime::new().unwrap(); | ||
| // Create the compat layer: | ||
| let runtime = RuntimeCompat::<TokioAdapter>::new(runtime).unwrap(); | ||
| // Execute your future: | ||
| runtime.execute(async { | ||
| // Run compio-specific code | ||
| }).await; | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| //! Runtime-compatibility layers for compio. | ||
| //! | ||
| //! This crate provides a compatibility layer for compio's runtime, allowing it | ||
| //! to be used with different underlying event loop implementations, e.g., | ||
| //! `tokio` or `smol`. | ||
|
|
||
| #![cfg_attr(docsrs, feature(doc_cfg))] | ||
| #![warn(missing_docs)] | ||
| #![deny(rustdoc::broken_intra_doc_links)] | ||
| #![doc( | ||
| html_logo_url = "https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-bold.svg" | ||
| )] | ||
| #![doc( | ||
| html_favicon_url = "https://github.com/compio-rs/compio-logo/raw/refs/heads/master/generated/colored-bold.svg" | ||
| )] | ||
|
|
||
| use std::{ | ||
| io, | ||
| ops::Deref, | ||
| task::{Context, Poll}, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| use compio_log::error; | ||
| use compio_runtime::Runtime; | ||
| use mod_use::mod_use; | ||
|
|
||
| mod_use![sys]; | ||
|
|
||
| /// A compatibility layer for [`Runtime`]. It is driven by the underlying | ||
| /// [`Adapter`]. | ||
| pub struct RuntimeCompat<A> { | ||
| runtime: A, | ||
| } | ||
|
|
||
| impl<A: Adapter> RuntimeCompat<A> { | ||
| /// Creates a new [`RuntimeCompat`] with the given runtime. | ||
| pub fn new(runtime: Runtime) -> io::Result<Self> { | ||
| let runtime = A::new(runtime)?; | ||
| Ok(Self { runtime }) | ||
| } | ||
|
|
||
| /// Executes the given future on the runtime, driving it to completion. | ||
| pub async fn execute<F: Future>(&self, f: F) -> F::Output { | ||
| let waker = self.runtime.waker(); | ||
| let mut context = Context::from_waker(&waker); | ||
| let mut future = std::pin::pin!(f); | ||
| loop { | ||
| if let Poll::Ready(result) = self.runtime.enter(|| future.as_mut().poll(&mut context)) { | ||
| self.runtime.enter(|| self.runtime.run()); | ||
| return result; | ||
| } | ||
|
|
||
| let mut remaining_tasks = self.runtime.enter(|| self.runtime.run()); | ||
|
|
||
| remaining_tasks |= self.runtime.flush(); | ||
|
|
||
| let timeout = if remaining_tasks { | ||
| Some(Duration::ZERO) | ||
| } else { | ||
| self.runtime.current_timeout() | ||
| }; | ||
|
|
||
| match self.runtime.wait(timeout).await { | ||
| Ok(_) => {} | ||
| Err(e) | ||
| if matches!( | ||
| e.kind(), | ||
| io::ErrorKind::TimedOut | io::ErrorKind::Interrupted | ||
| ) => {} | ||
| Err(e) => panic!("failed to wait for driver: {e:?}"), | ||
| } | ||
|
|
||
| if let Err(_e) = self.runtime.clear() { | ||
| error!("failed to clear notifier: {_e:?}"); | ||
| } | ||
|
|
||
| self.runtime.poll_with(Some(Duration::ZERO)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<A: Adapter> Deref for RuntimeCompat<A> { | ||
| type Target = Runtime; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.runtime | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use std::{io, ops::Deref, time::Duration}; | ||
|
|
||
| use compio_runtime::Runtime; | ||
| use mod_use::mod_use; | ||
|
|
||
| cfg_if::cfg_if! { | ||
| if #[cfg(windows)] { | ||
| mod_use![windows]; | ||
| } else if #[cfg(unix)] { | ||
| mod_use![unix]; | ||
| } else { | ||
| compile_error!("Unsupported platform"); | ||
| } | ||
| } | ||
|
|
||
| /// Adapter trait for different runtimes. | ||
| #[allow(async_fn_in_trait)] | ||
| pub trait Adapter: Sized + Deref<Target = Runtime> { | ||
| /// Creates a new adapter with the given runtime. | ||
| fn new(runtime: Runtime) -> io::Result<Self>; | ||
|
|
||
| /// Waits for the runtime to be ready, with an optional timeout. | ||
| async fn wait(&self, timeout: Option<Duration>) -> io::Result<()>; | ||
|
|
||
| /// Clears the runtime's state after waiting. | ||
| fn clear(&self) -> io::Result<()>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| use std::{io, ops::Deref, time::Duration}; | ||
|
|
||
| use async_io::Async; | ||
| use compio_runtime::Runtime; | ||
| use futures_util::FutureExt; | ||
|
|
||
| use crate::{Adapter, sys::unix::UnixAdapter}; | ||
|
|
||
| /// Adapter for general runtime. It is driven by `async-io`. | ||
| pub struct FuturesAdapter(Async<UnixAdapter>); | ||
|
|
||
| impl Adapter for FuturesAdapter { | ||
| fn new(runtime: Runtime) -> io::Result<Self> { | ||
| Ok(Self(Async::new_nonblocking(UnixAdapter::new(runtime)?)?)) | ||
| } | ||
|
|
||
| async fn wait(&self, timeout: Option<Duration>) -> io::Result<()> { | ||
| let fut = self.0.readable(); | ||
| if let Some(timeout) = timeout { | ||
| let timer = async_io::Timer::after(timeout); | ||
| futures_util::select! { | ||
| res = fut.fuse() => res, | ||
| _ = timer.fuse() => Err(io::ErrorKind::TimedOut.into()), | ||
| } | ||
| } else { | ||
| fut.await | ||
| } | ||
| } | ||
|
|
||
| fn clear(&self) -> io::Result<()> { | ||
| self.0.get_ref().clear() | ||
| } | ||
| } | ||
|
|
||
| impl Deref for FuturesAdapter { | ||
| type Target = Runtime; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| self.0.get_ref() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.