cast
Cast between different numeric types.
Parameters
- T:type
- type
- value:anytype
- anytype
Source
Implementation
pub fn cast(T: type, value: anytype) T {
return switch (@typeInfo(@TypeOf(value))) {
.int => switch (@typeInfo(T)) {
.int => @intCast(value),
.float => @floatFromInt(value),
else => unreachable,
},
.float => switch (@typeInfo(T)) {
.int => @trunc(value),
.float => @floatCast(value),
else => unreachable,
},
else => unreachable,
};
}