Skip to content

Commit

Permalink
Replaced the Fut trait with a struct that can be created from any Future
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v2d0g committed Oct 17, 2019
1 parent 57a4ff6 commit b17bef2
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions bastion/src/children.rs
Expand Up @@ -10,7 +10,7 @@ use runtime::task::JoinHandle;
use std::any::Any;
use std::fmt::Debug;
use std::future::Future;
use std::panic::UnwindSafe;
use std::panic::AssertUnwindSafe;
use std::pin::Pin;
use std::task::Poll;

Expand All @@ -29,12 +29,22 @@ where
}
}

pub trait Closure: Fn(BastionContext, Box<dyn Message>) -> Pin<Box<dyn Fut>> + Shell {}
impl<T> Closure for T where T: Fn(BastionContext, Box<dyn Message>) -> Pin<Box<dyn Fut>> + Shell {}
pub trait Closure: Fn(BastionContext, Box<dyn Message>) -> Fut + Shell {}
impl<T> Closure for T where T: Fn(BastionContext, Box<dyn Message>) -> Fut + Shell {}

// TODO: Ok(T) & Err(E)
pub trait Fut: Future<Output = Result<(), ()>> + Send + UnwindSafe {}
impl<T> Fut for T where T: Future<Output = Result<(), ()>> + Send + UnwindSafe {}
type FutInner = dyn Future<Output = Result<(), ()>> + Send;

pub struct Fut(Pin<Box<FutInner>>);

impl<T> From<T> for Fut
where
T: Future<Output = Result<(), ()>> + Send + 'static,
{
fn from(fut: T) -> Fut {
Fut(Box::pin(fut))
}
}

pub(super) struct Children {
thunk: Box<dyn Closure>,
Expand All @@ -44,7 +54,7 @@ pub(super) struct Children {
}

pub(super) struct Child {
exec: CatchUnwind<Pin<Box<dyn Fut>>>,
exec: CatchUnwind<AssertUnwindSafe<Pin<Box<FutInner>>>>,
bcast: Broadcast,
state: Qutex<ContextState>,
}
Expand Down Expand Up @@ -126,7 +136,7 @@ impl Children {
let parent = self.bcast.sender().clone();
let ctx = BastionContext::new(id, parent, state.clone());

let exec = thunk(ctx, msg)
let exec = AssertUnwindSafe(thunk(ctx, msg).0)
.catch_unwind();

let child = Child { exec, bcast, state };
Expand Down

0 comments on commit b17bef2

Please sign in to comment.