Skip to main content

Crate inline_csharp_macros

Crate inline_csharp_macros 

Source
Expand description

Proc-macro implementation for inline_csharp.

Provides three proc macros for embedding C# code in Rust:

MacroWhen it runs
csharp!program runtime
csharp_fn!program runtime
ct_csharp!compile time

All macros require the user to write a static <T> Run(...) method where T is one of: sbyte, byte, short, ushort, int, uint, long, ulong, float, double, bool, char, string, T[], List<T>, or T? — including arbitrarily nested types.

§Wire format (C# → Rust, stdout)

The macro generates a Main() that binary-serialises Run()’s return value to stdout via BinaryWriter (raw UTF-8 for top-level string).

Encoding per type (all little-endian):

  • string at top level: raw UTF-8 (no length prefix)
  • string inside a container: 4-byte LE u32 length + UTF-8 bytes
  • scalar: fixed-width little-endian bytes via BinaryWriter
  • T[] / List<T>: 4-byte LE u32 count + N × encode(T)
  • T? (Nullable): 1-byte tag (0=null, 1=present) + encode(T) if present

§Wire format (Rust → C#, stdin)

Parameters declared in Run(...) are serialised by Rust and piped to the child process’s stdin. C# reads them with BinaryReader.

§Options

All three macros accept zero or more key = "value" pairs before the C# body, comma-separated. Recognised keys:

  • build = "<args>" — extra arguments passed verbatim to dotnet build
  • run = "<args>" — extra arguments passed verbatim to dotnet run
  • reference = "<path>" — add a reference assembly (repeatable)

Macros§

csharp
Compile and run zero-argument C# code at program runtime.
csharp_fn
Return a typed Rust function that compiles and runs C# at program runtime.
ct_csharp
Run C# at compile time and splice its return value as a Rust literal.