C# and Rust: combining managed and unmanaged code without sacrificing safety
summary
Seq replaces its Windows ESENT-backed storage engine with a Rust engine and stitches it into an existing C# codebase using C-style FFI and P/Invoke stubs so .NET GC and thread semantics stay correct. Safety gets rebuilt with concrete types: DBResult thread-local last_result for errors, ref/out wrappers for fixed pointers, and handle_shared/handle_exclusive plus deferred cleanup and SafeHandle on the C# side to avoid races. Build trade-offs show up: C# debug builds ~3s, Rust ~2min, and runtime calls are dominated by disk I/O so the FFI overhead is acceptable; example repo (sled-based) proves it works.
AI-written summary · about
chapters (20)
- 0:00Introduction to hybrid apps
- 2:46Use case: Seek database
- 4:48Defining hybrid applications
- 6:14Trade-offs and language choices
- 8:28Rust as advanced C# types
- 10:34Structs and ownership
- 12:21Sharing data: References
- 14:03Lifetime management
- 16:49Safety through the compiler
- 18:50C# ecosystem benefits
- 20:54Stitching languages together
- 25:06FFI and PInvoke
- 28:22FFI error management
- 32:14Pointers and reference safety
- 36:34Out parameters in FFI
- 39:23Safe handle implementation
- 43:23Thread safety: Send and Sync
- 50:30Handling thread-local data
- 52:10Example walkthrough
- 56:22Future of hybrid apps
related talks
01h 04mRust-ifying Your C# Codebase: A Tale of Adventure and Transformation
31mUsing Rust from C... or Any Language
58mRust for The Curious C++ Developers
36mRust as a High-Level Langage
40mWhat's up with Rust?
42mRewrite It In Rust: Can We RIIR Everything?
56mGet the best of both worlds: Integrating Rust with other languages
40mWhy Rust?
