psync_macros/lib.rs
1//! `#[psync::graph]`: run the independent awaits of an async function concurrently.
2//! The analysis and code generation live in `psync-graph`; see its documentation.
3use proc_macro::TokenStream;
4
5/// Build the function's await dependency graph and run independent work concurrently.
6///
7/// Options:
8/// - `bound = N` caps concurrent loop iterations (default 16);
9/// - `explain` prints the graph at compile time;
10/// - `off` compiles the function as written (for comparisons; `PSYNC_GRAPH=off` does it for all);
11/// - `strict` treats awaited calls psync cannot classify as writes to an unknown resource;
12/// - `calls = read | write | write_independent | independent` marks the whole function: how
13/// unclassified awaited calls are taken (overriding `strict`), and, for the `independent` forms,
14/// that every statement's effects commute with the others';
15/// - `speculate` lets writes run before earlier exits everywhere in the function;
16/// - `effects(read(a, ..), write(b, ..), ordered(c, ..))` names more verbs for the effect analysis;
17/// - `yield_every = N` makes every loop of async code yield to the executor every `N` iterations
18/// (preemption points for cooperative executors);
19/// - `crate = path` names the psync crate in generated code.
20///
21/// Hints inside the function: `#[psync::seq]`, `#[psync::fresh]`, `#[psync::independent]`,
22/// `#[psync::speculate]`.
23///
24/// ```ignore
25/// #[psync::graph(bound = 8)]
26/// async fn sizes(store: &Store, keys: &[String]) -> Result<u64, Error> {
27/// let mut total = 0;
28/// for key in keys {
29/// total += store.head(key).await?.size; // up to 8 HEADs in flight
30/// }
31/// Ok(total)
32/// }
33/// ```
34#[proc_macro_attribute]
35pub fn graph(attr: TokenStream, item: TokenStream) -> TokenStream {
36 psync_graph::expand(attr.into(), item.into())
37 .unwrap_or_else(|e| e.to_compile_error())
38 .into()
39}
40
41/// Hint: keep this statement in its original position inside a `#[psync::graph]` function.
42/// Outside one, stable Rust rejects a custom
43/// attribute on a statement or expression (E0658): the graph macro removes it before the compiler
44/// sees it.
45#[proc_macro_attribute]
46pub fn seq(_attr: TokenStream, item: TokenStream) -> TokenStream {
47 item
48}
49
50/// Hint on a `let`: each concurrent branch that uses this binding evaluates its initializer
51/// again instead of sharing the value, e.g. `#[psync::fresh] let pool = &mut context.pool();`
52/// gives every branch its own pooled connection.
53/// Outside `#[psync::graph]`, stable Rust rejects a custom
54/// attribute on a statement or expression (E0658): the graph macro removes it before the compiler
55/// sees it.
56#[proc_macro_attribute]
57pub fn fresh(_attr: TokenStream, item: TokenStream) -> TokenStream {
58 item
59}
60
61/// Hint on a `for` loop: the programmer asserts that an iteration's awaited work does not depend
62/// on what earlier iterations wrote, so psync may run it ahead even where it cannot prove that.
63/// On a statement: its effects on outside resources commute with those of the statements around
64/// it (two writes to different objects through one client), so they need not keep their order.
65///
66/// Outside `#[psync::graph]`, stable Rust rejects a custom
67/// attribute on a statement or expression (E0658): the graph macro removes it before the compiler
68/// sees it.
69#[proc_macro_attribute]
70pub fn independent(_attr: TokenStream, item: TokenStream) -> TokenStream {
71 item
72}
73
74/// Hint on a statement of a synchronous `#[psync::graph]` function: it is worth a thread (a
75/// slow call psync cannot recognize by its path, such as a helper that downloads a file). psync
76/// may then run it on another thread beside independent statements. Outside a synchronous
77/// `#[psync::graph]` function it has no effect, and on a statement it does not compile (E0658).
78#[proc_macro_attribute]
79pub fn par(_attr: TokenStream, item: TokenStream) -> TokenStream {
80 item
81}
82
83/// Hint on a statement or `for` loop: its writes (to files, services, the network) may run ahead
84/// of an earlier `?`, `return` or `break`, or for later loop items before an earlier item ends the
85/// loop. The programmer asserts that performing them when sequential code would not have is
86/// harmless (idempotent uploads, cache fills).
87/// Outside `#[psync::graph]`, stable Rust rejects a custom
88/// attribute on a statement or expression (E0658): the graph macro removes it before the compiler
89/// sees it.
90#[proc_macro_attribute]
91pub fn speculate(_attr: TokenStream, item: TokenStream) -> TokenStream {
92 item
93}