-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
994 additions
and
509 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,69 @@ | ||
use futures_lite::future::FutureExt; | ||
use std::{future::Future, time::Duration}; | ||
use trillium_server_common::{DroppableFuture, Runtime, RuntimeTrait, Stream}; | ||
|
||
/// async-std runtime | ||
#[derive(Clone, Copy, Default, Debug)] | ||
pub struct AsyncStdRuntime(()); | ||
|
||
impl RuntimeTrait for AsyncStdRuntime { | ||
fn spawn<Fut>( | ||
&self, | ||
fut: Fut, | ||
) -> DroppableFuture<impl Future<Output = Option<Fut::Output>> + Send + 'static> | ||
where | ||
Fut: Future + Send + 'static, | ||
Fut::Output: Send + 'static, | ||
{ | ||
let join_handle = async_std::task::spawn(fut); | ||
DroppableFuture::new(async move { join_handle.catch_unwind().await.ok() }) | ||
} | ||
|
||
async fn delay(&self, duration: Duration) { | ||
async_std::task::sleep(duration).await | ||
} | ||
|
||
fn interval(&self, period: Duration) -> impl Stream<Item = ()> + Send + 'static { | ||
async_std::stream::interval(period) | ||
} | ||
|
||
fn block_on<Fut: Future>(&self, fut: Fut) -> Fut::Output { | ||
async_std::task::block_on(fut) | ||
} | ||
} | ||
|
||
impl AsyncStdRuntime { | ||
/// x | ||
pub fn spawn<Fut>( | ||
&self, | ||
fut: Fut, | ||
) -> DroppableFuture<impl Future<Output = Option<Fut::Output>> + Send + 'static> | ||
where | ||
Fut: Future + Send + 'static, | ||
Fut::Output: Send + 'static, | ||
{ | ||
let join_handle = async_std::task::spawn(fut); | ||
DroppableFuture::new(async move { join_handle.catch_unwind().await.ok() }) | ||
} | ||
|
||
/// x | ||
pub async fn delay(&self, duration: Duration) { | ||
async_std::task::sleep(duration).await | ||
} | ||
|
||
/// x | ||
pub fn interval(&self, period: Duration) -> impl Stream<Item = ()> + Send + 'static { | ||
async_std::stream::interval(period) | ||
} | ||
|
||
/// x | ||
pub fn block_on<Fut: Future>(&self, fut: Fut) -> Fut::Output { | ||
async_std::task::block_on(fut) | ||
} | ||
} | ||
|
||
impl From<AsyncStdRuntime> for Runtime { | ||
fn from(value: AsyncStdRuntime) -> Self { | ||
Runtime::new(value) | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.