1
Fork 0

Simplify box_region macros

This commit is contained in:
bjorn3 2021-05-31 14:51:31 +02:00
parent a50d72158e
commit 8f397bc8a0
2 changed files with 18 additions and 28 deletions

View file

@ -82,37 +82,35 @@ pub enum YieldType<I, A> {
#[macro_export] #[macro_export]
#[allow_internal_unstable(fn_traits)] #[allow_internal_unstable(fn_traits)]
macro_rules! declare_box_region_type { macro_rules! declare_box_region_type {
(impl $v:vis ($v:vis $name: ident, ($($args:ty),*) -> ($reti:ty, $retc:ty)) => {
$name: ident,
$yield_type:ty,
for($($lifetimes:tt)*),
($($args:ty),*) -> ($reti:ty, $retc:ty)
) => {
$v struct $name($crate::box_region::PinnedGenerator< $v struct $name($crate::box_region::PinnedGenerator<
$reti, $reti,
for<$($lifetimes)*> fn(($($args,)*)), fn(($($args,)*)),
$retc $retc
>); >);
impl $name { impl $name {
fn new<T: ::std::ops::Generator<$crate::box_region::Action, Yield = $yield_type, Return = $retc> + 'static>( fn new<T>(generator: T) -> ($reti, Self)
generator: T where T: ::std::ops::Generator<
) -> ($reti, Self) { $crate::box_region::Action,
Yield = $crate::box_region::YieldType<$reti, fn(($($args,)*))>,
Return = $retc,
> + 'static {
let (initial, pinned) = $crate::box_region::PinnedGenerator::new(generator); let (initial, pinned) = $crate::box_region::PinnedGenerator::new(generator);
(initial, $name(pinned)) (initial, $name(pinned))
} }
$v fn access<F: for<$($lifetimes)*> FnOnce($($args,)*) -> R, R>(&mut self, f: F) -> R { $v fn access<F: FnOnce($($args,)*) -> R, R>(&mut self, f: F) -> R {
// Turn the FnOnce closure into *mut dyn FnMut() // Turn the FnOnce closure into *mut dyn FnMut()
// so we can pass it in to the generator // so we can pass it in to the generator
let mut r = None; let mut r = None;
let mut f = Some(f); let mut f = Some(f);
let mut_f: &mut dyn for<$($lifetimes)*> FnMut(($($args,)*)) = let mut_f: &mut dyn FnMut(($($args,)*)) =
&mut |args| { &mut |args| {
let f = f.take().unwrap(); let f = f.take().unwrap();
r = Some(FnOnce::call_once(f, args)); r = Some(FnOnce::call_once(f, args));
}; };
let mut_f = mut_f as *mut dyn for<$($lifetimes)*> FnMut(($($args,)*)); let mut_f = mut_f as *mut dyn FnMut(($($args,)*));
// Get the generator to call our closure // Get the generator to call our closure
unsafe { unsafe {
@ -127,36 +125,29 @@ macro_rules! declare_box_region_type {
self.0.complete() self.0.complete()
} }
fn initial_yield(value: $reti) -> $yield_type { fn initial_yield(
value: $reti,
) -> $crate::box_region::YieldType<$reti, fn(($($args,)*))> {
$crate::box_region::YieldType::Initial(value) $crate::box_region::YieldType::Initial(value)
} }
} }
}; };
($v:vis $name: ident, for($($lifetimes:tt)*), ($($args:ty),*) -> ($reti:ty, $retc:ty)) => {
declare_box_region_type!(
impl $v $name,
$crate::box_region::YieldType<$reti, for<$($lifetimes)*> fn(($($args,)*))>,
for($($lifetimes)*),
($($args),*) -> ($reti, $retc)
);
};
} }
#[macro_export] #[macro_export]
#[allow_internal_unstable(fn_traits)] #[allow_internal_unstable(fn_traits)]
macro_rules! box_region_allow_access { macro_rules! box_region_allow_access {
(for($($lifetimes:tt)*), ($($args:ty),*), ($($exprs:expr),*), $action:ident) => { (($($args:ty),*), ($($exprs:expr),*), $action:ident) => {
loop { loop {
match $action { match $action {
$crate::box_region::Action::Access(accessor) => { $crate::box_region::Action::Access(accessor) => {
let accessor: &mut dyn for<$($lifetimes)*> FnMut($($args),*) = unsafe { let accessor: &mut dyn FnMut($($args),*) = unsafe {
::std::mem::transmute(accessor.get()) ::std::mem::transmute(accessor.get())
}; };
(*accessor)(($($exprs),*)); (*accessor)(($($exprs),*));
unsafe { unsafe {
let marker = $crate::box_region::Marker::< let marker = $crate::box_region::Marker::<
for<$($lifetimes)*> fn(($($args,)*)) fn(($($args,)*))
>::new(); >::new();
$action = yield $crate::box_region::YieldType::Accessor(marker); $action = yield $crate::box_region::YieldType::Accessor(marker);
}; };

View file

@ -87,7 +87,6 @@ fn count_nodes(krate: &ast::Crate) -> usize {
declare_box_region_type!( declare_box_region_type!(
pub BoxedResolver, pub BoxedResolver,
for(),
(&mut Resolver<'_>) -> (Result<ast::Crate>, ResolverOutputs) (&mut Resolver<'_>) -> (Result<ast::Crate>, ResolverOutputs)
); );
@ -133,7 +132,7 @@ pub fn configure_and_expand(
resolver resolver
} }
}; };
box_region_allow_access!(for(), (&mut Resolver<'_>), (&mut resolver), action); box_region_allow_access!((&mut Resolver<'_>), (&mut resolver), action);
resolver.into_outputs() resolver.into_outputs()
}); });
result.map(|k| (k, resolver)) result.map(|k| (k, resolver))