pub(crate) struct Rewriter<'a> {
pub options: &'a Options,
pub scope: Scope,
pub report: Report,
pub in_async: bool,
pub fresh: usize,
pub refusal: Option<String>,
pub loop_check: String,
pub loop_lines: Vec<(usize, usize)>,
}Expand description
The state of one #[psync::graph] expansion.
Fields§
§options: &'a Options§scope: ScopeMutability facts and hints for the whole function.
report: ReportEverything analyzed so far, for explain and tooling.
in_async: boolInside an async fn, async block or async closure: only there do awaits occur.
fresh: usizeCounter that keeps generated names unique within the function.
refusal: Option<String>Why the last loop statement could not run ahead, for the loop’s report.
loop_check: StringHow the last prefetched loop’s per-item files are kept apart, for its report.
loop_lines: Vec<(usize, usize)>The source lines of the last prefetched loop’s prefix, for its report.
Implementations§
Source§impl Rewriter<'_>
impl Rewriter<'_>
Sourcepub(crate) fn rewrite_loop(&mut self, f: &ExprForLoop) -> Option<Expr>
pub(crate) fn rewrite_loop(&mut self, f: &ExprForLoop) -> Option<Expr>
Rewrite a for loop by prefetching or by fission then prefetching; None (recorded in
the report) leaves it as written.
Sourcepub(crate) fn rewrite_while(&mut self, w: &ExprWhile) -> Option<Expr>
pub(crate) fn rewrite_while(&mut self, w: &ExprWhile) -> Option<Expr>
Rewrite a while/while let loop by fission. Only with #[psync::independent]: the
condition must be driven by the statements before the first await, which tokens cannot show.
Sourcefn prefetch(&mut self, f: &ExprForLoop) -> Result<(Expr, Vec<String>), String>
fn prefetch(&mut self, f: &ExprForLoop) -> Result<(Expr, Vec<String>), String>
Prefetch the loop’s awaited prefix. Returns the replacement and the prefetched labels.
Sourcefn fission(&mut self, f: &ExprForLoop) -> Option<(Expr, Vec<String>)>
fn fission(&mut self, f: &ExprForLoop) -> Option<(Expr, Vec<String>)>
Split off the leading non-awaiting statements into a first loop, then prefetch the rest.
Sourcefn fission_with(
&mut self,
head: TokenStream,
loop_label: &Option<Label>,
attrs: &[Attribute],
pattern: &[(Ident, bool)],
body: &Block,
) -> Option<(Expr, Vec<String>)>
fn fission_with( &mut self, head: TokenStream, loop_label: &Option<Label>, attrs: &[Attribute], pattern: &[(Ident, bool)], body: &Block, ) -> Option<(Expr, Vec<String>)>
Fission for a loop with the given header (for p in e or while c).
Sourcefn take_prefix(
&mut self,
body: Vec<Stmt>,
allow_guard: bool,
scope: &LoopScope,
) -> (Vec<Node>, Vec<Stmt>)
fn take_prefix( &mut self, body: Vec<Stmt>, allow_guard: bool, scope: &LoopScope, ) -> (Vec<Node>, Vec<Stmt>)
Split a loop body (or a guarded branch) into the statements that may run ahead of earlier iterations and the rest.
Bindings and awaits are taken from the front of the body. Further on, an awaited statement
may still join if nothing left behind before it could exit the iteration or touches what it
touches. Awaits inside a scrutinee, call arguments or an if branch are hoisted out first.
Sourcefn invert_guard(
&mut self,
cond: &Expr,
after: Vec<Stmt>,
scope: &LoopScope,
) -> Option<(Node, Vec<Stmt>)>
fn invert_guard( &mut self, cond: &Expr, after: Vec<Stmt>, scope: &LoopScope, ) -> Option<(Node, Vec<Stmt>)>
For an exit guard if c { ..; continue; } with pure c, take the awaited prefix of the
statements after it, guarded by !c: returns the prefix node
let g = if c { None } else { Some(<prefix chain>) }; and the statements that replace
after (unpacking g, which is Some once the guard has not exited).
Sourcefn prefix_candidate(
&mut self,
stmt: &Stmt,
allow_guard: bool,
scope: &LoopScope,
) -> Option<(Vec<Node>, Option<Stmt>)>
fn prefix_candidate( &mut self, stmt: &Stmt, allow_guard: bool, scope: &LoopScope, ) -> Option<(Vec<Node>, Option<Stmt>)>
The prefix nodes a statement contributes, and what stays in the body in its place.
Sourcepub(crate) fn hoist_branch(&mut self, stmt: &Stmt) -> Option<(Stmt, Stmt)>
pub(crate) fn hoist_branch(&mut self, stmt: &Stmt) -> Option<(Stmt, Stmt)>
if c { <awaited prefix>; rest } else { .. } becomes a prefix binding
let g = if c { Some(<prefix chain>) } else { None }; and the statement
if let Some(..) = g { <re-raise exits>; rest } else { .. }.
Outside loops: split if c { let x = f.await?; rest } into a guarded statement running the
branch’s leading awaited work (let g = if c { Some(..) } else { None };) and the branch’s
remainder, so that work can overlap with the statements before the if.
fn hoist_guard( &mut self, stmt: &Stmt, scope: &LoopScope, ) -> Option<(Stmt, Stmt)>
Source§impl Rewriter<'_>
impl Rewriter<'_>
Sourcefn rewrites_loops(&self) -> bool
fn rewrites_loops(&self) -> bool
Whether loops are rewritten: always, except under baseline = resync.
Sourcefn rewrite_stmts(&mut self, stmts: Vec<Stmt>) -> Vec<Stmt>
fn rewrite_stmts(&mut self, stmts: Vec<Stmt>) -> Vec<Stmt>
Rewrite one block’s statements, or return them unchanged if nothing becomes concurrent.
Sourcefn hoist_tail(&mut self, stmts: Vec<Stmt>) -> Vec<Stmt>
fn hoist_tail(&mut self, stmts: Vec<Stmt>) -> Vec<Stmt>
An awaited block value (foo().await? last) becomes let t = foo().await?; t, so the await
can overlap earlier work while the value still comes last.
Sourcefn expose_awaits(&mut self, stmts: Vec<Stmt>) -> Vec<Stmt>
fn expose_awaits(&mut self, stmts: Vec<Stmt>) -> Vec<Stmt>
Split statements that hide an await inside other work (a match scrutinee, a call
argument, a method receiver, the leading work of an if branch) into a binding of the
awaited value and the rest of the statement, repeatedly, since a hoisted value may itself
hide an await.