pub fn swap(comptime T: type, noalias a: *T, noalias b: *T) void T: typea: *Tb: *T3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902pub fn swap(comptime T: type, noalias a: *T, noalias b: *T) void {
if (@inComptime()) {
// In comptime, accessing bytes of values with no defined layout is a compile error.
const tmp = a.*;
a.* = b.*;
b.* = tmp;
} else {
// Swapping in streaming nature from start to end instead of swapping
// everything in one step allows easier optimizations and less stack usage.
const a_bytes: []align(@alignOf(T)) u8 = @ptrCast(a);
const b_bytes: []align(@alignOf(T)) u8 = @ptrCast(b);
for (a_bytes, b_bytes) |*ab, *bb| {
const tmp = ab.*;
ab.* = bb.*;
bb.* = tmp;
}
}
}
Exchanges contents of two memory locations.