logoalt Hacker News

AlotOfReading • today at 6:01 PM • 1 reply • view on HN

    I've occasionally contemplated whether Rust would benefit from another flavor of reference: no-access. 
Most of the uses for this I can think of are best solved by opaque pointers for FFI. Having a rust-native reference just seems incongruous. Like, does it have size and alignment info? How would it interact with NLL? The only way I can imagine it working is if it extended referent lifetime throughout the lexical lifetime of the reference, but that defeats the purpose of NLL.

Rust solves a similar problem in closures with unique immutable references, but they're not quite the same.


Replies

amluto • today at 6:51 PM

In my mind, a no-access reference would have exactly the same lifetime rules (except for the exclusivity part) as any other reference. And they'd have the same size and lifetime rules.

Unsafe Rust could promote a no-access reference to a shared or a mutable reference, and the unsafe code would be responsible for not violating exclusivity rules but would have a guarantee that the referent actually exists. Using unsafe code to create a no-access reference to a nonexistent object or to a misaligned object would be UB.

Safe code could convert the other way:

    let a: &mut u32 = ...;
    let b: &noaccess u32 = a;
    let c: &u32 = ...;
    let d: &noaccess u32 = c;
This is not an entirely serious proposal.
➕ show 2 replies