1
Fork 0

Inline the rest of box_region

This commit is contained in:
bjorn3 2021-05-31 15:20:50 +02:00
parent 86b3ebe2da
commit 99e112d282
3 changed files with 79 additions and 104 deletions

View file

@ -1,80 +0,0 @@
//! This module provides a way to deal with self-referential data.
//!
//! The main idea is to allocate such data in a generator frame and then
//! give access to it by executing user-provided closures inside that generator.
//! The module provides a safe abstraction for the latter task.
//!
//! The interface consists of two exported macros meant to be used together:
//! * `declare_box_region_type` wraps a generator inside a struct with `access`
//! method which accepts closures.
//! * `box_region_allow_access` is a helper which should be called inside
//! a generator to actually execute those closures.
use std::marker::PhantomData;
use std::ops::{Generator, GeneratorState};
use std::pin::Pin;
#[derive(Copy, Clone)]
pub struct AccessAction(*mut dyn FnMut());
impl AccessAction {
pub fn get(self) -> *mut dyn FnMut() {
self.0
}
}
#[derive(Copy, Clone)]
pub enum Action {
Initial,
Access(AccessAction),
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)]
pub struct Marker<T>(PhantomData<T>);
impl<T> Marker<T> {
pub unsafe fn new() -> Self {
Marker(PhantomData)
}
}
pub enum YieldType<I, A> {
Initial(I),
Accessor(Marker<A>),
}

View file

@ -10,7 +10,6 @@
#![feature(array_windows)] #![feature(array_windows)]
#![feature(control_flow_enum)] #![feature(control_flow_enum)]
#![feature(in_band_lifetimes)] #![feature(in_band_lifetimes)]
#![feature(generator_trait)]
#![feature(min_specialization)] #![feature(min_specialization)]
#![feature(auto_traits)] #![feature(auto_traits)]
#![feature(nll)] #![feature(nll)]
@ -63,7 +62,6 @@ macro_rules! unlikely {
pub mod base_n; pub mod base_n;
pub mod binary_search_util; pub mod binary_search_util;
pub mod box_region;
pub mod captures; pub mod captures;
pub mod flock; pub mod flock;
pub mod functor; pub mod functor;

View file

@ -47,7 +47,10 @@ use std::cell::RefCell;
use std::ffi::OsString; use std::ffi::OsString;
use std::io::{self, BufWriter, Write}; use std::io::{self, BufWriter, Write};
use std::lazy::SyncLazy; use std::lazy::SyncLazy;
use std::marker::PhantomData;
use std::ops::{Generator, GeneratorState};
use std::path::PathBuf; use std::path::PathBuf;
use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
use std::{env, fs, iter}; use std::{env, fs, iter};
@ -85,27 +88,85 @@ fn count_nodes(krate: &ast::Crate) -> usize {
counter.count counter.count
} }
#[derive(Copy, Clone)]
pub struct AccessAction(*mut dyn FnMut());
impl AccessAction {
pub fn get(self) -> *mut dyn FnMut() {
self.0
}
}
#[derive(Copy, Clone)]
pub enum Action {
Initial,
Access(AccessAction),
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)]
pub struct Marker<T>(PhantomData<T>);
impl<T> Marker<T> {
pub unsafe fn new() -> Self {
Marker(PhantomData)
}
}
pub enum YieldType<I, A> {
Initial(I),
Accessor(Marker<A>),
}
pub struct BoxedResolver( pub struct BoxedResolver(
rustc_data_structures::box_region::PinnedGenerator< PinnedGenerator<Result<ast::Crate>, fn(&mut Resolver<'_>), ResolverOutputs>,
Result<ast::Crate>,
fn(&mut Resolver<'_>),
ResolverOutputs,
>,
); );
impl BoxedResolver { impl BoxedResolver {
fn new<T>(generator: T) -> (Result<ast::Crate>, Self) fn new<T>(generator: T) -> (Result<ast::Crate>, Self)
where where
T: ::std::ops::Generator< T: ::std::ops::Generator<
rustc_data_structures::box_region::Action, Action,
Yield = rustc_data_structures::box_region::YieldType< Yield = YieldType<Result<ast::Crate>, fn(&mut Resolver<'_>)>,
Result<ast::Crate>,
fn(&mut Resolver<'_>),
>,
Return = ResolverOutputs, Return = ResolverOutputs,
> + 'static, > + 'static,
{ {
let (initial, pinned) = rustc_data_structures::box_region::PinnedGenerator::new(generator); let (initial, pinned) = PinnedGenerator::new(generator);
(initial, BoxedResolver(pinned)) (initial, BoxedResolver(pinned))
} }
@ -135,9 +196,8 @@ impl BoxedResolver {
fn initial_yield( fn initial_yield(
value: Result<ast::Crate>, value: Result<ast::Crate>,
) -> rustc_data_structures::box_region::YieldType<Result<ast::Crate>, fn(&mut Resolver<'_>)> ) -> YieldType<Result<ast::Crate>, fn(&mut Resolver<'_>)> {
{ YieldType::Initial(value)
rustc_data_structures::box_region::YieldType::Initial(value)
} }
} }
@ -186,20 +246,17 @@ pub fn configure_and_expand(
loop { loop {
match action { match action {
rustc_data_structures::box_region::Action::Access(accessor) => { Action::Access(accessor) => {
let accessor: &mut dyn FnMut(&mut Resolver<'_>) = let accessor: &mut dyn FnMut(&mut Resolver<'_>) =
unsafe { ::std::mem::transmute(accessor.get()) }; unsafe { ::std::mem::transmute(accessor.get()) };
(*accessor)(&mut resolver); (*accessor)(&mut resolver);
unsafe { unsafe {
let marker = rustc_data_structures::box_region::Marker::< let marker = Marker::<fn(&mut Resolver<'_>)>::new();
fn(&mut Resolver<'_>), action = yield YieldType::Accessor(marker);
>::new();
action =
yield rustc_data_structures::box_region::YieldType::Accessor(marker);
}; };
} }
rustc_data_structures::box_region::Action::Complete => break, Action::Complete => break,
rustc_data_structures::box_region::Action::Initial => { Action::Initial => {
panic!("unexpected box_region action: Initial") panic!("unexpected box_region action: Initial")
} }
} }