pub fn alignInBytes(bytes: []u8, comptime new_alignment: usize) ?[]align(new_alignment) u8 bytes: []u8new_alignment: usize5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025pub fn alignInBytes(bytes: []u8, comptime new_alignment: usize) ?[]align(new_alignment) u8 {
const begin_address = @intFromPtr(bytes.ptr);
const end_address = begin_address + bytes.len;
const begin_address_aligned = mem.alignForward(usize, begin_address, new_alignment);
const new_length = std.math.sub(usize, end_address, begin_address_aligned) catch |e| switch (e) {
error.Overflow => return null,
};
const alignment_offset = begin_address_aligned - begin_address;
return @alignCast(bytes[alignment_offset .. alignment_offset + new_length]);
}
Returns the largest slice in the given bytes that conforms to the new alignment, or
nullif the given bytes contain no conforming address.