Re: The Case for Rust (in the base system)

From: Alan Somers <asomers_at_freebsd.org>
Date: Tue, 03 Sep 2024 13:36:46 UTC
On Tue, Sep 3, 2024 at 7:04 AM <ske-89@pkmab.se> wrote:
>
> With all this talk about Rust in the OS kernels, can someone
> explain to me why Rust can't simply produce object files that
> can be linked into the kernel like any C object file? (If you
> just avoid calling Rust-specific libraries, just like you don't
> call the C standard library either from within the kernel.)
>
> I haven't worked with Rust, and the introductions to Rust that
> I have read do not discuss this.
>
> /Kristoffer Eriksson

Actually, it can.  You have to take some precautions, of course: many
3rd party libraries are off limits, as is much of the standard
library.  You must disable the usual panic mechanism.  You need to
declare that your module's public API uses the C calling convention.
Probably the biggest limitation is that you can't use the standard
memory allocator.  But you absolutely can do it.  For example:
https://github.com/spookyvision/hello-freebsd-rust .

One of the most interesting features of Rust is #![no_std] .  If you
declare that in your library, then none of the standard library will
be available.  However, there are a dozen or so primitives like memory
allocation and panic handling.  If you implement some of those, then
any parts of the standard library that depend on them will be
available again.  And if you implement all of them, then you can use
virtually the entire standard library.  It's a nice bridge between the
regular world and the embedded world.

-Alan