LWN: Comments on "Toward safe transmutation in Rust" https://lwn.net/Articles/994334/ This is a special feed containing comments posted to the individual LWN article titled "Toward safe transmutation in Rust". en-us Thu, 23 Oct 2025 20:14:53 +0000 Thu, 23 Oct 2025 20:14:53 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Transmute to user-defined types with all public fields? https://lwn.net/Articles/996246/ https://lwn.net/Articles/996246/ riking <div class="FormattedComment"> Yes, Assume::SAFETY will do nothing to a type whose fields are all public (recursively).<br> </div> Tue, 29 Oct 2024 23:21:51 +0000 Constructor checking https://lwn.net/Articles/995990/ https://lwn.net/Articles/995990/ NYKevin <blockquote> In this case the type would want to be able to mark itself as able to be transmuted to, but only when the type itself is doing the transmuting.</blockquote> <p> You can sort of do that now: <pre> // Simple one-field struct as an example, can be replaced with a more complicated struct. #[repr(C)] // But it does need to be repr(C) or repr(transparent) struct MyType(u64); use std::convert::TryFrom; impl&lt;'a&gt; TryFrom&lt;&amp;'a [u8]&gt; for &amp;'a MyType { type Error = &amp;'static str; // XXX: In a real codebase, use a proper Error type and not a string. fn try_from(x: &amp;'a [u8]) -&gt; Result&lt;Self, &amp;'static str&gt; { if x.len() != std::mem::size_of::&lt;MyType&gt;() { return Err("Wrong size!"); } let ptr: *const MyType = unsafe { std::mem::transmute(x.as_ptr()) }; if ptr.is_aligned() { Ok(unsafe { &amp;*ptr }) } else { Err("Not aligned!") } } } // Implementation for &amp;'a mut [u8] omitted because it's nearly identical. // Now safe code can call try_from() and try_into() for this conversion. </pre> <p> But this is just a thin wrapper around transmute. It's sound, in this case, but if MyType is more complicated, then you have all the problems that the article describes. The compiler is not going to do very much for you here (the only check that the compiler performs in the above example code is to make sure that a pointer-to-u8 is the same size as a pointer-to-MyType, which is rather obvious anyway). <p> (If this looks like a lot of boilerplate code, bear in mind that Rust has macros, so you don't have to write all of this out repeatedly if you're doing it a lot.) Mon, 28 Oct 2024 07:43:18 +0000 garbage in, garbage out https://lwn.net/Articles/995965/ https://lwn.net/Articles/995965/ amarao <div class="FormattedComment"> This completely breaks the foundation of modern cves, garbage in, garbage out.<br> <p> <p> </div> Sun, 27 Oct 2024 21:16:29 +0000 Transmute to user-defined types with all public fields? https://lwn.net/Articles/995917/ https://lwn.net/Articles/995917/ asahilina <div class="FormattedComment"> That is the simplified version and what is already being done with macros in certain crates: you can transmute into types where all bit patterns are valid. But what is being researched here seems to be more general, e.g. you would in theory be able to transmute [NonZeroU8; 4] into NonZeroU32, though not vice versa, despite neither type having all bit patterns valid.<br> </div> Sat, 26 Oct 2024 15:27:37 +0000 Transmute to user-defined types with all public fields? https://lwn.net/Articles/995797/ https://lwn.net/Articles/995797/ jpab <div class="FormattedComment"> I guess I'll extend this and say that I do see value in the more complicated safer-but-still-unsafe API, but having a subset of the capability available through a fully-safe API could be quite neat.<br> </div> Fri, 25 Oct 2024 17:25:14 +0000 Transmute to user-defined types with all public fields? https://lwn.net/Articles/995784/ https://lwn.net/Articles/995784/ jpab <div class="FormattedComment"> I agree, and would suggest that a possible simpler API here would be a safe transmute that applies the validity, alignment, and lifetime requirements, but is only available to transmute to types that *could be constructed directly* in the scope you're in.<br> <p> That is, if I'm in a scope where I can write a literal `Foo { x, y, z }` then I should also be able to transmute from an appropriate source to a Foo, without using `unsafe`, but relying on the compiler to apply all the checks.<br> <p> If I'm in a scope where I can't directly construct a Foo (ie, to get one then I need to call some function like Foo::new()) then I clearly need unsafe. Though I'm not sure when I would want to do that anyway. <br> <p> Visibility of direct construction is already an essential element of enforcing invariants in values. And since safe transmute already requires compiler magic to enforce the various structural safety constraints it could presumably check visibility as well.<br> <p> There is some care needed because you need to have visibility not only to directly construct a value of the target type but also to directly construct values of each field type (and of their fields recursively).<br> </div> Fri, 25 Oct 2024 15:54:09 +0000 Transmute to user-defined types with all public fields? https://lwn.net/Articles/995656/ https://lwn.net/Articles/995656/ roc <div class="FormattedComment"> Fine, a user-defined type with all public fields and no padding should be a valid transmutation target if its fields are<br> </div> Fri, 25 Oct 2024 00:49:31 +0000 Transmute to user-defined types with all public fields? https://lwn.net/Articles/995629/ https://lwn.net/Articles/995629/ heftig <div class="FormattedComment"> If the structure has padding bits, they generally must all be zero.<br> </div> Thu, 24 Oct 2024 20:30:38 +0000 Constructor checking https://lwn.net/Articles/995620/ https://lwn.net/Articles/995620/ ringerc <div class="FormattedComment"> This can also be done by giving the type another constructor or helper method that constructs an instance of the type by transmutation, right?<br> <p> In this case the type would want to be able to mark itself as able to be transmuted to, but only when the type itself is doing the transmuting. Essentially a private access marker trait. I don't know enough Rust to know if this is possible.<br> </div> Thu, 24 Oct 2024 19:01:21 +0000 Transmute to user-defined types with all public fields? https://lwn.net/Articles/995451/ https://lwn.net/Articles/995451/ roc <div class="FormattedComment"> Seems to me that a user-defined type whose fields are all public can't have any invariants so you should be able to transmute to it.<br> </div> Thu, 24 Oct 2024 11:17:34 +0000 Should aim to deprecate std::mem::transmute() https://lwn.net/Articles/995440/ https://lwn.net/Articles/995440/ fishface60 <div class="FormattedComment"> <span class="QuotedText">&gt; It may seem as though requiring the use of unsafe to do transmutation</span><br> <span class="QuotedText">&gt; represents a lack of progress. But this design has the advantage that the</span><br> <span class="QuotedText">&gt; programmer only needs to assert the safety of the specific invariant that</span><br> <span class="QuotedText">&gt; the compiler is unable to prove — the above code still uses the</span><br> <span class="QuotedText">&gt; compile-time checks for bit-validity, alignment, and lifetime problems.</span><br> <p> I hope this gets used more. You've still got to recheck the safety invariants when things change, but this should help when a type you're transmuting changes underneath you.<br> <p> I think there would have to be an eventual goal of deprecating std::mem::transmute() though, since TransmuteFrom::&lt;_, Assume::SAFETY&gt;::transmute(src) is more work to use and people will tend towards using the simplest option, so the language should aim to make the best option the simplest to use.<br> </div> Thu, 24 Oct 2024 09:52:15 +0000 Constructor checking https://lwn.net/Articles/995421/ https://lwn.net/Articles/995421/ epa <div class="FormattedComment"> A user-defined type may have its own constructor that does real work, like opening a network connection. Or maybe the constructor just checks some invariant, like requiring an even number in the above example. Often you could check that invariant after assigning the object’s fields. You can express it as taking an already-populated object and validating it before allowing it to be used. <br> <p> In that case the language could let you define a ‘check’ method as an extra step which runs after the constructor. When casting (‘transmuting’) an area of memory to an instance of the type, the check method is run. Then the requirement like ‘must be even’ can be expressed in code and the programmer doesn’t have to promise the compiler he or she has already checked it. <br> </div> Thu, 24 Oct 2024 07:53:52 +0000 zerocopy https://lwn.net/Articles/995423/ https://lwn.net/Articles/995423/ gspr <div class="FormattedComment"> Indeed, zerocopy figures prominently in the talk and in this LWN article.<br> </div> Thu, 24 Oct 2024 07:43:15 +0000 zerocopy https://lwn.net/Articles/995415/ https://lwn.net/Articles/995415/ philipptoelke <div class="FormattedComment"> I have used <a href="https://docs.rs/zerocopy/latest/zerocopy/index.html">https://docs.rs/zerocopy/latest/zerocopy/index.html</a> successfully to parse packet data from buffers into structs. I think it comes from the Google Fuchsia team.<br> </div> Thu, 24 Oct 2024 06:26:43 +0000 Cast https://lwn.net/Articles/995391/ https://lwn.net/Articles/995391/ intelfx <div class="FormattedComment"> <span class="QuotedText">&gt; As far as I lnow, you can cast your char* buffer into a struct whatever* and then access the struct's fields with no copy.</span><br> <p> Yes, and that's exactly the kind of UB-laden thing that is much harder to do (properly) than to talk about it. It is absolutely invalid to "just" cast an arbitrary buffer into a "struct whatever", unless multiple very specific conditions are met. It has to be done with utter attention to detail (*if* it even can be done under specific circumstances), and the article is precisely about offloading some of that utter care and attention to the compiler.<br> </div> Wed, 23 Oct 2024 21:01:40 +0000 Cast https://lwn.net/Articles/995385/ https://lwn.net/Articles/995385/ daroc <div class="FormattedComment"> Yes, certainly you can. That is an example of using type casting — transmutation, in the Rust lingo.<br> <p> The traditional parsers I was trying to distinguish are libraries that parse structures using actual parsing techniques — parser combinators, grammars, etc.<br> </div> Wed, 23 Oct 2024 20:41:26 +0000 Cast https://lwn.net/Articles/995384/ https://lwn.net/Articles/995384/ ju3Ceemi <div class="FormattedComment"> I do not understand this : "In a traditional parser, the programmer would have to copy all of the data in the UDP header at least once in order to move it from the incoming buffer into a structure"<br> <p> As far as I lnow, you can cast your char* buffer into a struct whatever* and then access the struct's fields with no copy.<br> </div> Wed, 23 Oct 2024 20:37:58 +0000