| ... | @@ -88,6 +88,9 @@ fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype | ... | @@ -88,6 +88,9 @@ fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype |
| 88 | .pointer => { | 88 | .pointer => { |
| 89 | return castPtr(DestType, target); | 89 | return castPtr(DestType, target); |
| 90 | }, | 90 | }, |
| | 91 | .@"fn" => { |
| | 92 | return castPtr(DestType, &target); |
| | 93 | }, |
| 91 | .optional => |target_opt| { | 94 | .optional => |target_opt| { |
| 92 | if (@typeInfo(target_opt.child) == .pointer) { | 95 | if (@typeInfo(target_opt.child) == .pointer) { |
| 93 | return castPtr(DestType, target); | 96 | return castPtr(DestType, target); |
| ... | @@ -686,3 +689,14 @@ test "Extended C ABI casting" { | ... | @@ -686,3 +689,14 @@ test "Extended C ABI casting" { |
| 686 | try testing.expect(@TypeOf(Macros.L_SUFFIX(math.maxInt(c_long) + 1)) == c_longlong); // comptime_int -> c_longlong | 689 | try testing.expect(@TypeOf(Macros.L_SUFFIX(math.maxInt(c_long) + 1)) == c_longlong); // comptime_int -> c_longlong |
| 687 | } | 690 | } |
| 688 | } | 691 | } |
| | 692 | |
| | 693 | // Function with complex signature for testing the SDL case |
| | 694 | fn complexFunction(_: ?*anyopaque, _: c_uint, _: ?*const fn (?*anyopaque) callconv(.c) c_uint, _: ?*anyopaque, _: c_uint, _: [*c]c_uint) callconv(.c) usize { |
| | 695 | return 0; |
| | 696 | } |
| | 697 | |
| | 698 | test "function pointer casting" { |
| | 699 | const SDL_FunctionPointer = ?*const fn () callconv(.c) void; |
| | 700 | const fn_ptr = cast(SDL_FunctionPointer, complexFunction); |
| | 701 | try testing.expect(fn_ptr != null); |
| | 702 | } |