Expand description
Rust code syntax is available through this module.
Rust syntax elements.
Note that no checking exists to make sure the elements are used correctly, i.e. the correct combination of structs. Instead, the library user is expected to have basic knowledge of how Rust syntax is composed, and to combine the structs in this module likewise.
Example:
async fn write_function<O>(output: &mut O) -> Result<(), O::Error> where O: Output {
// For more advanced usage, you can replace Str("") by other Writable implementations
let function_def = FunctionDef {
mods: SingularSeq(ModPub),
name: Str("my_func"),
args: CombinedSeq(
SingularSeq(FunctionParam(Str("var1"), Str("Type"))),
SingularSeq(FunctionParam(Str("var2"), Parameterized(Str("Option"), SingularSeq(Str("bool")))))
),
return_type: Parameterized(Str("Box"), SingularSeq(Str("str"))),
where_conds: NoOpSeq,
body: FunctionBodyImplement(Str("todo!()"))
};
function_def.write_to(output).await
// Will render as:
/*
pub fn my_func(var1: Type, var2: Option<bool>) -> Box<str> {
todo!()
}
*/
}Structs§
- Allow
Lints - The attribute content for
allow(...). The tuple value must be a sequence. - Anon
Tuple - An anonymous tuple type. This struct’s tuple value must be a sequence.
- Array
From Elements - An array literal with predefined elements written out.
Renders as
[E1, E2, E3, ...]where EX is in the element sequence. - Assign
Stmt - An assignation. This statement includes the semicolon and a new line.
- Associated
Item - An item attached to an associated container, via “::”.
The output will look like
Cont::Item. - Associated
Type Def - Declares an associated type, rendering as
type VarName = Value;. Adds new lines before and after. - Associated
Type Equals - Renders as
Type=Value. Intended to be used as a type argument, to specify associated types. - Attributes
Accept - A sequence acceptor that writes attributes. Every attribute will be surrounded with “#[]”
- Block
- An unlabeled block. This can be used in many contexts, including merely organizing the code.
- Bounded
Type Var - A type variable with a sequence of bounds.
Will render as
TypeVar: B1 + B2 + ... - Cfg
- A cfg attribute. Renders as
cfg(Cond). - CfgAttr
- An attribute enabled conditionally, i.e.
#[cfg_attr(Cond, Attr)] - Closure
- Writes a closure. Adds new lines inside the brackets, wrapping the inner expression.
- Declare
Field - Declares a single field within a struct. Renders as
Name: Value. - DynOf
- Adds a “dyn “ before a type expression.
- Else
- Represents “else” syntactically. Renders as
Before else After. - EnumDef
- Defines an enum.
- Extern
Block - Declaration of an extern block, i.e. for FFI. In Rust 2024 and later, the unsafe keyword must be added for extern blocks. Thus, this struct requires that the context satisfies [ContextProvides] for Edition.
- Function
Body Declare - Declares a function body. This is equivalent to just a semicolon.
- Function
Body Implement - Implements a function body. Places the contents inside brackets
- Function
Call - Performs a call to a function inside code.
- Function
Def - A function declaration
- Function
Param - Renders an individual function parameter,
Name: Type - Function
Ptr - A function pointer. Can be used for
fn,Fn,FnMut, andFnOnce. - IfBlock
- An if block. The condition and body must both be writable.
- ImplOf
- Adds an “impl “ before a type expression
- LetExpr
- A let expression. This can be used, for example, as the condition of IfBlock in order to create an “if-let” block.
- LetStmt
- A let statement. This statement includes the semicolon and a new line.
- Lifetime
- A standalone lifetime, intended to be used as a type argument or variable
- Lifetimed
RefOf - Adds a reference with a lifetime before a type expression, i.e.
&'<lifetime> <type> - Link
- The link attribute.
- ModBlock
- Declaration of a module block. Renders as
mod Mod {Body}. - ModPub
- The public modifier
- ModUnsafe
- The unsafe modifier
- ModUnsafe
Extern - The extern modifier, with the ABI selected as the tuple value.
- MustUse
- The must_use attribute
- Named
Tuple - A named tuple type.
- NoMangle
- The no mangle attribute.
- OkResult
Of - Wraps an expression in
Ok(EXPR). - Parameterized
- A type argument-parameterized expression. Used in relation to parameterized names and their
arguments. Examples:
function_name<args>,TypeName<'lifetime, args>,MyType<Assoc=Value>. - Question
Mark After - A question mark following another expression.
- RefOf
- Adds a “&” before a type expression
- Return
Stmt - A return statement. Renders as
return Expr;with a new line at the end. - Stmt
- A standalone statement. Renders the expression and adds a semicolon and a new line.
- Struct
Call - The construction or deconstruction of a struct.
- Struct
Def - The declaration of a struct.
- Struct
Fields - Named struct fields. This will place every field on a new line with a comma afterward. It is recommended that the sequence should pass DeclareField.
- Trait
Def - The declaration of a trait
- Trait
Impl - The implementation declaration for a trait, applying to a certain receiver.
- Turbofish
- Provides access to the “turbofish” syntax, i.e.
Name::<Args>. The first tuple value must be writable, and the second must be a sequence. - Type
AsTrait - Uses the
asexpression to perform a qualified trait cast (ready for a method call). I.e., this will render as<Type as Trait>. - Unsafe
Block - Places the expression inside an unsafe block. Adds new lines inside the brackets, wrapping the inner expression.
- UseType
- Imports a single type so that it can be used later.
Renders as
use Type;. Adds a new line after the semicolon. - With
Attributes - Adds attributes to ANY item.
Enums§
- Deprecated
- The deprecated attribute. The three variants of this enum correspond to the deprecated attribute’s multiple ways of being specified. See: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute
- Edition
- All possible Rust editions. This is the only type in this module meant to be used as context, and not as a writable itself.
- Function
PtrKind - The kind of function type
- Struct
Kind - Completes the struct definition as either a named tuple or a struct with named fields.
- Target
- A cfg condition for targeting an OS, OS family, or architecture. For example:
Traits§
- CanHave
Attributes - A writable that can have attributes attached to it
Type Aliases§
- Unit
Type - The unit type, i.e.
()