Posts

Showing posts from May, 2021

Rust for the Seasoned Scala Developer

Updated: removed lack of extension methods as they can be implemented using traits, added language editions Intro Scala has been my language of choice (both for work and personal projects) for at least 10 years now. No other language has even made me consider switching: Kotlin is basically a lesser clone of Scala with a weaker type system Java and C# have both improved greatly, but are still lacking important features (like type classes/implicit parameters) Go is a no-go because of the badly designed type system Swift has a pretty nice type system, but it's not better than Scala in any way really, has worse memory management (basically implicit reference counting) and the portability is bad TypeScript also has a pretty nice type system, but again doesn't provide any benefits for general application development, and if I want to target the browser I'll rather use Scala.js That pretty much just leaves Rust , which as an old C++ developer, always has intrigued me as it provide

Data Modelling in Rust Continued

Intro So, just after my previous blog posting about data modelling in Rust I started looking at GhostCell  (and corresponding crate ) and how to use that for my game example. It's an extremely simple data type that works similarly to RefCell , but all borrow checks are performed at compile time and thus avoids all runtime overhead. It achieves this by separating the cells from the actual borrowing permission. When borrowing a reference to the interior of a cell you need a reference to a token connected to the cell. The cell and token are connected using a common lifetime parameter. Truly ingenious stuff! Solution 4: Reference Counted GhostCell's For my example it's probably most convenient to use GhostCell in combination with Rc . The reference types now looks like this: struct Ref<'brand, T>(Rc<GhostCell<'brand, T>>); type WRef<'brand, T> = Weak<GhostCell<'brand, T>>; type PlayerRef<'brand> = Ref<'brand

Data Modelling in Rust

Intro Recently I've been experimenting on how to efficiently and ergonomically model data types containing mutual and cyclic references in Rust. In a language with built in garbage collector like Java, C# etc., this is relatively straightforward as the GC handles cycles and object deallocation automatically for you. In Rust there's no included general purpose GC, instead there are many helper types for facilitating memory handling included in the standard library. There are many ways of solving the problem, each with different trade offs and characteristics. In this post I'll present some of the alternatives available using the Rust standard library. First I should point out that I'm not an expert Rust developer, so this experimentation has been part of my Rust learning process, but I hope it can provide some ideas and insights for new Rust developers coming from a GC language. Please point any stupid errors I might have made, or just things I've totally missed. Exa