I think I understand some of the confusion occurring in this conversation, so I'm going to address some points that I think you aren't clear on. Tell me if I'm misinterpreting things.
UnsafeCell stores data in place
You do not want to have a UnsafeCell<*mut c_void> because UnsafeCell is accessed through a pointer, so you are conceiving this construction as a pointer to a pointer. UnsafeCell is accessed through a pointer, but the UnsafeCell itself is not a pointer. The UnsafeCell<T> stores T in-place. Wrapping a T in an UnsafeCell<T> does not at all change the representation in memory.
UnsafeCell::<T>::get(&self) -> *mut T simply serves as a way to begin an access to the inner data in a way that is semantically consistent with the existing language mechanism of pointers.
After all, a pointer is just a memory address, and all data is accessed with memory addresses.
Raw pointers only allow exceptions to Rust's lifetime rules
Raw pointers do not serve as a mechanism for interior mutability. They are simply a pointer type, just like references or the Box, which allows the user to unsafely take control of managing the data lifetime, often to build safe abstractions like Rc and Vec.
Raw pointers are still expected to follow Rust's borrowing rules
Both references and raw pointers in Rust follow the expectation that, at a given time, there can only be one mutable reference to a particular piece of data.
Consider this code:
unsafe {
let pointer: *mut i32 = unimplemented!();
*pointer = 7;
let integer: i32 = *pointer;
}
The compiler is not required to actually write and then read to the pointed-at memory address. Since the compiler, with optimizations enabled, can trivially deduce that integer will have the value 7, it can choose to optimize the write and read out of existence. It does not have to consider the possibility that another thread could write to it.
UnsafeCell allows exceptions to Rust's borrowing rules
UnsafeCell is the foundation of abstractions for interior mutability. When you call UnsafeCell.get(), to create a pointer to the wrapped data, the compiler is not allowed to perform caching optimizations on the inner data. It has to assume that some other thread may have mutated that data.
If we adapted the previous example into this:
unsafe {
let cell: *mut UnsafeCell<i32> = unimplemented!();
let pointer: *mut i32 = (&*cell).get();
*pointer = 7;
let pointer: *mut i32 = (&*cell).get();
let integer: i32 = *pointer;
}
The compiler would actually have to write then read the pointer, because each time we call UnsafeCell.get(), the compiler cannot cache the cell's contents.
You want an UnsafeCell<*mut c_void>
From what you're describing, it sounds like you want an UnsafeCell<*mut c_void>. That will be a mutable pointer to data of an unknown type which leaves you in charge of maintaining a valid lifetime and reference rules.
unsafe {
// as an example, it can hold a 1 byte heap allocation
let pointer: UnsafeCell<*mut c_void> = UnsafeCell::new(Box::into_raw(Box::new(0u8)) as *mut u8 as *mut c_void);
// write
*(*pointer.get() as *mut u8) = 42u8;
// read
let b: u8 = *(*pointer.get() as *mut u8);
println!("{}", b);
}
vin your code, or is it opaque? If it's opaque, all of this is not necessary. – Sebastian Redl Oct 2 '18 at 6:14UnsafeCell? or*mut c_void(which I think would be necessory...) – Earth Engine Oct 2 '18 at 6:22UnsafeCellinforms the Rust compiler that a value may change at any time, even though the compiler can see a shared reference to it. This is important for when the compiler wants to use the value, so that it knows it can't just hold it in a register. But if the compiler never touches the field, because it's only touched by FFI functions, then the annotation isn't necessary. – Sebastian Redl Oct 2 '18 at 6:26UnsafeCelldoes not requireSized, so we can put DST in it... – Earth Engine Oct 2 '18 at 6:50