Function cast [src]

Given a type and value, cast the value to the type as c would.

Prototype

pub fn cast(comptime DestType: type, target: anytype) DestType

Parameters

DestType: type

Source

pub fn cast(comptime DestType: type, target: anytype) DestType { // this function should behave like transCCast in translate-c, except it's for macros const SourceType = @TypeOf(target); switch (@typeInfo(DestType)) { .@"fn" => return castToPtr(*const DestType, SourceType, target), .pointer => return castToPtr(DestType, SourceType, target), .optional => |dest_opt| { if (@typeInfo(dest_opt.child) == .pointer) { return castToPtr(DestType, SourceType, target); } else if (@typeInfo(dest_opt.child) == .@"fn") { return castToPtr(?*const dest_opt.child, SourceType, target); } }, .int => { switch (@typeInfo(SourceType)) { .pointer => { return castInt(DestType, @intFromPtr(target)); }, .optional => |opt| { if (@typeInfo(opt.child) == .pointer) { return castInt(DestType, @intFromPtr(target)); } }, .int => { return castInt(DestType, target); }, .@"fn" => { return castInt(DestType, @intFromPtr(&target)); }, .bool => { return @intFromBool(target); }, else => {}, } }, .float => { switch (@typeInfo(SourceType)) { .int => return @as(DestType, @floatFromInt(target)), .float => return @as(DestType, @floatCast(target)), .bool => return @as(DestType, @floatFromInt(@intFromBool(target))), else => {}, } }, .@"union" => |info| { inline for (info.fields) |field| { if (field.type == SourceType) return @unionInit(DestType, field.name, target); } @compileError("cast to union type '" ++ @typeName(DestType) ++ "' from type '" ++ @typeName(SourceType) ++ "' which is not present in union"); }, .bool => return cast(usize, target) != 0, else => {}, } return @as(DestType, target); }