pub trait Runtime<'a> {
// Required methods
fn push_boxed(&mut self, future: Pin<Box<dyn Future<Output = ()>>>);
fn spawn_boxed(
&'a mut self,
future: Pin<Box<dyn Future<Output = ()>>>,
) -> SpawnedFuture<'a> ⓘ;
fn sleep<'b>(
&'a self,
duration: Duration,
) -> Pin<Box<dyn Future<Output = ()> + 'b>>;
fn stop(&mut self) -> Stop ⓘ;
// Provided methods
fn push(&mut self, future: impl Future<Output = ()> + 'static) { ... }
fn spawn(
&'a mut self,
future: impl Future<Output = ()> + 'static,
) -> SpawnedFuture<'a> ⓘ { ... }
fn sleep_ms<'b>(
&'a self,
amount: u64,
) -> Pin<Box<dyn Future<Output = ()> + 'b>> { ... }
}
Expand description
Auto-trait with the methods that will actually be called by the users of the runtime.
Required Methods§
Sourcefn push_boxed(&mut self, future: Pin<Box<dyn Future<Output = ()>>>)
fn push_boxed(&mut self, future: Pin<Box<dyn Future<Output = ()>>>)
Adds a new future to the queue to be completed.
Sourcefn spawn_boxed(
&'a mut self,
future: Pin<Box<dyn Future<Output = ()>>>,
) -> SpawnedFuture<'a> ⓘ
fn spawn_boxed( &'a mut self, future: Pin<Box<dyn Future<Output = ()>>>, ) -> SpawnedFuture<'a> ⓘ
Adds a new future to the queue to be completed and returns a future waiting for the added future’s completion.
Provided Methods§
Sourcefn push(&mut self, future: impl Future<Output = ()> + 'static)
fn push(&mut self, future: impl Future<Output = ()> + 'static)
Adds a new future to the queue to be completed.
Examples found in repository?
examples/tcp.rs (line 15)
13fn main() {
14 let mut runtime = QueuedRuntime::new();
15 runtime.push(go(("0.0.0.0", 5000)));
16 sync(runtime);
17}
18
19async fn go(addr: (&str, u16)) {
20 let mut listener = TcpListener::bind(addr).unwrap();
21 loop {
22 get_current_runtime()
23 .await
24 .push(handle(accept(&mut listener).await.unwrap()));
25 }
26}
More examples
examples/recursion.rs (line 8)
6fn main() {
7 let mut runtime = QueuedRuntime::new();
8 runtime.push(print_something_after_ms(0));
9 sync(runtime);
10}
11
12async fn print_something_after_ms(ms: u64) {
13 get_current_runtime()
14 .await
15 .sleep(Duration::from_millis(ms))
16 .await;
17 println!("something after {ms}ms! :D");
18 get_current_runtime()
19 .await
20 .push(print_something_after_ms(ms + 1));
21}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.