2 use std::marker::PhantomData;
3 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 pub fn new<T: Generator<Yield = YieldType<I, A>, Return = R> + 'static>(
32 let mut result = PinnedGenerator { generator: Box::pin(generator) };
34 // Run it to the first yield to set it up
35 let init = match Pin::new(&mut result.generator).resume() {
36 GeneratorState::Yielded(YieldType::Initial(y)) => y,
43 #[cfg(not(bootstrap))]
44 pub fn new<T: Generator<Yield = YieldType<I, A>, Return = R> + 'static>(
47 let mut result = PinnedGenerator { generator: Box::pin(generator) };
49 // Run it to the first yield to set it up
50 let init = match Pin::new(&mut result.generator).resume(()) {
51 GeneratorState::Yielded(YieldType::Initial(y)) => y,
59 pub unsafe fn access(&mut self, closure: *mut dyn FnMut()) {
60 BOX_REGION_ARG.with(|i| {
61 i.set(Action::Access(AccessAction(closure)));
64 // Call the generator, which in turn will call the closure in BOX_REGION_ARG
65 if let GeneratorState::Complete(_) = Pin::new(&mut self.generator).resume() {
70 #[cfg(not(bootstrap))]
71 pub unsafe fn access(&mut self, closure: *mut dyn FnMut()) {
72 BOX_REGION_ARG.with(|i| {
73 i.set(Action::Access(AccessAction(closure)));
76 // Call the generator, which in turn will call the closure in BOX_REGION_ARG
77 if let GeneratorState::Complete(_) = Pin::new(&mut self.generator).resume(()) {
83 pub fn complete(&mut self) -> R {
84 // Tell the generator we want it to complete, consuming it and yielding a result
85 BOX_REGION_ARG.with(|i| i.set(Action::Complete));
87 let result = Pin::new(&mut self.generator).resume();
88 if let GeneratorState::Complete(r) = result { r } else { panic!() }
91 #[cfg(not(bootstrap))]
92 pub fn complete(&mut self) -> R {
93 // Tell the generator we want it to complete, consuming it and yielding a result
94 BOX_REGION_ARG.with(|i| i.set(Action::Complete));
96 let result = Pin::new(&mut self.generator).resume(());
97 if let GeneratorState::Complete(r) = result { r } else { panic!() }
102 pub struct Marker<T>(PhantomData<T>);
105 pub unsafe fn new() -> Self {
110 pub enum YieldType<I, A> {
116 #[allow_internal_unstable(fn_traits)]
117 macro_rules! declare_box_region_type {
121 for($($lifetimes:tt)*),
122 ($($args:ty),*) -> ($reti:ty, $retc:ty)
124 $v struct $name($crate::box_region::PinnedGenerator<
126 for<$($lifetimes)*> fn(($($args,)*)),
131 fn new<T: ::std::ops::Generator<Yield = $yield_type, Return = $retc> + 'static>(
134 let (initial, pinned) = $crate::box_region::PinnedGenerator::new(generator);
135 (initial, $name(pinned))
138 $v fn access<F: for<$($lifetimes)*> FnOnce($($args,)*) -> R, R>(&mut self, f: F) -> R {
139 // Turn the FnOnce closure into *mut dyn FnMut()
140 // so we can pass it in to the generator using the BOX_REGION_ARG thread local
143 let mut_f: &mut dyn for<$($lifetimes)*> FnMut(($($args,)*)) =
145 let f = f.take().unwrap();
146 r = Some(FnOnce::call_once(f, args));
148 let mut_f = mut_f as *mut dyn for<$($lifetimes)*> FnMut(($($args,)*));
150 // Get the generator to call our closure
152 self.0.access(::std::mem::transmute(mut_f));
159 $v fn complete(mut self) -> $retc {
163 fn initial_yield(value: $reti) -> $yield_type {
164 $crate::box_region::YieldType::Initial(value)
169 ($v:vis $name: ident, for($($lifetimes:tt)*), ($($args:ty),*) -> ($reti:ty, $retc:ty)) => {
170 declare_box_region_type!(
172 $crate::box_region::YieldType<$reti, for<$($lifetimes)*> fn(($($args,)*))>,
174 ($($args),*) -> ($reti, $retc)
180 #[allow_internal_unstable(fn_traits)]
181 macro_rules! box_region_allow_access {
182 (for($($lifetimes:tt)*), ($($args:ty),*), ($($exprs:expr),*) ) => {
184 match $crate::box_region::BOX_REGION_ARG.with(|i| i.get()) {
185 $crate::box_region::Action::Access(accessor) => {
186 let accessor: &mut dyn for<$($lifetimes)*> FnMut($($args),*) = unsafe {
187 ::std::mem::transmute(accessor.get())
189 (*accessor)(($($exprs),*));
191 let marker = $crate::box_region::Marker::<
192 for<$($lifetimes)*> fn(($($args,)*))
194 yield $crate::box_region::YieldType::Accessor(marker)
197 $crate::box_region::Action::Complete => break,