Skip to content

Commit

Permalink
Remove async_traits dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis committed Dec 2, 2023
1 parent 20f5671 commit 2fa760c
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 22 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ tokio-util = { version = "0.7.10", default-features = false }
pin-project-lite = "0.2.13"
thiserror = "1.0.49"
miette = "5.10.0"
async-trait = "0.1.73"
atomic = "0.6.0"
bytemuck = { version = "1.14.0", features = ["derive"] }

Expand Down
5 changes: 1 addition & 4 deletions examples/02_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
//!
//! There are two ways of using structs as subsystems, by either
//! wrapping them in a closure, or by implementing the
//! IntoSubsystem trait. Note, though, that the IntoSubsystem
//! trait requires an additional dependency, `async-trait`.
//! IntoSubsystem trait.
use async_trait::async_trait;
use miette::Result;
use tokio::time::{sleep, Duration};
use tokio_graceful_shutdown::{IntoSubsystem, SubsystemBuilder, SubsystemHandle, Toplevel};
Expand All @@ -30,7 +28,6 @@ struct Subsystem2 {
arg: u32,
}

#[async_trait]
impl IntoSubsystem<miette::Report> for Subsystem2 {
async fn run(self, subsys: SubsystemHandle) -> Result<()> {
tracing::info!("Subsystem2 started. Extra argument: {}", self.arg);
Expand Down
1 change: 0 additions & 1 deletion examples/18_error_type_passthrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ async fn subsys5(_subsys: SubsystemHandle<MyError>) -> Result<(), MyError> {
// both are identical.
struct Subsys6;

#[async_trait::async_trait]
impl IntoSubsystem<MyError, MyError> for Subsys6 {
async fn run(self, _subsys: SubsystemHandle<MyError>) -> Result<(), MyError> {
tracing::info!("Subsystem6 started.");
Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"
24 changes: 9 additions & 15 deletions src/into_subsystem.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
use core::future::Future;
use std::pin::Pin;

use async_trait::async_trait;

use crate::{BoxedError, ErrTypeTraits, SubsystemHandle};

type SubsystemFuture<Err> = dyn Future<Output = Result<(), Err>> + Send + 'static;
type SubsystemFunction<Err, ErrWrapper> =
dyn FnOnce(SubsystemHandle<ErrWrapper>) -> Pin<Box<SubsystemFuture<Err>>> + Send + 'static;

#[async_trait]
/// Allows a struct to be used as a subsystem.
///
/// Implementing this trait requires the `async_trait` dependency.
///
/// Using a struct that does not implement this trait as a subsystem is possible
/// by wrapping it in an async closure. This trait exists primarily
/// for convenience.
Expand All @@ -30,7 +21,6 @@ type SubsystemFunction<Err, ErrWrapper> =
///
/// struct MySubsystem;
///
/// #[async_trait::async_trait]
/// impl IntoSubsystem<miette::Report> for MySubsystem {
/// async fn run(self, subsys: SubsystemHandle) -> Result<()> {
/// subsys.request_shutdown();
Expand Down Expand Up @@ -67,13 +57,17 @@ where
///
/// For more information about subsystem functions, see
/// [`SubsystemHandle::start()`](crate::SubsystemHandle::start).
async fn run(self, subsys: SubsystemHandle<ErrWrapper>) -> Result<(), Err>;
fn run(
self,
subsys: SubsystemHandle<ErrWrapper>,
) -> impl std::future::Future<Output = Result<(), Err>> + Send;

/// Converts the object into a type that can be passed into
/// [`SubsystemHandle::start()`](crate::SubsystemHandle::start).
fn into_subsystem(self) -> Box<SubsystemFunction<Err, ErrWrapper>> {
Box::new(|handle: SubsystemHandle<ErrWrapper>| {
Box::pin(async move { self.run(handle).await })
})
fn into_subsystem(
self,
) -> impl FnOnce(SubsystemHandle<ErrWrapper>) -> Pin<Box<dyn Future<Output = Result<(), Err>> + Send>>
{
|handle: SubsystemHandle<ErrWrapper>| Box::pin(async move { self.run(handle).await })
}
}
1 change: 0 additions & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ async fn normal_shutdown() {
async fn use_subsystem_struct() {
struct MySubsystem;

#[async_trait::async_trait]
impl IntoSubsystem<BoxedError> for MySubsystem {
async fn run(self, subsys: SubsystemHandle) -> BoxedResult {
subsys.on_shutdown_requested().await;
Expand Down

0 comments on commit 2fa760c

Please sign in to comment.