Guest User

Untitled

a guest
Aug 26th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. fn AsRef(comptime SELF: type, comptime Target: type) type {
  2.     return struct {
  3.         pub fn REQUIRE_as_ref(self: *const SELF) *const Target {
  4.             return undefined;
  5.         }
  6.     };
  7. }
  8.  
  9. fn AsMut(comptime SELF: type, comptime Target: type) type {
  10.     comptime {
  11.         require(AsRef(SELF, Target), SELF, "AsRef is a superset of AsMut, and should therefore be implemented.");
  12.     }
  13.    
  14.     return struct {
  15.         pub fn REQUIRE_as_mut(self: *SELF) *Target {
  16.             return undefined;
  17.         }
  18.  
  19.         pub fn update(item: *SELF, new: Target) void {
  20.             item.as_mut().* = new;
  21.         }
  22.     };
  23. }
  24.  
  25.  
  26. fn require(comptime TRAIT: var, comptime CHECK: var, comptime msg: []const u8) void {
  27.     const fn_names_to_check = @typeInfo(TRAIT).Struct;
  28.    
  29.     const to_check_type_info = @typeInfo(CHECK).Struct;
  30.    
  31.     inline for(fn_names_to_check.defs) |name| {
  32.         inline for (to_check_type_info.defs) |def| {
  33.             if (mem.eql(u8, name.name[0..7], "REQUIRE")) {
  34.                 if(mem.eql(u8, name.name[8..], def.name)) {
  35.                     if (mem.eql(u8, @typeName(name.data.Fn.fn_type), @typeName(def.data.Fn.fn_type))) {
  36.                         return;
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.     }
  42.  
  43.     @compileError(msg);
  44. }
  45.  
  46. const thingy = struct {
  47.     data: usize,
  48.     pub fn next(self: *this) ?u32 {
  49.         return 10;
  50.     }
  51.  
  52.     pub fn as_ref(self: *const this) *const usize {
  53.         return &(self.data);
  54.     }
  55.  
  56.     pub fn as_mut(self: *this) *usize {
  57.         return &(self.data);
  58.     }
  59. };
  60.  
  61. pub fn test_asref(comptime T: type, item: T) void {
  62.     comptime {
  63.         require(AsRef(T, usize), T, "T needs to implement AsRef");
  64.     }
  65.  
  66.     std.debug.warn("data: {}\n", item.as_ref().* );
  67. }
  68.  
  69. pub fn test_asmut(comptime T: type, item: *T) void {
  70.     comptime {
  71.         require(AsMut(T, usize), T, "T needs to implement AsMut");
  72.     }
  73.  
  74.     AsMut(T, usize).update(item, 100);
  75.  
  76.     std.debug.warn("data: {}\n", item.as_ref().* );
  77. }
  78.  
  79. pub fn repro(comptime T: type, item: T) void {
  80.     comptime {
  81.         require(AsMut(T, usize), T, "T needs to implement AsMut");
  82.     }
  83.  
  84.     var ptr = item.as_mut();
  85.    
  86.     std.debug.warn("data: {}\n", item.as_ref().* );
  87. }
  88.  
  89.  
  90. pub fn main() void {
  91.  
  92.     var thing = thingy { .data = 10 };
  93.  
  94.     test_asmut(thingy, &thing);
  95.  
  96.     std.debug.warn("after test_asmut: {}\n", thing.data);
  97.  
  98.     repro(thingy, thing);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment