feature. See also
. The project being documented here (as the example) is the Zig library itself.
helpers.castToPtr
fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype) DestType
File
Code
fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype) DestType {
switch (@typeInfo(SourceType)) {
.int => {
return @as(DestType, @ptrFromInt(castInt(usize, target)));
},
.comptime_int => {
if (target < 0)
return @as(DestType, @ptrFromInt(@as(usize, @bitCast(@as(isize, @intCast(target))))))
else
return @as(DestType, @ptrFromInt(@as(usize, @intCast(target))));
},
.pointer => {
return castPtr(DestType, target);
},
.@"fn" => {
return castPtr(DestType, &target);
},
.optional => |target_opt| {
if (@typeInfo(target_opt.child) == .pointer) {
return castPtr(DestType, target);
}
},
else => {},
}
return @as(DestType, target);
}