authorgravatar for 37453713+ominitay@users.noreply.github.comominitay <37453713+ominitay@users.noreply.github.com> 2021-10-29 00:34:17+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-29 19:20:31-04:00
log544d7d99822912a4f85768b0df882775e5fdb80d
tree5510c47eae76e09dda02ba0fd0c483841ba70b1f
parent66b4bd19c08963b750f6b186739d4ac9402fd896

Add argument for `fillFn` to `Random.init`

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 {
3232 ptr: *c_void,
3333 fillFn: fn (ptr: *c_void, buf: []u8) void,
3434
35 pub fn init(pointer: anytype) Random {
35 pub fn init(pointer: anytype, comptime fillFn: fn (ptr: @TypeOf(pointer), buf: []u8) void) Random {
3636 const Ptr = @TypeOf(pointer);
3737 assert(@typeInfo(Ptr) == .Pointer); // Must be a pointer
3838 assert(@typeInfo(Ptr).Pointer.size == .One); // Must be a single-item pointer
3939 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
4140 const gen = struct {
4241 fn fill(ptr: *c_void, buf: []u8) void {
4342 const alignment = @typeInfo(Ptr).Pointer.alignment;
4443 const self = @ptrCast(Ptr, @alignCast(alignment, ptr));
45 self.fill(buf);
44 fillFn(self, buf);
4645 }
4746 };
4847
......@@ -333,7 +332,7 @@ const SequentialPrng = struct {
333332 }
334333
335334 pub fn random(self: *Self) Random {
336 return Random.init(self);
335 return Random.init(self, fill);
337336 }
338337
339338 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 {
2121}
2222
2323pub fn random(self: *Gimli) Random {
24 return Random.init(self);
24 return Random.init(self, fill);
2525}
2626
2727pub 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 {
3131}
3232
3333pub fn random(self: *Isaac64) Random {
34 return Random.init(self);
34 return Random.init(self, fill);
3535}
3636
3737fn 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 {
2222}
2323
2424pub fn random(self: *Pcg) Random {
25 return Random.init(self);
25 return Random.init(self, fill);
2626}
2727
2828fn next(self: *Pcg) u32 {
lib/std/rand/Sfc64.zig+1-1
......@@ -24,7 +24,7 @@ pub fn init(init_s: u64) Sfc64 {
2424}
2525
2626pub fn random(self: *Sfc64) Random {
27 return Random.init(self);
27 return Random.init(self, fill);
2828}
2929
3030fn next(self: *Sfc64) u64 {
lib/std/rand/Xoroshiro128.zig+1-1
......@@ -17,7 +17,7 @@ pub fn init(init_s: u64) Xoroshiro128 {
1717}
1818
1919pub fn random(self: *Xoroshiro128) Random {
20 return Random.init(self);
20 return Random.init(self, fill);
2121}
2222
2323fn next(self: *Xoroshiro128) u64 {
lib/std/rand/Xoshiro256.zig+1-1
......@@ -19,7 +19,7 @@ pub fn init(init_s: u64) Xoshiro256 {
1919}
2020
2121pub fn random(self: *Xoshiro256) Random {
22 return Random.init(self);
22 return Random.init(self, fill);
2323}
2424
2525fn next(self: *Xoshiro256) u64 {