OK, let me make sure I understand:
Given this declaration of a C function:
uint64_t volatile *f();
Is this the proper Rust FFI declaration for this function?:
extern {
fn f() -> *mut u64;
}
Can we assume that as long as Rust code never attempts to load or store from the result of f
, the compiler will never attempt any loads or stores?
Let’s say I want to allocate a buffer that Rust will never load or store from. Is there a way to do that? For example, consider:
struct Foo {
buffer: std::cell::UnsafeCell<[u8; 64]>
}
impl Foo {
fn new() -> Foo {
Foo {
buffer: std::cell::UnsafeCell::new([0u8; 64])
}
}
}
fn main() {
let _ = Foo::new();
}
It seems like the compiler is allowed to insert spurious loads and stores into the buffer. Is there any way to avoid that? In particular, I would like to pass this buffer to some non-Rust code so that it can use it for whatever it wants, but I want Rust to be responsible for the allocation. Is this possible? In C, we would use volatile uint8_t buffer[64]
.