Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common traits #16

Merged
merged 5 commits into from
Oct 21, 2017
Merged

Common traits #16

merged 5 commits into from
Oct 21, 2017

Conversation

pitdicker
Copy link

As discussed in #13, this removes the Copy trait from Rng implementations, and adds Clone and Debug where needed.

The Debug implementations look like this:

OsRng {
    inner: OsGetrandomRng
}
StdRng {
    rng: IsaacWordRng(
        Isaac64Rng {}
    )
}
ThreadRng {
    rng: RefCell {
        value: ReseedingRng {
            rng: StdRng {
                rng: IsaacWordRng(
                    Isaac64Rng {}
                )
            },
            generation_threshold: 32768,
            bytes_generated: 0,
            reseeder: ReseedWithNew
        }
    }
}
ReadRng {
    reader: File {
        fd: 3,
        path: "/dev/urandom",
        read: true,
        write: false
    }
}
XorShiftRng {}
IsaacRng {}
Isaac64Rng {}
IsaacWordRng(
    Isaac64Rng {}
)
ChaChaRng {}
MockAddRng {
    v: 2,
    a: 1
}

(generated with)

    use NewSeeded;
    use std::fs::File;
    use {StdRng, OsRng, ReadRng};
    use prng::{XorShiftRng, IsaacRng, Isaac64Rng, IsaacWordRng, ChaChaRng};
    println!("Debug:");
    println!("{:#?}", OsRng::new().unwrap());
    println!("{:#?}", StdRng::new().unwrap());
    println!("{:#?}", thread_rng());
    {
        let file = File::open("/dev/random").unwrap();
        let mut rng = ReadRng::new(file);
    }
    println!("{:#?}", XorShiftRng::new().unwrap());
    println!("{:#?}", IsaacRng::new().unwrap());
    println!("{:#?}", Isaac64Rng::new().unwrap());
    println!("{:#?}", IsaacWordRng::new().unwrap());
    println!("{:#?}", ChaChaRng::new().unwrap());
    println!("{:#?}", ::rand_core::mock::MockAddRng::new(2u32, 1u32));

It is not easily possible to implement Clone for OsRng, because on some operating systems it needs to read from /dev/urandom, anf File does not implement Clone.

I also did not implement Clone for ReadRng. If it is initialized with a reference to the underlying reader, the clone would share the same reader, not a clone of it. That seems like an easy footgun to me.

@dhardy
Copy link
Owner

dhardy commented Oct 21, 2017

Nice print-out. Looks okay I guess.

In general cloning any external generator is problematic, 1st because the resource may not be "cloneable" and second because it might be expected that after let a = b.clone(); the following is true: a.next_u32() == b.next_u32(), which of course may not be the case for an external generator.

@dhardy dhardy merged commit 6edac27 into dhardy:master Oct 21, 2017
@pitdicker
Copy link
Author

Wow that is fast :-). I will add your comment to the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants