pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T, @TypeOf(bytes)) T: typebytes4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T, @TypeOf(bytes)) {
// let's not give an undefined pointer to @ptrCast
// it may be equal to zero and fail a null check
if (bytes.len == 0 or @sizeOf(T) == 0) {
return &[0]T{};
}
const cast_target = CopyPtrAttrs(@TypeOf(bytes), .many, T);
return @as(cast_target, @ptrCast(bytes))[0..@divExact(bytes.len, @sizeOf(T))];
}
Given a slice of bytes, returns a slice of the specified type backed by those bytes, preserving pointer attributes. If
Tis zero-bytes sized, the returned slice has a len of zero.