| author | |
| committer | |
| log | 544d7d99822912a4f85768b0df882775e5fdb80d |
| tree | 5510c47eae76e09dda02ba0fd0c483841ba70b1f |
| parent | 66b4bd19c08963b750f6b186739d4ac9402fd896 |
As suggested by @leecannon, this provides more flexibility to the
`Random` interface. For exmaple, this allows for an implementation to
provide multiple different fill functions.7 files changed, 9 insertions(+), 10 deletions(-)
lib/std/rand.zig+3-4| ... | ... | @@ -32,17 +32,16 @@ pub const Random = struct { |
| 32 | 32 | ptr: *c_void, |
| 33 | 33 | fillFn: fn (ptr: *c_void, buf: []u8) void, |
| 34 | 34 | |
| 35 | pub fn init(pointer: anytype) Random { | |
| 35 | pub fn init(pointer: anytype, comptime fillFn: fn (ptr: @TypeOf(pointer), buf: []u8) void) Random { | |
| 36 | 36 | const Ptr = @TypeOf(pointer); |
| 37 | 37 | assert(@typeInfo(Ptr) == .Pointer); // Must be a pointer |
| 38 | 38 | assert(@typeInfo(Ptr).Pointer.size == .One); // Must be a single-item pointer |
| 39 | 39 | assert(@typeInfo(@typeInfo(Ptr).Pointer.child) == .Struct); // Must point to a struct |
| 40 | assert(std.meta.trait.hasFn("fill")(@typeInfo(Ptr).Pointer.child)); // Struct must provide the `fill` function | |
| 41 | 40 | const gen = struct { |
| 42 | 41 | fn fill(ptr: *c_void, buf: []u8) void { |
| 43 | 42 | const alignment = @typeInfo(Ptr).Pointer.alignment; |
| 44 | 43 | const self = @ptrCast(Ptr, @alignCast(alignment, ptr)); |
| 45 | self.fill(buf); | |
| 44 | fillFn(self, buf); | |
| 46 | 45 | } |
| 47 | 46 | }; |
| 48 | 47 | |
| ... | ... | @@ -333,7 +332,7 @@ const SequentialPrng = struct { |
| 333 | 332 | } |
| 334 | 333 | |
| 335 | 334 | pub fn random(self: *Self) Random { |
| 336 | return Random.init(self); | |
| 335 | return Random.init(self, fill); | |
| 337 | 336 | } |
| 338 | 337 | |
| 339 | 338 | pub fn fill(self: *Self, buf: []u8) void { |
lib/std/rand/Gimli.zig+1-1| ... | ... | @@ -21,7 +21,7 @@ pub fn init(secret_seed: [secret_seed_length]u8) Gimli { |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | pub fn random(self: *Gimli) Random { |
| 24 | return Random.init(self); | |
| 24 | return Random.init(self, fill); | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | pub fn fill(self: *Gimli, buf: []u8) void { |
lib/std/rand/Isaac64.zig+1-1| ... | ... | @@ -31,7 +31,7 @@ pub fn init(init_s: u64) Isaac64 { |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | pub fn random(self: *Isaac64) Random { |
| 34 | return Random.init(self); | |
| 34 | return Random.init(self, fill); | |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | fn step(self: *Isaac64, mix: u64, base: usize, comptime m1: usize, comptime m2: usize) void { |
lib/std/rand/Pcg.zig+1-1| ... | ... | @@ -22,7 +22,7 @@ pub fn init(init_s: u64) Pcg { |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | pub fn random(self: *Pcg) Random { |
| 25 | return Random.init(self); | |
| 25 | return Random.init(self, fill); | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | fn next(self: *Pcg) u32 { |
lib/std/rand/Sfc64.zig+1-1| ... | ... | @@ -24,7 +24,7 @@ pub fn init(init_s: u64) Sfc64 { |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | pub fn random(self: *Sfc64) Random { |
| 27 | return Random.init(self); | |
| 27 | return Random.init(self, fill); | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | fn next(self: *Sfc64) u64 { |
lib/std/rand/Xoroshiro128.zig+1-1| ... | ... | @@ -17,7 +17,7 @@ pub fn init(init_s: u64) Xoroshiro128 { |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | pub fn random(self: *Xoroshiro128) Random { |
| 20 | return Random.init(self); | |
| 20 | return Random.init(self, fill); | |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | fn next(self: *Xoroshiro128) u64 { |
lib/std/rand/Xoshiro256.zig+1-1| ... | ... | @@ -19,7 +19,7 @@ pub fn init(init_s: u64) Xoshiro256 { |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | pub fn random(self: *Xoshiro256) Random { |
| 22 | return Random.init(self); | |
| 22 | return Random.init(self, fill); | |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | fn next(self: *Xoshiro256) u64 { |