Advertisement
Guest User

Untitled

a guest
Aug 26th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. const std = @import("std");
  2. const mem = @import("std").mem;
  3. const builtin = @import("builtin");
  4.  
  5. fn Iterator(comptime Self: type, comptime Item: type) type {
  6.     return struct {
  7.         pub fn REQUIRE_next(self: *Self) ?Item {
  8.             return undefined;
  9.         }
  10.  
  11.         pub fn enumerate(self: Self) @typeOf(Enumerate(Self)) {
  12.             return Enumerate(Self);
  13.         }
  14.     };
  15. }
  16.  
  17. pub fn Enumerate(comptime Iter: type) type {
  18.     comptime {
  19.         require(Iterator(Iter, usize), Iter, "Enumerate only works for Iterators that yield usize's!");
  20.     }
  21.  
  22.     return struct {
  23.        
  24.         fn next(self: *this) ?usize {
  25.             return 42;
  26.         }
  27.     };
  28.  
  29. }
  30.  
  31. const range = struct {
  32.     const Self = this;
  33.  
  34.     fn init() Self {
  35.         return Self;
  36.     }
  37.  
  38.     fn next(self: *Self) ?usize {
  39.         return 43;
  40.     }
  41. };
  42.  
  43. fn require(comptime TRAIT: var, comptime CHECK: var, comptime msg: []const u8) void {
  44.     const fn_names_to_check = @typeInfo(TRAIT).Struct;
  45.    
  46.     const to_check_type_info = @typeInfo(CHECK).Struct;
  47.    
  48.     inline for(fn_names_to_check.defs) |name| {
  49.         inline for (to_check_type_info.defs) |def| {
  50.             if (mem.eql(u8, name.name[0..7], "REQUIRE")) {
  51.                 if(mem.eql(u8, name.name[8..], def.name)) {
  52.                     if (mem.eql(u8, @typeName(name.data.Fn.fn_type), @typeName(def.data.Fn.fn_type))) {
  53.                         return;
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59.  
  60.     @compileError(msg);
  61. }
  62.  
  63. pub fn main() void {
  64.  
  65.  
  66.     var r = range.init();
  67.  
  68.     var enumer = Iterator(range, usize).enumerate(r);
  69.    
  70.     std.debug.warn("Printing to stdout");
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement