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 {...@@ -32,17 +32,16 @@ pub const Random = struct {
32 ptr: *c_void,32 ptr: *c_void,
33 fillFn: fn (ptr: *c_void, buf: []u8) void,33 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 {
36 const Ptr = @TypeOf(pointer);36 const Ptr = @TypeOf(pointer);
37 assert(@typeInfo(Ptr) == .Pointer); // Must be a pointer37 assert(@typeInfo(Ptr) == .Pointer); // Must be a pointer
38 assert(@typeInfo(Ptr).Pointer.size == .One); // Must be a single-item pointer38 assert(@typeInfo(Ptr).Pointer.size == .One); // Must be a single-item pointer
39 assert(@typeInfo(@typeInfo(Ptr).Pointer.child) == .Struct); // Must point to a struct39 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 const gen = struct {40 const gen = struct {
42 fn fill(ptr: *c_void, buf: []u8) void {41 fn fill(ptr: *c_void, buf: []u8) void {
43 const alignment = @typeInfo(Ptr).Pointer.alignment;42 const alignment = @typeInfo(Ptr).Pointer.alignment;
44 const self = @ptrCast(Ptr, @alignCast(alignment, ptr));43 const self = @ptrCast(Ptr, @alignCast(alignment, ptr));
45 self.fill(buf);44 fillFn(self, buf);
46 }45 }
47 };46 };
4847
...@@ -333,7 +332,7 @@ const SequentialPrng = struct {...@@ -333,7 +332,7 @@ const SequentialPrng = struct {
333 }332 }
334333
335 pub fn random(self: *Self) Random {334 pub fn random(self: *Self) Random {
336 return Random.init(self);335 return Random.init(self, fill);
337 }336 }
338337
339 pub fn fill(self: *Self, buf: []u8) void {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,7 +21,7 @@ pub fn init(secret_seed: [secret_seed_length]u8) Gimli {
21}21}
2222
23pub fn random(self: *Gimli) Random {23pub fn random(self: *Gimli) Random {
24 return Random.init(self);24 return Random.init(self, fill);
25}25}
2626
27pub fn fill(self: *Gimli, buf: []u8) void {27pub 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,7 +31,7 @@ pub fn init(init_s: u64) Isaac64 {
31}31}
3232
33pub fn random(self: *Isaac64) Random {33pub fn random(self: *Isaac64) Random {
34 return Random.init(self);34 return Random.init(self, fill);
35}35}
3636
37fn step(self: *Isaac64, mix: u64, base: usize, comptime m1: usize, comptime m2: usize) void {37fn 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,7 +22,7 @@ pub fn init(init_s: u64) Pcg {
22}22}
2323
24pub fn random(self: *Pcg) Random {24pub fn random(self: *Pcg) Random {
25 return Random.init(self);25 return Random.init(self, fill);
26}26}
2727
28fn next(self: *Pcg) u32 {28fn next(self: *Pcg) u32 {
lib/std/rand/Sfc64.zig+1-1
...@@ -24,7 +24,7 @@ pub fn init(init_s: u64) Sfc64 {...@@ -24,7 +24,7 @@ pub fn init(init_s: u64) Sfc64 {
24}24}
2525
26pub fn random(self: *Sfc64) Random {26pub fn random(self: *Sfc64) Random {
27 return Random.init(self);27 return Random.init(self, fill);
28}28}
2929
30fn next(self: *Sfc64) u64 {30fn next(self: *Sfc64) u64 {
lib/std/rand/Xoroshiro128.zig+1-1
...@@ -17,7 +17,7 @@ pub fn init(init_s: u64) Xoroshiro128 {...@@ -17,7 +17,7 @@ pub fn init(init_s: u64) Xoroshiro128 {
17}17}
1818
19pub fn random(self: *Xoroshiro128) Random {19pub fn random(self: *Xoroshiro128) Random {
20 return Random.init(self);20 return Random.init(self, fill);
21}21}
2222
23fn next(self: *Xoroshiro128) u64 {23fn next(self: *Xoroshiro128) u64 {
lib/std/rand/Xoshiro256.zig+1-1
...@@ -19,7 +19,7 @@ pub fn init(init_s: u64) Xoshiro256 {...@@ -19,7 +19,7 @@ pub fn init(init_s: u64) Xoshiro256 {
19}19}
2020
21pub fn random(self: *Xoshiro256) Random {21pub fn random(self: *Xoshiro256) Random {
22 return Random.init(self);22 return Random.init(self, fill);
23}23}
2424
25fn next(self: *Xoshiro256) u64 {25fn next(self: *Xoshiro256) u64 {