Skip to content

Add transmute_ref! and transmute_mut! macros #159

Closed
@joshlf

Description

@joshlf

We currently have a transmute! macro which safely transmutes an AsBytes type into a FromBytes type. We should add macros which do the same for mutable and immutable references.

A candidate implementation of transmute_ref! is in #183. We may also want to ditch it in favor of something based on this approach suggested by @gootorov, but we'd need to first confirm that invalid uses are guaranteed to generate compile-time rather than runtime errors (rust-lang/rust#112090). EDIT: Based on the discussion there, it seems that there is such a guarantee, but we'd need to be careful about how we structure things to make sure that we actually write code that benefits from that guarantee.

Note that rust-lang/rust#112301 will likely make @gootorov's technique a bit annoying for users (although the error messages are way better than the existing technique, and the implementation is way simpler, so IMO it's still worth it on balance).

EDIT: Another issue with this approach is that generic APIs might never fail until they're used by a downstream crate since monomorphization only happens at that point. One potential solution would be to keep the functions #[doc(hidden)] and provide macros to invoke them which also call mem::transmute in order to ensure that they can only be called from type-concrete contexts. This would artificially hamstring the API, but the footgun is big enough that it might be worth it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions