Skip to content

Instantly share code, notes, and snippets.

@GeorgeHahn
Last active September 12, 2018 19:34
Show Gist options
  • Save GeorgeHahn/914f2ebafff1ccd485b4 to your computer and use it in GitHub Desktop.
Save GeorgeHahn/914f2ebafff1ccd485b4 to your computer and use it in GitHub Desktop.
Considering Rust + CLR interop

#Brainstorming Rust + C# interop

Use case A

  • Rust UI via WPF or Persperex
  • Make it feel like XAML is Rust-native
  • Put function name in WPF event handler, we stub out from that to Rust code
  • Rust event handlers written with a macro that we use to write interop stubs on each side
  • Can I do GUI nesting without XAML? Eg like the Zinc.rs config block, but for UI elements
  • Pros:
  • Super cool
  • Gives Rust a native Windows UI
  • Cons:
  • Tough interop; event handlers will have limited ways to call back into c# without a macro or plugin to enable it
  • EG:
  • + Rust clrfn! { print!("Worked!"); }

Use case B

  • Compiler plugin that allows direct use of CLR objects from within Rust code
  • Can I intercept errors from Rust and handle them? Not possible otherwise.
  • Pros:
  • Super powerful
  • Can run any C# code
  • Cons:
  • Interop still hard
  • Inventing new syntax
  • EG:
  • Something like
  • fn do_stuff() { Console.WriteLine("Hi from C#"); }
  • Or maybe (note alternate line ending
  • fn do_stuff() { Console.WriteLine("Hi from C#"): }
  • Or could be done with a macro to create the function, a function naming convention, a magic comment, or something else

Use case C:

  • Rust WinForms binding
  • Wrap WinForms with Rust code
  • Pros
  • Known scope
  • Not terribly hard
  • No preconceptions to overcome (eg WPF binding to C# objects would make interop hard)
  • All interop + CLR hosting can be totally hidden behind a layer of pure Rust
  • Cons
  • It's WinForms
  • Not sexy
  • EG:
  • message_box.show("Words");
  • (new window()).show();
  • (new window()).add_element(new label("Words"));
@n074v41l4bl34u
Copy link

+1 for use case B

@AlexanderBaggett
Copy link

+1 for use case B and C

@dutchmartin
Copy link

Cool ideas, using rust instead of c++ would be cool in combination with the mono framework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment