For example why can't I declare either a signed or unsigned integer that uses 4 bits instead of 8 bits?
You can't declare a single item that uses fractional amount of bytes. Of course, you can define the structure consisting of one u8
which will behave as an array of two integers, but you'll have to implement all the operations manually for now.
Why can't I do this exactly?
#[repr(packed)]
struct int<const Width: usize> {
bits: [Bit; Width],
}
#[repr(packed)]
struct uint<const Width: usize> {
bits: [Bit; Width],
}
What does this mean exactly?
Byte is a minimal addressable memory element. If something takes less then byte, CPU will treat it as a byte anyway.
Zig uses LLVM and supports int
s and uint
s that are not a multiple of 8 bits, so byte addressing is not an intrinsic limitation at the LLVM level (except probably for atomic ops). The more likely problem in Rust is one of ownership/borrowing and potential UB with respect to the unused bits in the underlying hardware (multi)byte object. It may be possible to add the necessary semantics to MIR and MIRI so that stacked borrows work on such objects and that different fields of the same object can be borrowed or moved independently, but it's unlikely to be trivial except on now-obsolete bit-addressable hardware.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.