Inline PinnedGenerator
This commit is contained in:
parent
99e112d282
commit
1b7ec34878
1 changed files with 29 additions and 42 deletions
|
@ -104,41 +104,6 @@ pub enum Action {
|
||||||
Complete,
|
Complete,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PinnedGenerator<I, A, R> {
|
|
||||||
generator: Pin<Box<dyn Generator<Action, Yield = YieldType<I, A>, Return = R>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<I, A, R> PinnedGenerator<I, A, R> {
|
|
||||||
pub fn new<T: Generator<Action, Yield = YieldType<I, A>, Return = R> + 'static>(
|
|
||||||
generator: T,
|
|
||||||
) -> (I, Self) {
|
|
||||||
let mut result = PinnedGenerator { generator: Box::pin(generator) };
|
|
||||||
|
|
||||||
// Run it to the first yield to set it up
|
|
||||||
let init = match Pin::new(&mut result.generator).resume(Action::Initial) {
|
|
||||||
GeneratorState::Yielded(YieldType::Initial(y)) => y,
|
|
||||||
_ => panic!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
(init, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub unsafe fn access(&mut self, closure: *mut dyn FnMut()) {
|
|
||||||
// Call the generator, which in turn will call the closure
|
|
||||||
if let GeneratorState::Complete(_) =
|
|
||||||
Pin::new(&mut self.generator).resume(Action::Access(AccessAction(closure)))
|
|
||||||
{
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn complete(&mut self) -> R {
|
|
||||||
// Tell the generator we want it to complete, consuming it and yielding a result
|
|
||||||
let result = Pin::new(&mut self.generator).resume(Action::Complete);
|
|
||||||
if let GeneratorState::Complete(r) = result { r } else { panic!() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
pub struct Marker<T>(PhantomData<T>);
|
pub struct Marker<T>(PhantomData<T>);
|
||||||
|
|
||||||
|
@ -153,9 +118,17 @@ pub enum YieldType<I, A> {
|
||||||
Accessor(Marker<A>),
|
Accessor(Marker<A>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BoxedResolver(
|
pub struct BoxedResolver {
|
||||||
PinnedGenerator<Result<ast::Crate>, fn(&mut Resolver<'_>), ResolverOutputs>,
|
generator: Pin<
|
||||||
);
|
Box<
|
||||||
|
dyn Generator<
|
||||||
|
Action,
|
||||||
|
Yield = YieldType<Result<ast::Crate>, fn(&mut Resolver<'_>)>,
|
||||||
|
Return = ResolverOutputs,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
}
|
||||||
|
|
||||||
impl BoxedResolver {
|
impl BoxedResolver {
|
||||||
fn new<T>(generator: T) -> (Result<ast::Crate>, Self)
|
fn new<T>(generator: T) -> (Result<ast::Crate>, Self)
|
||||||
|
@ -166,8 +139,15 @@ impl BoxedResolver {
|
||||||
Return = ResolverOutputs,
|
Return = ResolverOutputs,
|
||||||
> + 'static,
|
> + 'static,
|
||||||
{
|
{
|
||||||
let (initial, pinned) = PinnedGenerator::new(generator);
|
let mut generator = Box::pin(generator);
|
||||||
(initial, BoxedResolver(pinned))
|
|
||||||
|
// Run it to the first yield to set it up
|
||||||
|
let init = match Pin::new(&mut generator).resume(Action::Initial) {
|
||||||
|
GeneratorState::Yielded(YieldType::Initial(y)) => y,
|
||||||
|
_ => panic!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
(init, BoxedResolver { generator })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn access<F: FnOnce(&mut Resolver<'_>) -> R, R>(&mut self, f: F) -> R {
|
pub fn access<F: FnOnce(&mut Resolver<'_>) -> R, R>(&mut self, f: F) -> R {
|
||||||
|
@ -183,7 +163,12 @@ impl BoxedResolver {
|
||||||
|
|
||||||
// Get the generator to call our closure
|
// Get the generator to call our closure
|
||||||
unsafe {
|
unsafe {
|
||||||
self.0.access(::std::mem::transmute(mut_f));
|
// Call the generator, which in turn will call the closure
|
||||||
|
if let GeneratorState::Complete(_) = Pin::new(&mut self.generator)
|
||||||
|
.resume(Action::Access(AccessAction(::std::mem::transmute(mut_f))))
|
||||||
|
{
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unwrap the result
|
// Unwrap the result
|
||||||
|
@ -191,7 +176,9 @@ impl BoxedResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn complete(mut self) -> ResolverOutputs {
|
pub fn complete(mut self) -> ResolverOutputs {
|
||||||
self.0.complete()
|
// Tell the generator we want it to complete, consuming it and yielding a result
|
||||||
|
let result = Pin::new(&mut self.generator).resume(Action::Complete);
|
||||||
|
if let GeneratorState::Complete(r) = result { r } else { panic!() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn initial_yield(
|
fn initial_yield(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue