Expand description
§externally-driven-async-rt
What is this:
- an async runtime
- that is
no_std - and is driven by an external event loop
Why would I need it? Well, the most typical example is building a single shared async library that must be embedded into an external event loop, e.g.
g_source_add_unix_fdfrom GIO/GTKQSocketNotifierfrom QtMessageQueue.OnFileDescriptorEventListeneron AndroidDispatchSource.makeReadSourceon macOS
The whole concept of the library is to define an async closure, wrap it with the runtime provided by this crate, and then pass its file descriptor (because it’s epoll/kqueue under the hood) to the outer loop. From there you can subscribe to this epollfd/kqueuefd, and once it’s readable you can “poke” the runtime from the outside and make it process accumulated events.
§Where to look
EventLoop- the main type- takes an async closure in
EventLoop::new - borrows user-defined state (see
State) for its whole lifetime - implements
rustix::fd::AsFd - can be driven with
EventLoop::drain_events_without_blocking - the state can be accessed between drains with
EventLoop::state_mut
- takes an async closure in
LoopStorage- the storage- must be static
- can be used by only one
EventLoopever, creating a second one with the same storage panics, even after the first one is dropped
Selector- a
cfg(target_os)-selected selector, epoll or kqueue - create it with
Selector::newand pass it toEventLoop::new, it must outlive theEventLoop - it’s fully managed by
EventLoop, you don’t need to call anything else on it
- a
Context- a shared handle to interact with the event loop- provides methods like
Context::spawn,Context::sleep - is an argument of most network related operations like
net::TcpStream::connect
- provides methods like
Statetrait - a way to inject your own state management- define your own state and event types, make it implement
Stateby specifying how it consumes events - call
Context::dispatchto trigger state transition - between drains read what the tasks produced via
EventLoop::state_mut
- define your own state and event types, make it implement
netmodule with some types to work with TCP/UDP/Unix sockets
§Example
use core::{cell::RefCell, time::Duration};
use externally_driven_async_rt::{EventLoop, LoopStorage, Selector};
use rustix::event::{PollFd, PollFlags, poll};
const TASKS: usize = 1;
const TASK_SIZE: usize = 64;
const ROOT_TASK_SIZE: usize = 256;
const TIMERS: usize = 1;
const FDS: usize = 1;
type Storage = LoopStorage<TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>;
fn main() -> Result<(), Box<dyn std::error::Error>> {
static STORAGE: Storage = Storage::new();
let mut selector = Selector::new()?;
let mut state = RefCell::new(());
let mut event_loop = EventLoop::new(&STORAGE, &mut selector, &mut state, async |ctx| loop {
ctx.sleep(Duration::from_secs(1)).await;
println!("hello");
})?;
loop {
poll(&mut [PollFd::new(&event_loop, PollFlags::IN)], None)?;
event_loop.drain_events_without_blocking()?;
}
}As you can see, everything has an upper bound:
- maximum number of co-existing tasks (spawning a new one either panics or gives your a future depending on whether you use
ctx.spawn()orctx.reserve_task().awaitAPI) - maximum size of the future of the task (exceeding it panics at runtime)
- maximum size of the root task (stored separately, it’s usually bigger than sub-tasks, exceeding it panics at runtime)
- maximum number of co-existing timers (
ctx.sleep(<duration>)orctx.timeout()API, exceeding it panics at runtime) - maximum number of co-existing file descriptors that runtime operates on (exceeding it panics at runtime)
Modules§
- net
- A module that implements TCP/UDP/Unix sockets.
Structs§
- Context
- A handle of the event loop that you are currently in.
- Event
Loop - The main type, an event loop.
- Join
Handle - A join handle of the spawned task, resolves once the task is finished.
- Loop
Storage - The main pre-allocated storage that
EventLoopneeds. - Reserve
Task - A future to obtain a
TaskReservation. - Selector
- A selector for your current platform (epoll on Linux and kqueue on macOS).
- Sleep
- A sleep future.
- Task
Reservation - A task reservation that can be used to spawn a task without waiting for a free slot.
- Timeout
- A timeout future.
Enums§
Traits§
- State
- A trait that your custom state must implement.