-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1 @@ | ||
use std::rc::Rc; | ||
|
||
pub struct Context<M, A> { | ||
waker: Rc<dyn Fn(Rc<dyn Fn(&mut M) -> Option<A>>)>, | ||
} | ||
|
||
pub trait Task<M, A = ()> { | ||
type Output; | ||
|
||
type State; | ||
|
||
fn build(&mut self, cx: &Context<M, A>, model: &mut M) -> (Self::Output, Self::State); | ||
|
||
fn rebuild( | ||
&mut self, | ||
cx: &Context<M, A>, | ||
model: &mut M, | ||
state: &mut Self::State, | ||
) -> Self::Output; | ||
} | ||
pub mod task; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::rc::Rc; | ||
|
||
mod then; | ||
pub use self::then::Then; | ||
|
||
pub struct Context<M, A> { | ||
waker: Rc<dyn Fn(Rc<dyn Fn(&mut M) -> Option<A>>)>, | ||
} | ||
|
||
pub trait Task<M, A = ()> { | ||
type Output; | ||
|
||
type State; | ||
|
||
fn build(&mut self, cx: &Context<M, A>, model: &mut M) -> (Self::Output, Self::State); | ||
|
||
fn rebuild( | ||
&mut self, | ||
cx: &Context<M, A>, | ||
model: &mut M, | ||
state: &mut Self::State, | ||
) -> Self::Output; | ||
|
||
fn then<F, T>(self, f: F) -> Then<Self, F, T, M> | ||
where | ||
Self: Sized + 'static, | ||
F: FnMut(&mut M, Self::Output) -> T + 'static, | ||
T: Task<M> + 'static, | ||
M: 'static, | ||
{ | ||
Then::new(self, f) | ||
} | ||
} | ||
|
||
impl<M> Task<M> for () { | ||
type Output = (); | ||
|
||
type State = (); | ||
|
||
fn build(&mut self, _cx: &Context<M, ()>, _model: &mut M) -> (Self::Output, Self::State) { | ||
((), ()) | ||
} | ||
|
||
fn rebuild( | ||
&mut self, | ||
_cx: &Context<M, ()>, | ||
_model: &mut M, | ||
_state: &mut Self::State, | ||
) -> Self::Output { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use super::{Context, Task}; | ||
use std::marker::PhantomData; | ||
|
||
pub struct Then<T1, F, T2, M> { | ||
task: T1, | ||
f: F, | ||
_marker: PhantomData<(T2, M)>, | ||
} | ||
|
||
impl<T1, F, T2, M> Then<T1, F, T2, M> { | ||
pub(super) fn new(task: T1, f: F) -> Self { | ||
Self { | ||
task, | ||
f, | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<T1, F, T2, M> Task<M> for Then<T1, F, T2, M> | ||
where | ||
T1: Task<M> + 'static, | ||
F: FnMut(&mut M, T1::Output) -> T2 + 'static, | ||
T2: Task<M> + 'static, | ||
M: 'static, | ||
{ | ||
type Output = T2::Output; | ||
|
||
type State = (T1::State, T2::State); | ||
|
||
fn build(&mut self, cx: &Context<M, ()>, model: &mut M) -> (Self::Output, Self::State) { | ||
let (output, state) = self.task.build(cx, model); | ||
let mut next = (self.f)(model, output); | ||
let (next_output, next_state) = next.build(cx, model); | ||
(next_output, (state, next_state)) | ||
} | ||
|
||
fn rebuild( | ||
&mut self, | ||
cx: &Context<M, ()>, | ||
model: &mut M, | ||
state: &mut Self::State, | ||
) -> Self::Output { | ||
let output = self.task.rebuild(cx, model, &mut state.0); | ||
let mut next = (self.f)(model, output); | ||
next.rebuild(cx, model, &mut state.1) | ||
} | ||
} |