|
|
Log in / Subscribe / Register

Code Review

Code Review

Posted Nov 14, 2025 19:41 UTC (Fri) by tialaramex (subscriber, #21167)
In reply to: Code Review by Vorpal
Parent article: Rust in Android: move fast and fix things (Google Security Blog)

For Sum Types in particular I think the type theoretical name (Sum types) is better than referring to ideas like "tagged unions" because Rust is quite aggressively (and likely to be even more aggressive in future) optimising these types.

A tagged union type can feel like it has to be slower, the naive C implementation of Option<OwnedFd> would be bigger and slower than C's int file descriptors because it would need to store that separate tag value. In fact rust's Option<OwnedFd> is mechanically indistinguishable from just a C file descriptor at runtime because a valid file descriptor can't be -1, so the -1 bit pattern is used for None while all the other file descriptors Some(fd) are stored as the bit pattern for their integer value. Correct C and Rust ends up with basically the same machine code, but the type system means it is more likely that the compiler will catch your inevitable mistakes in Rust.

Linear types would be very cool, but alas Rust doesn't have them. The Affine types, which Rust has, mean that you can't accidentally refer to a Goose after it no longer exists, and thus you also can't "double free" it, because it's gone. But the Linear types additionally ensure you must free the Goose, whereas Affine types aren't bothered if you forget. The fallout from that was the Leakpocalypse of fame slightly before Rust 1.0 and corresponding choice to mark Box::leak safe because these are affine types -- people *can* safely leak the Box even if you tell them not to so marking Box::leak unsafe would be a futile gesture.


The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:

Note: you can avoid this step in the future by logging into your LWN account.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds