Skip to content

Commit 544d7d9

Browse files
ominitayandrewrk
authored andcommitted
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.
1 parent 66b4bd1 commit 544d7d9

File tree

7 files changed

+9
-10
lines changed

7 files changed

+9
-10
lines changed

lib/std/rand.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn init(secret_seed: [secret_seed_length]u8) Gimli {
2121
}
2222

2323
pub fn random(self: *Gimli) Random {
24-
return Random.init(self);
24+
return Random.init(self, fill);
2525
}
2626

2727
pub fn fill(self: *Gimli, buf: []u8) void {

lib/std/rand/Isaac64.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn init(init_s: u64) Isaac64 {
3131
}
3232

3333
pub fn random(self: *Isaac64) Random {
34-
return Random.init(self);
34+
return Random.init(self, fill);
3535
}
3636

3737
fn step(self: *Isaac64, mix: u64, base: usize, comptime m1: usize, comptime m2: usize) void {

lib/std/rand/Pcg.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn init(init_s: u64) Pcg {
2222
}
2323

2424
pub fn random(self: *Pcg) Random {
25-
return Random.init(self);
25+
return Random.init(self, fill);
2626
}
2727

2828
fn next(self: *Pcg) u32 {

lib/std/rand/Sfc64.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn init(init_s: u64) Sfc64 {
2424
}
2525

2626
pub fn random(self: *Sfc64) Random {
27-
return Random.init(self);
27+
return Random.init(self, fill);
2828
}
2929

3030
fn next(self: *Sfc64) u64 {

lib/std/rand/Xoroshiro128.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn init(init_s: u64) Xoroshiro128 {
1717
}
1818

1919
pub fn random(self: *Xoroshiro128) Random {
20-
return Random.init(self);
20+
return Random.init(self, fill);
2121
}
2222

2323
fn next(self: *Xoroshiro128) u64 {

lib/std/rand/Xoshiro256.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn init(init_s: u64) Xoshiro256 {
1919
}
2020

2121
pub fn random(self: *Xoshiro256) Random {
22-
return Random.init(self);
22+
return Random.init(self, fill);
2323
}
2424

2525
fn next(self: *Xoshiro256) u64 {

0 commit comments

Comments
 (0)