#Pointer and reference interaction

9 messages · Page 1 of 1 (latest)

timber lance
#

I am wondering what is allowed in terms of aliasing and mutation when having pointers and references to the same value in Rust. I already looked inside the book1, the nomicon2 and the documentation of std::ptr3.

My current understanding is as follows: Pointers do not play into the aliasing guarantees made for references so that there can be an unbounded number of pointers to any reference. Pointers can always read, they can only write if no reference to their pointee exists. References work as normal, it is the users responsibility to anticipate that while using pointers.

After reading the following two sentences from the safety documentation from std::ptr3 I am confused though:

The result of casting a reference to a pointer is valid for as long as the underlying object is live and no reference (just raw pointers) is used to access the same memory. That is, reference and pointer accesses cannot be interleaved.
Since reads are also accesses as far as I know I interpret this text as saying it is not even allowed to read alternating from a pointer and a reference even if the value stays constant. Is that correct?

reef skiff
#

that sounds odd because I would think you ought to be able to mix &T reads and *const T reads; it might be that they forgot to specify "writes" somewhere in there

#

but I am not a pointer soundness expert

remote flume
#

The precise rules for validity are not determined yet. The guarantees that are provided at this point are very minimal:

The way I understand the leading disclaimer, these are positive guarantees. They are not invalidating anything outside of what's mentioned.
I guess rustc has the final say in this.

timber lance
remote flume
#

I have barely any experience with unsafe tbh, but I assume you'd take a &mut in your interface to obey that specific rule

#

Or they all just get their blessing from the hive mind that is #dark-arts

vagrant forum
#

Some parts of it are in flux, but some are rather stable