From 2fa760c471e034c677de2b5cceb847c6fd295377 Mon Sep 17 00:00:00 2001 From: Finomnis Date: Sat, 2 Dec 2023 23:02:18 +0100 Subject: [PATCH 1/2] Remove async_traits dependency --- Cargo.toml | 1 - examples/02_structs.rs | 5 +---- examples/18_error_type_passthrough.rs | 1 - rust-toolchain.toml | 2 ++ src/into_subsystem.rs | 24 +++++++++--------------- tests/integration_test.rs | 1 - 6 files changed, 12 insertions(+), 22 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/Cargo.toml b/Cargo.toml index 4a3ae20..6f05a4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/examples/02_structs.rs b/examples/02_structs.rs index 089da5d..c9586e5 100644 --- a/examples/02_structs.rs +++ b/examples/02_structs.rs @@ -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}; @@ -30,7 +28,6 @@ struct Subsystem2 { arg: u32, } -#[async_trait] impl IntoSubsystem for Subsystem2 { async fn run(self, subsys: SubsystemHandle) -> Result<()> { tracing::info!("Subsystem2 started. Extra argument: {}", self.arg); diff --git a/examples/18_error_type_passthrough.rs b/examples/18_error_type_passthrough.rs index 4b18c4e..5e3e765 100644 --- a/examples/18_error_type_passthrough.rs +++ b/examples/18_error_type_passthrough.rs @@ -65,7 +65,6 @@ async fn subsys5(_subsys: SubsystemHandle) -> Result<(), MyError> { // both are identical. struct Subsys6; -#[async_trait::async_trait] impl IntoSubsystem for Subsys6 { async fn run(self, _subsys: SubsystemHandle) -> Result<(), MyError> { tracing::info!("Subsystem6 started."); diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..5d56faf --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" diff --git a/src/into_subsystem.rs b/src/into_subsystem.rs index 14b814a..59806a8 100644 --- a/src/into_subsystem.rs +++ b/src/into_subsystem.rs @@ -1,19 +1,10 @@ use core::future::Future; use std::pin::Pin; -use async_trait::async_trait; - use crate::{BoxedError, ErrTypeTraits, SubsystemHandle}; -type SubsystemFuture = dyn Future> + Send + 'static; -type SubsystemFunction = - dyn FnOnce(SubsystemHandle) -> Pin>> + 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. @@ -30,7 +21,6 @@ type SubsystemFunction = /// /// struct MySubsystem; /// -/// #[async_trait::async_trait] /// impl IntoSubsystem for MySubsystem { /// async fn run(self, subsys: SubsystemHandle) -> Result<()> { /// subsys.request_shutdown(); @@ -67,13 +57,17 @@ where /// /// For more information about subsystem functions, see /// [`SubsystemHandle::start()`](crate::SubsystemHandle::start). - async fn run(self, subsys: SubsystemHandle) -> Result<(), Err>; + fn run( + self, + subsys: SubsystemHandle, + ) -> impl std::future::Future> + Send; /// Converts the object into a type that can be passed into /// [`SubsystemHandle::start()`](crate::SubsystemHandle::start). - fn into_subsystem(self) -> Box> { - Box::new(|handle: SubsystemHandle| { - Box::pin(async move { self.run(handle).await }) - }) + fn into_subsystem( + self, + ) -> impl FnOnce(SubsystemHandle) -> Pin> + Send>> + { + |handle: SubsystemHandle| Box::pin(async move { self.run(handle).await }) } } diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 463d463..cfb1af7 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -42,7 +42,6 @@ async fn normal_shutdown() { async fn use_subsystem_struct() { struct MySubsystem; - #[async_trait::async_trait] impl IntoSubsystem for MySubsystem { async fn run(self, subsys: SubsystemHandle) -> BoxedResult { subsys.on_shutdown_requested().await; From 35caf0acdc8ea348858faf4d85e9ca02dc554d15 Mon Sep 17 00:00:00 2001 From: Finomnis Date: Sat, 2 Dec 2023 23:06:54 +0100 Subject: [PATCH 2/2] Add rustfmt and clippy to nightly toolchain --- rust-toolchain.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 5d56faf..8e275b7 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,3 @@ [toolchain] channel = "nightly" +components = ["rustfmt", "clippy"]