castToPtr
Function parameters
Parameters
- DestType:type
- SourceType:type
- target:anytype
Type definitions in this namespace
Types
Given a type and value, cast the value to the type as c would.
Functions
- cast
- Given a type and value, cast the value to the type as c would.
- sizeof
- Given a value returns its size as C's sizeof operator would.
- promoteIntLiteral
- Promote the type of an integer literal until it fits as C would.
- shuffleVectorIndex
- Convert from clang __builtin_shufflevector index to Zig @shuffle index
- FlexibleArrayType
- Constructs a [*c] pointer with the const and volatile annotations
- signedRemainder
- C `%` operator for signed integers
Source
Implementation
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);
}