External Publication
Visit Post

`BitSlice` or a sound way to implement one

Rust Internals [Unofficial] May 2, 2026
Source

I think we can permit forming two &mut u1 references to neighboring bits of the same byte in single-threaded scenarios, but I could be missing something. However, if you have something like ([Mutex<()>; 2], UnsafeCell<[u1; 2]>), is it sound to concurrently mutate each u1 value on different threads? I believe the answer to that is no , which makes things… complicated. However, I don’t think it can break existing generic code.

Here’s my attempt:

struct DoubleMutex<T: ?Sized> {
    one: Mutex<()>,
    two: Mutex<()>,
    // not possible.

    // And `T: Sized` wouldn’t break from `u1`.
    // packed: UnsafeCell<[T; 2]>,

    // uses `size_of_val` to pack two `T`s on the heap.
    // However, that function has byte granularity;
    // if `u1: MetaSized`, we’d presumably end up
    // adding 7 bits of padding between the `u1`s here.
    packed2: NonNull<()>,

    // other fields used to get `packed2` to work
    …,
}

Edit: wait, doesn’t this imply that &mut u1: Send cannot be permitted to hold, so u1: !Send?

That’s a massive pain for a POD type.

Discussion in the ATmosphere

Loading comments...