Skip to main content

gix_macros/
lib.rs

1//! A crate of useful macros used in `gix` primarily.
2//!
3//! Note that within `gix-*` crates, monomorphization should never be used for convenience, but only for performance
4//! reasons. And in the latter case, manual denomophization should be considered if the trait in questions isn't called
5//! often enough or measurements indicate that `&dyn Trait` is increasing the runtime. Thus, `gix-*` crates should probably
6//! by default prefer using `&dyn` unless measurements indicate otherwise.
7use proc_macro::TokenStream;
8
9/// Turn async-shaped Rust code into its blocking equivalent by removing `async` and `.await` tokens.
10#[proc_macro_attribute]
11pub fn sync(_attrs: TokenStream, input: TokenStream) -> TokenStream {
12    bisync::sync(input.into()).into()
13}
14
15/// Keep an item unchanged when selecting one side of a shared blocking and async implementation.
16#[proc_macro_attribute]
17pub fn keep(_attrs: TokenStream, input: TokenStream) -> TokenStream {
18    input
19}
20
21/// Remove an item when selecting one side of a shared blocking and async implementation.
22#[proc_macro_attribute]
23pub fn discard(_attrs: TokenStream, _input: TokenStream) -> TokenStream {
24    TokenStream::new()
25}
26
27/// Expose an item only within its crate unless the consuming crate enables `unstable`.
28///
29/// Supply `#[gix_macros::unstable(since = "YYYY-MM-DD")]` with the date the item was introduced.
30/// The date must be a valid calendar date. When enabled, the instability note follows the first
31/// nonblank line of literal documentation so it appears in rustdoc overviews. If the macro cannot
32/// edit the summary, it adds a separate note instead.
33/// Usually the date alone is enough; add an optional, nonempty `reason = "..."` only to explain
34/// a non-obvious rationale. The gate also applies directly to file-backed modules
35/// (`pub mod name;`), requiring Rust 1.99 or newer.
36/// On older compilers, use an inline module body containing `include!("name.rs")` instead.
37/// Inherent implementations retain their public members as `pub(crate)` when disabled;
38/// trait implementations are omitted instead, since Rust has no private trait implementations.
39/// Macro invocations within gated inherent implementations are rejected because their generated
40/// members' visibility cannot be restricted; gate the generated members themselves instead.
41#[proc_macro_attribute]
42pub fn unstable(attrs: TokenStream, input: TokenStream) -> TokenStream {
43    unstable::item(attrs.into(), input.into())
44        .unwrap_or_else(syn::Error::into_compile_error)
45        .into()
46}
47
48/// Gate selected public struct fields while retaining their storage and access within the crate.
49///
50/// Mark each field with `#[unstable(since = "YYYY-MM-DD")]`, dating its introduction.
51/// Add an optional, nonempty `reason = "..."` only when the rationale is not obvious.
52/// With the consuming crate's `unstable` feature enabled, the field remains public and gets the
53/// same documentation note as [`unstable`]. Otherwise it becomes `pub(crate)`.
54/// All unmarked fields are unchanged.
55#[proc_macro_attribute]
56pub fn unstable_fields(attrs: TokenStream, input: TokenStream) -> TokenStream {
57    unstable::fields(attrs.into(), input.into())
58        .unwrap_or_else(syn::Error::into_compile_error)
59        .into()
60}
61
62/// When applied to functions or methods, it will turn it into a wrapper that will immediately call
63/// a de-monomorphized implementation (i.e. one that uses `&dyn Trait`).
64///
65/// That way, the landing-pads for convenience will be as small as possible which then delegate to a single
66/// function or method for implementation.
67///
68/// The parameters using the following traits can be de-monomorphized:
69///
70/// * `Into`
71/// * `AsRef`
72/// * `AsMut`
73#[proc_macro_attribute]
74pub fn momo(_attrs: TokenStream, input: TokenStream) -> TokenStream {
75    momo::inner(input.into()).into()
76}
77
78mod bisync;
79mod momo;
80mod unstable;