| ... | @@ -694,16 +694,14 @@ pub fn Vector(comptime len: u32, comptime child: type) type { | ... | @@ -694,16 +694,14 @@ pub fn Vector(comptime len: u32, comptime child: type) type { |
| 694 | }); | 694 | }); |
| 695 | } | 695 | } |
| 696 | | 696 | |
| 697 | /// Given a type and value, cast the value to the type as c would | 697 | /// Given a type and value, cast the value to the type as c would. |
| | 698 | /// This is for translate-c and is not intended for general use. |
| 698 | pub fn cast(comptime DestType: type, target: var) DestType { | 699 | pub fn cast(comptime DestType: type, target: var) DestType { |
| 699 | const TargetType = @TypeOf(target); | 700 | const TargetType = @TypeOf(target); |
| 700 | switch (@typeInfo(DestType)) { | 701 | switch (@typeInfo(DestType)) { |
| 701 | .Pointer => |_| { | 702 | .Pointer => { |
| 702 | switch (@typeInfo(TargetType)) { | 703 | switch (@typeInfo(TargetType)) { |
| 703 | .Int => |_| { | 704 | .Int, .ComptimeInt => { |
| 704 | return @intToPtr(DestType, target); | | |
| 705 | }, | | |
| 706 | .ComptimeInt => |_| { | | |
| 707 | return @intToPtr(DestType, target); | 705 | return @intToPtr(DestType, target); |
| 708 | }, | 706 | }, |
| 709 | .Pointer => |ptr| { | 707 | .Pointer => |ptr| { |
| ... | @@ -720,10 +718,7 @@ pub fn cast(comptime DestType: type, target: var) DestType { | ... | @@ -720,10 +718,7 @@ pub fn cast(comptime DestType: type, target: var) DestType { |
| 720 | .Optional => |opt| { | 718 | .Optional => |opt| { |
| 721 | if (@typeInfo(opt.child) == .Pointer) { | 719 | if (@typeInfo(opt.child) == .Pointer) { |
| 722 | switch (@typeInfo(TargetType)) { | 720 | switch (@typeInfo(TargetType)) { |
| 723 | .Int => |_| { | 721 | .Int, .ComptimeInt => { |
| 724 | return @intToPtr(DestType, target); | | |
| 725 | }, | | |
| 726 | .ComptimeInt => |_| { | | |
| 727 | return @intToPtr(DestType, target); | 722 | return @intToPtr(DestType, target); |
| 728 | }, | 723 | }, |
| 729 | .Pointer => |ptr| { | 724 | .Pointer => |ptr| { |
| ... | @@ -738,19 +733,14 @@ pub fn cast(comptime DestType: type, target: var) DestType { | ... | @@ -738,19 +733,14 @@ pub fn cast(comptime DestType: type, target: var) DestType { |
| 738 | } | 733 | } |
| 739 | } | 734 | } |
| 740 | }, | 735 | }, |
| 741 | .Enum => |_| { | 736 | .Enum, .EnumLiteral => { |
| 742 | if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) { | | |
| 743 | return @intToEnum(DestType, target); | | |
| 744 | } | | |
| 745 | }, | | |
| 746 | .EnumLiteral => |_| { | | |
| 747 | if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) { | 737 | if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) { |
| 748 | return @intToEnum(DestType, target); | 738 | return @intToEnum(DestType, target); |
| 749 | } | 739 | } |
| 750 | }, | 740 | }, |
| 751 | .Int => |_| { | 741 | .Int, .ComptimeInt => { |
| 752 | switch (@typeInfo(TargetType)) { | 742 | switch (@typeInfo(TargetType)) { |
| 753 | .Pointer => |_| { | 743 | .Pointer => { |
| 754 | return @as(DestType, @ptrToInt(target)); | 744 | return @as(DestType, @ptrToInt(target)); |
| 755 | }, | 745 | }, |
| 756 | .Optional => |opt| { | 746 | .Optional => |opt| { |
| ... | @@ -758,10 +748,7 @@ pub fn cast(comptime DestType: type, target: var) DestType { | ... | @@ -758,10 +748,7 @@ pub fn cast(comptime DestType: type, target: var) DestType { |
| 758 | return @as(DestType, @ptrToInt(target)); | 748 | return @as(DestType, @ptrToInt(target)); |
| 759 | } | 749 | } |
| 760 | }, | 750 | }, |
| 761 | .Enum => |_| { | 751 | .Enum, .EnumLiteral => { |
| 762 | return @as(DestType, @enumToInt(target)); | | |
| 763 | }, | | |
| 764 | .EnumLiteral => |_| { | | |
| 765 | return @as(DestType, @enumToInt(target)); | 752 | return @as(DestType, @enumToInt(target)); |
| 766 | }, | 753 | }, |
| 767 | else => {}, | 754 | else => {}, |