aboutsummaryrefslogtreecommitdiff
path: root/src/util.zig
blob: 60462648f415f4fcd7d29d555ee3f07575e0672d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const std = @import("std");
const Type = std.builtin.Type;

pub fn typeVerify(T: type, expected: anytype) Type {
    const expectedType = @TypeOf(expected);
    const expectedTypeInfo = @typeInfo(expectedType);
    if (expectedTypeInfo != .@"struct")
        @compileError("Expected struct or tuple, found " ++ @typeName(expectedType));
    const realTypeInfo = @typeInfo(T);
    for (expected) |e| {
        if(realTypeInfo == e) return realTypeInfo;
    }
    for (expected) |e|
        @compileError("Expected one of " ++ @tagName(e) ++ ", found " ++ @typeName(T));
    return realTypeInfo;
}

pub inline fn fnInput(f: anytype) type {
    return typeVerify(@TypeOf(f), .{ .@"fn" }).@"fn".params[0].type.?;
}

pub inline fn fnOutput(f: anytype) type {
    return typeVerify(@TypeOf(f), .{ .@"fn" }).@"fn".return_type.?;
}