Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn Iterator(comptime SELF: type, comptime Item: type) type {
- return struct {
- pub fn REQUIRE_next(self: *SELF) ?Item {
- return undefined;
- }
- };
- }
- fn AsRef(comptime SELF: type, comptime Target: type) type {
- return struct {
- pub fn REQUIRE_as_ref(self: *const SELF) *const Target {
- return undefined;
- }
- };
- }
- fn require(comptime TRAIT: var, comptime CHECK: var) bool {
- const fn_names_to_check = @typeInfo(TRAIT).Struct;
- const to_check_type_info = @typeInfo(CHECK).Struct;
- inline for(fn_names_to_check.defs) |name| {
- inline for (to_check_type_info.defs) |def| {
- if(mem.eql(u8, name.name[8..], def.name)) {
- if (mem.eql(u8, @typeName(name.data.Fn.fn_type), @typeName(def.data.Fn.fn_type))) {
- return true;
- }
- }
- }
- }
- return false;
- }
- const thingy = struct {
- data: usize,
- pub fn next(self: *this) ?u32 {
- return 10;
- }
- pub fn as_ref(self: *const this) *const usize {
- return &(self.data);
- }
- };
- pub fn test_asref(comptime T: type, item: T) void {
- comptime {
- if (!require(AsRef(T, usize), T)) {
- @compileError("T needs to implement AsRef");
- }
- }
- std.debug.warn("data: {}\n", item.as_ref().* );
- }
- pub fn main() void {
- var thing = thingy { .data = 10 };
- test_asref(thingy, thing);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement