Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const std = @import("std");
- const mem = @import("std").mem;
- const builtin = @import("builtin");
- fn Iterator(comptime Self: type, comptime Item: type) type {
- return struct {
- pub fn REQUIRE_next(self: *Self) ?Item {
- return undefined;
- }
- pub fn enumerate(self: Self) @typeOf(Enumerate(Self)) {
- return Enumerate(Self);
- }
- };
- }
- pub fn Enumerate(comptime Iter: type) type {
- comptime {
- require(Iterator(Iter, usize), Iter, "Enumerate only works for Iterators that yield usize's!");
- }
- return struct {
- fn next(self: *this) ?usize {
- return 42;
- }
- };
- }
- const range = struct {
- const Self = this;
- fn init() Self {
- return Self;
- }
- fn next(self: *Self) ?usize {
- return 43;
- }
- };
- fn require(comptime TRAIT: var, comptime CHECK: var, comptime msg: []const u8) void {
- 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[0..7], "REQUIRE")) {
- 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;
- }
- }
- }
- }
- }
- @compileError(msg);
- }
- pub fn main() void {
- var r = range.init();
- var enumer = Iterator(range, usize).enumerate(r);
- std.debug.warn("Printing to stdout");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement