Skip to content

Commit

Permalink
feat: use From<Arc<impl RuntimeTrait>> to create a Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Jun 25, 2024
1 parent c2b0c0f commit 4c428c6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions async-std/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use futures_lite::future::FutureExt;
use std::{future::Future, time::Duration};
use std::{future::Future, sync::Arc, time::Duration};
use trillium_server_common::{DroppableFuture, Runtime, RuntimeTrait, Stream};

/// async-std runtime
Expand Down Expand Up @@ -89,6 +89,6 @@ impl AsyncStdRuntime {

impl From<AsyncStdRuntime> for Runtime {
fn from(value: AsyncStdRuntime) -> Self {
Runtime::from_trait_impl(value)
Arc::new(value).into()
}
}
13 changes: 6 additions & 7 deletions server-common/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ impl Debug for Runtime {
}
}

impl<R: RuntimeTrait> From<Arc<R>> for Runtime {
fn from(value: Arc<R>) -> Self {
Self(value)
}
}

impl Runtime {
/// Construct a new type-erased runtime object from any [`RuntimeTrait`] implementation.
pub fn new(runtime: impl RuntimeTrait) -> Self {
runtime.into() // we avoid re-arcing a Runtime by using Into::into
}

// in order to avoid re-arcing Runtime in new / into, we use this to actually construct the
// Runtime within From implementations on the runtime trait type
#[doc(hidden)]
pub fn from_trait_impl(runtime: impl RuntimeTrait) -> Self {
Self(Arc::new(runtime))
}

/// Spawn a future on the runtime, returning a future that has detach-on-drop semantics
///
/// Spawned tasks conform to the following behavior:
Expand Down
3 changes: 2 additions & 1 deletion smol/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use futures_lite::{FutureExt, Stream, StreamExt};
use std::{
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
time::Duration,
};
Expand Down Expand Up @@ -113,6 +114,6 @@ impl SmolRuntime {

impl From<SmolRuntime> for Runtime {
fn from(value: SmolRuntime) -> Self {
Runtime::from_trait_impl(value)
Arc::new(value).into()
}
}
6 changes: 4 additions & 2 deletions testing/src/runtimeless/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use futures_lite::{future, Stream};
use std::{future::Future, thread, time::Duration};
use std::{future::Future, sync::Arc, thread, time::Duration};
use trillium_server_common::{DroppableFuture, Runtime, RuntimeTrait};

/// a runtime that isn't a runtime
Expand Down Expand Up @@ -49,11 +49,13 @@ impl RuntimeTrait for RuntimelessRuntime {
future::block_on(fut)
}
}

impl From<RuntimelessRuntime> for Runtime {
fn from(value: RuntimelessRuntime) -> Self {
Runtime::from_trait_impl(value)
Arc::new(value).into()
}
}

impl RuntimelessRuntime {
/// Spawn a future on the runtime, returning a future that has detach-on-drop semantics
///
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ impl TokioRuntime {

impl From<TokioRuntime> for Runtime {
fn from(value: TokioRuntime) -> Self {
Runtime::from_trait_impl(value)
Arc::new(value).into()
}
}

0 comments on commit 4c428c6

Please sign in to comment.