async_codegen/rust/mod.rs
1/*
2 * Copyright © 2025 Anand Beh
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17mod syntax;
18
19/// All possible Rust editions.
20/// This is the only type in this module meant to be used as context, and not as a writable itself.
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
22#[non_exhaustive]
23pub enum Edition {
24 /// This Rust edition is declared for usability purposes. However, not all [crate::Writable]
25 /// implementations are guaranteed to work with it.
26 Rust2015,
27 Rust2018,
28 Rust2021,
29 Rust2024,
30}
31
32/// The attribute content for `allow(...)`. The tuple value must be a sequence.
33pub struct AllowLints<Lints>(pub Lints);
34
35/// The public modifier
36pub struct ModPub;
37
38/// The extern modifier, with the ABI selected as the tuple value
39pub struct ModExtern<ABI>(pub ABI);
40
41/// A let statement. This statement includes the semi-colon and a new line.
42pub struct LetStmt<Variable, Expr>(pub Variable, pub Expr);
43
44/// An item attached to an associated container, via "::".
45/// The output will look like `Cont::Item`.
46pub struct AssociatedItem<Cont, Item>(pub Cont, pub Item);
47
48/// A question mark following another expression.
49pub struct QuestionMarkAfter<Expr>(pub Expr);
50
51/// Wraps an expression in `Ok(EXPR)`.
52pub struct OkResultOf<Expr>(pub Expr);
53
54/// Uses the `as` expression to perform a qualified trait cast (ready for a method call).
55/// I.e., this will render as `<TYPE as OTHER>`.
56pub struct TypeAsTrait<Type, Trait>(pub Type, pub Trait);
57
58/// Places the expression inside an unsafe block.
59/// Adds new lines inside the brackets, wrapping the inner expression.
60pub struct UnsafeBlock<Expr>(pub Expr);
61
62/// Writes a closure.
63/// Adds new lines inside the brackets, wrapping the inner expression.
64pub struct Closure<InputVars, Expr> {
65 /// The input variables.
66 /// Should be a sequence. They will be comma separated and placed within the pipes.
67 /// To use no input variables, use [crate::common::NoOpSeq].
68 pub input_vars: InputVars,
69 /// The expression inside the closure block.
70 pub inside_block: Expr,
71}
72
73/// Performs a call to a function inside code.
74pub struct FunctionCall<Recv, FuncName, Args> {
75 /// The function receiver
76 pub receiver: Recv,
77 /// Whether the function is associated, false if it's a method
78 pub is_assoc: bool,
79 /// The function being called
80 pub function: Function<FuncName, Args>,
81}
82
83/// The base struct for a function. Includes name and arguments.
84/// This function is re-used in other places and is likely not helpful by itself.
85pub struct Function<Name, Args> {
86 /// The function name. Must be writable.
87 pub name: Name,
88 /// The function arguments. Must be a sequence.
89 pub args: Args,
90}
91
92/// A function declaration
93pub struct FunctionDef<Attr, Mods, Name, Args, Return, Body> {
94 /// The attributes. Must be a sequence, and each value will be placed inside `#[]`.
95 pub attr: Attr,
96 /// The modifiers. Must be a sequence.
97 pub mods: Mods,
98 /// The function itself
99 pub decl: Function<Name, Args>,
100 /// The return type, i.e. after the `->` arrow
101 pub return_type: Return,
102 /// The function body. At the minimum, this must be `;` (see [`FuncBodyDeclare`])
103 pub body: Body,
104}
105
106/// Declares a function body. This is equivalent to just a semicolon.
107pub struct FuncBodyDeclare;
108
109/// Renders as `Type=Value`. Intended to be used as a type argument, to specify associated types.
110pub struct AssocTypeEquals<Type, Value>(pub Type, pub Value);
111
112/// Adds a "dyn " before a type expression.
113pub struct DynOf<Type>(pub Type);
114
115/// Adds a "&" before a type expression
116pub struct RefOf<Type>(pub Type);
117
118/// Adds a "impl " before a type expression
119pub struct ImplOf<Type>(pub Type);
120
121/// Adds a reference with a lifetime before a type expression, i.e. `&'<lifetime> <type>`
122pub struct LifetimeOf<'l, Type>(pub &'l str, pub Type);
123
124/// The declaration of a trait
125pub struct TraitDef<Attr, Mods, Name, TypeVars, SuperTraits, Body> {
126 /// The trait attributes. Must be a sequence, and each value will be placed inside `#[]`.
127 pub attr: Attr,
128 /// The trait modifiers, e.g. visibility. Must be a sequence.
129 pub mods: Mods,
130 /// The name of the trait
131 pub name: Name,
132 /// The type variables. Must be a sequence
133 pub type_variables: TypeVars,
134 /// The super traits. Must be a sequence
135 pub super_traits: SuperTraits,
136 /// The trait definition's body. Use [crate::common::NoOp] if none exists.
137 pub body: Body,
138}
139
140/// The implementation declaration for a trait, applying to a certain receiver.
141pub struct TraitImpl<TypeVars, Trait, Recv, Body> {
142 /// The type variables to use for the impl block itself. All type variables that appear later
143 /// on the trait or the receiver must be declared here, per Rust language rules.
144 ///
145 /// This field must be a sequence.
146 pub type_variables: TypeVars,
147 /// The trait being implemented
148 pub the_trait: Trait,
149 /// The receiver for which it is implemented
150 pub receiver: Recv,
151 /// The body. Use [crate::common::NoOp] if none exists.
152 pub body: Body,
153}
154
155/// A type argument-parameterized expression. Used in relation to parameterized names and their
156/// arguments. Examples: `function_name<args>`, `TypeName<'lifetime, args>`, `MyType<Assoc=Value>`.
157///
158/// If no type args exist, [`crate::common::NoOpSeq`] should be used.
159pub struct Parameterized<Name, TypeArgs> {
160 name: Name,
161 type_args: TypeArgs,
162}
163
164impl<Name, TypeArgs> Parameterized<Name, TypeArgs> {
165 /// Initializes an instance
166 pub fn new(name: Name, type_args: TypeArgs) -> Self {
167 Self { name, type_args }
168 }
169}
170
171/// A type variable with a sequence of bounds.
172/// Will render as `TypeVar: B1 + B2 + ...`
173pub struct BoundedTypeVar<TypeVar, Bounds>(pub TypeVar, pub Bounds);