Zig Version
0.16.0
Steps to Reproduce and Observed Behavior
conststd=@import("std");pubfnmain()void{constv:u24=0xAABBCC;constbytes=std.mem.asBytes(&v);std.debug.print("0x{x} -> {d} ({x})\n",.{v,bytes.len,bytes});}
$ zig run main.zig
0xaabbcc -> 4 (ccbbaa00)
Expected Behavior
Maybe this is just me being new to the language, but I'd expect std.mem.asBytes to return a 3 byte array not a 4 byte array. This seems to be a consequence of
fnAsBytesReturnType(comptimeP:type)type{constpointer=@typeInfo(P).pointer;assert(pointer.size==.one);constsize=@sizeOf(pointer.child);returnCopyPtrAttrs(P,.one,[size]u8);}
Where @sizeOf returns backing storage size, which is 4. If I change that code in my local zig's std.mem to:
fnAsBytesReturnType(comptimeP:type)type{constpointer=@typeInfo(P).pointer;assert(pointer.size==.one);//const size = @sizeOf(pointer.child);constsize=@bitSizeOf(pointer.child)/8;returnCopyPtrAttrs(P,.one,[size]u8);}
I get
$ zig run main.zig
0xaabbcc -> 3 (ccbbaa)
but that obviously won't work for something like a u25 and I also assume it could break a lot of other things.
fnAsBytesReturnType(comptimeP:type)type{constpointer=@typeInfo(P).pointer;assert(pointer.size==.one);constbit_size=@bitSizeOf(pointer.child);constsize=if(bit_size%8==0)(bit_size/8)else@sizeOf(pointer.child);returnCopyPtrAttrs(P,.one,[size]u8);}
This seems to behave well in the u24 case.
### Zig Version
0.16.0
### Steps to Reproduce and Observed Behavior
```zig
const std = @import("std");
pub fn main() void {
const v: u24 = 0xAABBCC;
const bytes = std.mem.asBytes(&v);
std.debug.print("0x{x} -> {d} ({x})\n", .{ v, bytes.len, bytes });
}
```
```
$ zig run main.zig
0xaabbcc -> 4 (ccbbaa00)
```
### Expected Behavior
Maybe this is just me being new to the language, but I'd expect `std.mem.asBytes` to return a 3 byte array not a 4 byte array. This seems to be a consequence of
```zig
fn AsBytesReturnType(comptime P: type) type {
const pointer = @typeInfo(P).pointer;
assert(pointer.size == .one);
const size = @sizeOf(pointer.child);
return CopyPtrAttrs(P, .one, [size]u8);
}
```
Where `@sizeOf` returns backing storage size, which is 4. If I change that code in my local zig's `std.mem` to:
```zig
fn AsBytesReturnType(comptime P: type) type {
const pointer = @typeInfo(P).pointer;
assert(pointer.size == .one);
//const size = @sizeOf(pointer.child);
const size = @bitSizeOf(pointer.child)/8;
return CopyPtrAttrs(P, .one, [size]u8);
}
```
I get
```
$ zig run main.zig
0xaabbcc -> 3 (ccbbaa)
```
but that obviously won't work for something like a `u25` and I also assume it could break a lot of other things.
```zig
fn AsBytesReturnType(comptime P: type) type {
const pointer = @typeInfo(P).pointer;
assert(pointer.size == .one);
const bit_size = @bitSizeOf(pointer.child);
const size = if (bit_size % 8 == 0) (bit_size/8) else @sizeOf(pointer.child);
return CopyPtrAttrs(P, .one, [size]u8);
}
```
This seems to behave well in the `u24` case.