diff options
| author | Charlie Stanton <charlie@shtanton.com> | 2020-06-21 21:48:12 +0100 |
|---|---|---|
| committer | Charlie Stanton <charlie@shtanton.com> | 2020-06-21 21:48:12 +0100 |
| commit | 8c15cfe3dabe736754b138f16de905e6487bd8b5 (patch) | |
| tree | 1afd1e6d298171fa35d0cb4c50330ae983446c1a /lib/std/meta.zig | |
| parent | 6f475130098a5913b485828c72cb47ab8146f9ea (diff) | |
| download | zig-8c15cfe3dabe736754b138f16de905e6487bd8b5.tar.gz zig-8c15cfe3dabe736754b138f16de905e6487bd8b5.zip | |
Compacts switch statements and string literal
Diffstat (limited to 'lib/std/meta.zig')
| -rw-r--r-- | lib/std/meta.zig | 31 |
1 files changed, 9 insertions, 22 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index f27da1a512..6c10941aa7 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -694,16 +694,14 @@ pub fn Vector(comptime len: u32, comptime child: type) type { }); } -/// Given a type and value, cast the value to the type as c would +/// Given a type and value, cast the value to the type as c would. +/// This is for translate-c and is not intended for general use. pub fn cast(comptime DestType: type, target: var) DestType { const TargetType = @TypeOf(target); switch (@typeInfo(DestType)) { - .Pointer => |_| { + .Pointer => { switch (@typeInfo(TargetType)) { - .Int => |_| { - return @intToPtr(DestType, target); - }, - .ComptimeInt => |_| { + .Int, .ComptimeInt => { return @intToPtr(DestType, target); }, .Pointer => |ptr| { @@ -720,10 +718,7 @@ pub fn cast(comptime DestType: type, target: var) DestType { .Optional => |opt| { if (@typeInfo(opt.child) == .Pointer) { switch (@typeInfo(TargetType)) { - .Int => |_| { - return @intToPtr(DestType, target); - }, - .ComptimeInt => |_| { + .Int, .ComptimeInt => { return @intToPtr(DestType, target); }, .Pointer => |ptr| { @@ -738,19 +733,14 @@ pub fn cast(comptime DestType: type, target: var) DestType { } } }, - .Enum => |_| { - if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) { - return @intToEnum(DestType, target); - } - }, - .EnumLiteral => |_| { + .Enum, .EnumLiteral => { if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) { return @intToEnum(DestType, target); } }, - .Int => |_| { + .Int, .ComptimeInt => { switch (@typeInfo(TargetType)) { - .Pointer => |_| { + .Pointer => { return @as(DestType, @ptrToInt(target)); }, .Optional => |opt| { @@ -758,10 +748,7 @@ pub fn cast(comptime DestType: type, target: var) DestType { return @as(DestType, @ptrToInt(target)); } }, - .Enum => |_| { - return @as(DestType, @enumToInt(target)); - }, - .EnumLiteral => |_| { + .Enum, .EnumLiteral => { return @as(DestType, @enumToInt(target)); }, else => {}, |
