On Sunday, 15 May 2016 at 13:26:52 UTC, Joseph Rushton Wakeling wrote:
On Sunday, 15 May 2016 at 11:15:38 UTC, Joseph Rushton Wakeling wrote:
On Sunday, 15 May 2016 at 10:43:55 UTC, Joseph Rushton Wakeling wrote:
Probably the best way to handle this is to handle the take-the-address side of things by a @trusted wrapper that uses `return ref` to guarantee the pointer remains valid for the lifetime of the wrapper itself.

Note, I've been mulling over this myself for a while, so I'll probably put something together in a future dxorshift release (and probably try to get it in Phobos ASAP, as it will be very helpful in avoiding the worst cases of the existing RNG functionality).


Sneak preview to try out, before I tidy this up for actual inclusion in the library (needs docs, etc):

The wrapper could be smaller with an alias this:

----
public struct SafeUniformRNG (BaseRNG)
    if (isUniformRNG!BaseRNG)
{
  public:
    this (return ref BaseRNG generator) @trusted
    {
        this.gen = &generator;
    }

    enum isUniformRandom = BaseRNG.isUniformRandom;

    ref BaseRNG getGen()
    {
        return *gen;
    }

    alias getGen this;

  private:
    BaseRNG* gen;

    invariant()
    {
        assert(this.gen !is null);
    }
}
----

even if I'm not 100% sure if this is conform with previous version. At least the tests pass.


  • dxorshift: random numbe... Joseph Rushton Wakeling via Digitalmars-d-announce
    • Re: dxorshift: ran... Basile B. via Digitalmars-d-announce
      • Re: dxorshift:... Joseph Rushton Wakeling via Digitalmars-d-announce
        • Re: dxorsh... Joseph Rushton Wakeling via Digitalmars-d-announce
          • Re: dx... Joseph Rushton Wakeling via Digitalmars-d-announce
            • R... Basile B. via Digitalmars-d-announce
              • ... Joseph Rushton Wakeling via Digitalmars-d-announce
                • ... Basile B. via Digitalmars-d-announce
                • ... Joseph Rushton Wakeling via Digitalmars-d-announce
                • ... Joseph Rushton Wakeling via Digitalmars-d-announce
                • ... ag0aep6g via Digitalmars-d-announce
          • Re: dx... Joseph Rushton Wakeling via Digitalmars-d-announce
            • R... jmh530 via Digitalmars-d-announce
              • ... Kagamin via Digitalmars-d-announce
              • ... Joseph Rushton Wakeling via Digitalmars-d-announce

Reply via email to