fastrand icon indicating copy to clipboard operation
fastrand copied to clipboard

Remove Internal Mutability

Open DE0CH opened this issue 2 years ago • 4 comments

I am thinking if we can remove the internal mutability in Rng(Cell<u64>) by just using Rng<u64> and implementing methods with fn ...(&mut self). This behaviour is more consistent with the rand crate and it also makes more logical sense because an rng mutates as you use it.

This change will definitely need a major version bump.

DE0CH avatar Jul 17 '22 17:07 DE0CH

What would be the use case for this? I enjoy being able to use an RNG from a shared reference

notgull avatar Jul 17 '22 20:07 notgull

I was thinking of racing condition in concurrent thread, but the Cell prevents you from sharing an rng between threads. Now I can't think of any case where refactoring this would be beneficial. Perhaps there are some edge cases where only one mutable borrow rule can detect some bugs but I can't think of any. One minor usefulness is that it makes it clear to the user that calling .u64(..) twice will not give you the same result.

DE0CH avatar Jul 18 '22 02:07 DE0CH

A case where removing the interior mutability would be beneficial is when it is intended to share a Rng across threads, e.g. #26, that is, when someone wants to use a different interior-mutability abstraction, which is e.g. thread safe, or uses atomics instead.

fogti avatar Sep 13 '22 12:09 fogti

The use case in #26 is pretty tempting, now that I think about it. I could implement this (it would also let this crate by no_std).

notgull avatar Sep 13 '22 14:09 notgull