roam_session/runtime/
tokio_runtime.rs1use std::future::Future;
4use std::time::Duration;
5
6pub use tokio::sync::Mutex;
8pub use tokio::sync::mpsc::{
9 Receiver, Sender, UnboundedReceiver, UnboundedSender, channel, error::SendError,
10 unbounded_channel,
11};
12pub use tokio::sync::oneshot::{Receiver as OneshotReceiver, Sender as OneshotSender};
13
14pub fn bounded<T>(buffer: usize) -> (Sender<T>, Receiver<T>) {
16 channel(buffer)
17}
18
19pub fn unbounded<T>() -> (UnboundedSender<T>, UnboundedReceiver<T>) {
21 unbounded_channel()
22}
23
24pub fn oneshot<T>() -> (OneshotSender<T>, OneshotReceiver<T>) {
26 tokio::sync::oneshot::channel()
27}
28
29#[derive(Debug)]
34pub struct AbortHandle(tokio::task::AbortHandle);
35
36impl AbortHandle {
37 pub fn abort(&self) -> bool {
42 self.0.abort();
44 true
46 }
47}
48
49pub fn spawn<F>(future: F) -> tokio::task::JoinHandle<F::Output>
53where
54 F: Future + Send + 'static,
55 F::Output: Send + 'static,
56{
57 tokio::spawn(future)
58}
59
60pub fn spawn_with_abort<F>(future: F) -> AbortHandle
65where
66 F: Future<Output = ()> + Send + 'static,
67{
68 let handle = tokio::spawn(future);
69 AbortHandle(handle.abort_handle())
70}
71
72pub async fn sleep(duration: Duration) {
74 tokio::time::sleep(duration).await;
75}
76
77pub async fn timeout<F, T>(duration: Duration, future: F) -> Option<T>
82where
83 F: Future<Output = T>,
84{
85 (tokio::time::timeout(duration, future).await).ok()
86}