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:
mlugg 2024-10-31 02:32:34 +00:00
parent d11bbde5f9
commit 24babde746
No known key found for this signature in database
GPG Key ID: 3F5B7DCCBF4AF02E

View File

@ -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);
}