mirror of
https://github.com/ziglang/zig.git
synced 2024-11-25 05:40:16 +00:00
std.mem.asBytes: fix footgun when passing non-single pointer
I was just bitten by this footgun, where I actually wanted `sliceAsBytes` but unintentionally used `asBytes`, which in practice ignored all but the first element. Just add a comptime assertion to trigger a compile error in this case.
This commit is contained in:
parent
d11bbde5f9
commit
24babde746
@ -3965,7 +3965,9 @@ fn CopyPtrAttrs(
|
||||
}
|
||||
|
||||
fn AsBytesReturnType(comptime P: type) type {
|
||||
const size = @sizeOf(std.meta.Child(P));
|
||||
const pointer = @typeInfo(P).pointer;
|
||||
assert(pointer.size == .One);
|
||||
const size = @sizeOf(pointer.child);
|
||||
return CopyPtrAttrs(P, .One, [size]u8);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user