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:
use async_codegen::common::{CombinedSeq, SingularSeq, Str};
use async_codegen::{Output, Writable};
use async_codegen::rust::{CfgAttr, Deprecated, Function, FunctionDef, FunctionParam, ModPub, MustUse, NoMangle, Parameterized};
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 {
    attr: SingularSeq(Deprecated::with_message(Str("because we all love deprecation"))),
    mods: SingularSeq(ModPub),
    decl: Function {
      name: Str("my_func"),
      args: CombinedSeq(
        SingularSeq(FunctionParam(Str("var1"), Str("Type"))),
        SingularSeq(FunctionParam(Str("var2"), Parameterized::new(Str("Option"), SingularSeq(Str("bool")))))
      )
    },
    return_type: Parameterized::new(Str("Box"), SingularSeq(Str("str"))),
    body: Str("\ntodo!()\n")
  };
  function_def.write_to(output).await
  // Will render as:
  /*
  #[deprecated = "because we all love deprecation"]
  pub fn my_func(var1: Type, var2: Option<bool>) -> Box<str> {
    todo!()
  }
   */
}Structs§
- AllowLints 
- The attribute content for allow(...). The tuple value must be a sequence.
- AssociatedItem 
- An item attached to an associated container, via “::”.
The output will look like Cont::Item.
- AssociatedType Def 
- Declares an associated type, rendering as type VarName = Value;. Adds new lines before and after.
- AssociatedType Equals 
- Renders as Type=Value. Intended to be used as a type argument, to specify associated types.
- AttributesAccept 
- A sequence acceptor that writes attributes
- BoundedType Var 
- A type variable with a sequence of bounds.
Will render as TypeVar: B1 + B2 + ...
- 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.
- DynOf
- Adds a “dyn “ before a type expression.
- ExternBlock 
- Declaration of an extern block, i.e. for FFI.
- Function
- The base struct for a function. Includes name and arguments. This function is re-used in other places and is likely not helpful by itself.
- FunctionBody Declare 
- Declares a function body. This is equivalent to just a semicolon.
- FunctionCall 
- Performs a call to a function inside code.
- FunctionDef 
- A function declaration
- FunctionParam 
- Renders an individual function parameter, Name: Type
- ImplOf
- Adds a “impl “ before a type expression
- 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
- LifetimedRefOf 
- Adds a reference with a lifetime before a type expression, i.e. &'<lifetime> <type>
- ModExtern
- The extern modifier, with the ABI selected as the tuple value
- ModPub
- The public modifier
- MustUse
- The must_use attribute
- NoMangle
- The no mangle attribute.
- OkResultOf 
- 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>.
- QuestionMark After 
- A question mark following another expression.
- RefOf
- Adds a “&” before a type expression
- TraitDef 
- The declaration of a trait
- TraitImpl 
- The implementation declaration for a trait, applying to a certain receiver.
- TypeAsTrait 
- Uses the asexpression to perform a qualified trait cast (ready for a method call). I.e., this will render as<Type as Trait>.
- UnsafeBlock 
- Places the expression inside an unsafe block. Adds new lines inside the brackets, wrapping the inner expression.
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.