Skip to content

Move Rng to rand-core crate? #264

@dhardy

Description

@dhardy

This has come up many times already, so quick summary:

  • move Rng, SeedableRng, Error, and some related code to a new crate, rand-core
  • move some helper code for implementing Rng (impls and le modules) to rand-core
  • export all but the helper code in rand too
    Do we want to do this?

Note: this has already been tested.

Also note: rand already supports no_std; we don't need rand-core for that!

I think it's time we tried to answer this question. I already know some people would like to see this, but I'm less clear of all the reasons. I know @burdges doesn't, because it divides related logic between two different crates, and is mostly unnecessary.

For:

  • fairly clean separation; we can put backend docs in rand-core and user docs in rand
  • enables us to move PRNG implementations into sub-crates, and use code from there (see below)
  • enables some other crate tricks, e.g. allowing users to replace OsRng (see below)
  • less code to review for crates only needing rand-core

Against:

  • doing this requires Add trait SampleRng: Rng and related doc #244 (or something very similar) — contentious but not a big deal
  • this might restrict specialisation implementations — no currently known issues
  • code and documentation gets split between two crates (though this is also a potential advantage)

PRNG sub-crates: we can for example move XorShiftRng to a rand-small crate, which depends on the rand-core, and have rand expose XorShiftRng as WeakRng. If we tried to do this without a separate rand-core there would be a cyclic dependency randrand-smallrand, so we might have to copy the PRNG code.

User-replaceable OsRng: @pitdicker mentioned this somewhere: move OsRng to a sub-crate, and allow users building a binary on a platform not supported by our OsRng to replace our rand-os crate with their own implementation. It is not practical for us to provide an OS entropy source in all cases, e.g. WASM and embedded platforms, and this would allow users to keep thread_rng etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-questionParticipation: opinions wanted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions