Skip to content

Commit

Permalink
Fix another compatibility issue between IntoSubsystem and custom erro…
Browse files Browse the repository at this point in the history
…r types
  • Loading branch information
Finomnis committed May 9, 2022
1 parent 68be4be commit 3ad3987
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/18_error_type_passthrough.rs
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ async fn subsys5(_subsys: SubsystemHandle<MyError>) -> Result<(), MyError> {
struct Subsys6;

#[async_trait::async_trait]
impl IntoSubsystem<MyError> for Subsys6 {
impl IntoSubsystem<MyError, MyError> for Subsys6 {
async fn run(self, _subsys: SubsystemHandle<MyError>) -> Result<(), MyError> {
log::info!("Subsystem6 started.");
sleep(Duration::from_millis(200)).await;
19 changes: 11 additions & 8 deletions src/into_subsystem.rs
Original file line number Diff line number Diff line change
@@ -3,11 +3,11 @@ use std::pin::Pin;

use async_trait::async_trait;

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

type SubsystemFuture<Err> = dyn Future<Output = Result<(), Err>> + Send + 'static;
type SubsystemFunction<Err> =
dyn FnOnce(SubsystemHandle<Err>) -> Pin<Box<SubsystemFuture<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.
@@ -49,10 +49,11 @@ type SubsystemFunction<Err> =
/// }
/// ```
///
pub trait IntoSubsystem<Err>
pub trait IntoSubsystem<Err, ErrWrapper = BoxedError>
where
Self: Sized + Send + Sync + 'static,
Err: std::fmt::Debug + std::fmt::Display + Send + Sync + 'static,
Err: ErrTypeTraits,
ErrWrapper: ErrTypeTraits,
{
/// The logic of the subsystem.
///
@@ -62,11 +63,13 @@ where
///
/// For more information about subsystem functions, see
/// [`Toplevel::start()`](crate::Toplevel::start) and [`SubsystemHandle::start()`](crate::SubsystemHandle::start).
async fn run(self, subsys: SubsystemHandle<Err>) -> Result<(), Err>;
async fn run(self, subsys: SubsystemHandle<ErrWrapper>) -> Result<(), Err>;

/// Converts the object into a type that can be passed into
/// [`Toplevel::start()`](crate::Toplevel::start) and [`SubsystemHandle::start()`](crate::SubsystemHandle::start).
fn into_subsystem(self) -> Box<SubsystemFunction<Err>> {
Box::new(|handle: SubsystemHandle<Err>| Box::pin(async move { self.run(handle).await }))
fn into_subsystem(self) -> Box<SubsystemFunction<Err, ErrWrapper>> {
Box::new(|handle: SubsystemHandle<ErrWrapper>| {
Box::pin(async move { self.run(handle).await })
})
}
}

0 comments on commit 3ad3987

Please sign in to comment.