#rust #rustlang low-level and #no_std Devs: I have these memory registers which I can write to whenever needed.

Considering this is shared mutable static memory, does this violate aliasing rules in some way? It's impossible to cause memory unsafety because the content written is POD, no constructors, drops, etc.

I'm either copying/reading u8/u16 or C packed structs.

@diegovsky you cannot use references for something like this directly (if I'm understanding you correctly). I would probably wrap whatever container with an UnsafeCell too for an extra precaution.

I've encountered weird issues like this with my os project, so it's always nice to play it safe. Sometimes it's also recommend to use core::ptr::write/read_volatile when it's IO based memory. Idk exactly from your post if it is or not

@corigan01 Thing is, I'm often dealing with slices of mutable memory. For example, this platform has a global 128 entry slice. To move my characters/sprites/objs, I store an entry in an `Entity` struct and write game logic as usual.

However, at every game loop step, all 128 entries are accessed by C code and updated, meaning my `Entity` structs manipulate mutable shared memory. Introducing UnsafeCell will make for a very cumbersome API I think.

@corigan01 It looks like `UnsafeCell` does nothing to address what I'm doing. It's just a way to share references with stuff such that you can provide mutable access in some way, given that you ensure there is only ever one mutable reference to that block of memory. I can never ensure this because this is global mutable memory.

Unless Rust provides better ergonomics for pointer manipulation (like C's arrow), I genuinely prefer to risk undefined behaviour lol

@diegovsky Hey thanks for the response! I wanted to add that you don't have to provide unique access with an UnsafeCell as long as you don't reference the data inside it. It's like a safety thing, you can pass the UnsafeCell<T> by a reference (which is literally just a reference to the internal field that cannot be aliased) and then use something like core::ptr::read_volatile(my_cell.get()) when you want the field. 1/2

@diegovsky if you really like C's -> semantics you could write a little helper macro in rust to emulate the behavior:

macro_rules! c {
($n:ident -> $f:ident) => {
core::ptr::read_volatile(&raw mut (*$n.get()).$f)
};
}

I'm on mobile so the macro isn't tested, but should at least give kinda an idea of what I mean! 2/2