2 use std::marker::PhantomData;
4 use std::ops::{Generator, GeneratorState};
7 pub struct AccessAction(*mut dyn FnMut());
10 pub fn get(self) -> *mut dyn FnMut() {
15 #[derive(Copy, Clone)]
21 thread_local!(pub static BOX_REGION_ARG: Cell<Action> = Cell::new(Action::Complete));
23 pub struct PinnedGenerator<I, A, R> {
24 generator: Pin<Box<dyn Generator<Yield = YieldType<I, A>, Return = R>>>
27 impl<I, A, R> PinnedGenerator<I, A, R> {
29 T: Generator<Yield = YieldType<I, A>, Return = R> + 'static
30 >(generator: T) -> (I, Self) {
31 let mut result = PinnedGenerator {
32 generator: Box::pin(generator)
35 // Run it to the first yield to set it up
36 let init = match Pin::new(&mut result.generator).resume() {
37 GeneratorState::Yielded(
46 pub unsafe fn access(&mut self, closure: *mut dyn FnMut()) {
47 BOX_REGION_ARG.with(|i| {
48 i.set(Action::Access(AccessAction(closure)));
51 // Call the generator, which in turn will call the closure in BOX_REGION_ARG
52 if let GeneratorState::Complete(_) = Pin::new(&mut self.generator).resume() {
57 pub fn complete(&mut self) -> R {
58 // Tell the generator we want it to complete, consuming it and yielding a result
59 BOX_REGION_ARG.with(|i| {
60 i.set(Action::Complete)
63 let result = Pin::new(&mut self.generator).resume();
64 if let GeneratorState::Complete(r) = result {
73 pub struct Marker<T>(PhantomData<T>);
76 pub unsafe fn new() -> Self {
81 pub enum YieldType<I, A> {
87 #[allow_internal_unstable(fn_traits)]
88 macro_rules! declare_box_region_type {
92 for($($lifetimes:tt)*),
93 ($($args:ty),*) -> ($reti:ty, $retc:ty)
95 $v struct $name($crate::box_region::PinnedGenerator<
97 for<$($lifetimes)*> fn(($($args,)*)),
102 fn new<T: ::std::ops::Generator<Yield = $yield_type, Return = $retc> + 'static>(
105 let (initial, pinned) = $crate::box_region::PinnedGenerator::new(generator);
106 (initial, $name(pinned))
109 $v fn access<F: for<$($lifetimes)*> FnOnce($($args,)*) -> R, R>(&mut self, f: F) -> R {
110 // Turn the FnOnce closure into *mut dyn FnMut()
111 // so we can pass it in to the generator using the BOX_REGION_ARG thread local
114 let mut_f: &mut dyn for<$($lifetimes)*> FnMut(($($args,)*)) =
116 let f = f.take().unwrap();
117 r = Some(FnOnce::call_once(f, args));
119 let mut_f = mut_f as *mut dyn for<$($lifetimes)*> FnMut(($($args,)*));
121 // Get the generator to call our closure
123 self.0.access(::std::mem::transmute(mut_f));
130 $v fn complete(mut self) -> $retc {
134 fn initial_yield(value: $reti) -> $yield_type {
135 $crate::box_region::YieldType::Initial(value)
140 ($v:vis $name: ident, for($($lifetimes:tt)*), ($($args:ty),*) -> ($reti:ty, $retc:ty)) => {
141 declare_box_region_type!(
143 $crate::box_region::YieldType<$reti, for<$($lifetimes)*> fn(($($args,)*))>,
145 ($($args),*) -> ($reti, $retc)
151 #[allow_internal_unstable(fn_traits)]
152 macro_rules! box_region_allow_access {
153 (for($($lifetimes:tt)*), ($($args:ty),*), ($($exprs:expr),*) ) => {
155 match $crate::box_region::BOX_REGION_ARG.with(|i| i.get()) {
156 $crate::box_region::Action::Access(accessor) => {
157 let accessor: &mut dyn for<$($lifetimes)*> FnMut($($args),*) = unsafe {
158 ::std::mem::transmute(accessor.get())
160 (*accessor)(($($exprs),*));
162 let marker = $crate::box_region::Marker::<
163 for<$($lifetimes)*> fn(($($args,)*))
165 yield $crate::box_region::YieldType::Accessor(marker)
168 $crate::box_region::Action::Complete => break,