-
Notifications
You must be signed in to change notification settings - Fork 13k
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
std: Recreate a rand
module
#14427
std: Recreate a rand
module
#14427
Conversation
cc me |
Presumably you mean "allow librand to not depend on liballoc"? Also I think these impls are cute and useful when you need them, but I don't think they're worth having around for now. (I.e. not depending from liballoc is good: can always change in future if the sugar is just too sweet to miss.) |
@@ -20,8 +20,10 @@ that do not need to record state. | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you mark this module #[unstable]
(or even #[experimental]
)?
That is, I don't think this is necessarily the best fit for being under std
at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole crate is currently marked #![experimental]
(although the lint attributes aren't working well enough to pick that up). Do you think I should mark this module as having a stability level with a specific message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is being reexported as std::rand::distributions
, i.e. the normal path to access it is not experimental. I want the reexported std::rand::distributions
to be experimental, and I don't really know how we're expecting stability levels to interact with reexported items.
Updated with comments. |
/// propagated via the `IoResult` return value. | ||
pub fn new() -> IoResult<StdRng> { | ||
OSRng::new().map(|mut r| { | ||
let mut ret = IsaacWordRng::new_unseeded(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be from_rng
now?
I removed my r+ since it appears @huonw is still reviewing. |
Does this leave a way to seed RNG's with a known value, or does it require seeding from another RNG? |
Yes, |
Updated with @huonw's comments. |
/// ``` | ||
fn gen_vec<T: Rand>(&mut self, len: uint) -> Vec<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's mildly annoying that these can't be #[deprecated]
like other types, but there's not much we can do about it without larger changes...
r=me with a rebase and the two minor doc comments. |
I went ahead an implemented |
/// Reading the randomness from the OS may fail, and any error is | ||
/// propagated via the `IoResult` return value. | ||
pub fn new() -> IoResult<StdRng> { | ||
OSRng::new().map(|mut r| StdRng { rng: r.gen() }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heh, this Rand
impl is just so magical. :)
This commit shuffles around some of the `rand` code, along with some reorganization. The new state of the world is as follows: * The librand crate now only depends on libcore. This interface is experimental. * The standard library has a new module, `std::rand`. This interface will eventually become stable. Unfortunately, this entailed more of a breaking change than just shuffling some names around. The following breaking changes were made to the rand library: * Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which will return an infinite stream of random values. Previous behavior can be regained with `rng.gen_iter().take(n).collect()` * Rng::gen_ascii_str() was removed. This has been replaced with Rng::gen_ascii_chars() which will return an infinite stream of random ascii characters. Similarly to gen_iter(), previous behavior can be emulated with `rng.gen_ascii_chars().take(n).collect()` * {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all relied on being able to use an OSRng for seeding, but this is no longer available in librand (where these types are defined). To retain the same functionality, these types now implement the `Rand` trait so they can be generated with a random seed from another random number generator. This allows the stdlib to use an OSRng to create seeded instances of these RNGs. * Rand implementations for `Box<T>` and `@T` were removed. These seemed to be pretty rare in the codebase, and it allows for librand to not depend on liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not supported. If this is undesirable, librand can depend on liballoc and regain these implementations. * The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`, but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice structure now has a lifetime associated with it. * The `sample` method on `Rng` has been moved to a top-level function in the `rand` module due to its dependence on `Vec`. cc rust-lang#13851 [breaking-change]
This commit shuffles around some of the `rand` code, along with some reorganization. The new state of the world is as follows: * The librand crate now only depends on libcore. This interface is experimental. * The standard library has a new module, `std::rand`. This interface will eventually become stable. Unfortunately, this entailed more of a breaking change than just shuffling some names around. The following breaking changes were made to the rand library: * Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which will return an infinite stream of random values. Previous behavior can be regained with `rng.gen_iter().take(n).collect()` * Rng::gen_ascii_str() was removed. This has been replaced with Rng::gen_ascii_chars() which will return an infinite stream of random ascii characters. Similarly to gen_iter(), previous behavior can be emulated with `rng.gen_ascii_chars().take(n).collect()` * {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all relied on being able to use an OSRng for seeding, but this is no longer available in librand (where these types are defined). To retain the same functionality, these types now implement the `Rand` trait so they can be generated with a random seed from another random number generator. This allows the stdlib to use an OSRng to create seeded instances of these RNGs. * Rand implementations for `Box<T>` and `@T` were removed. These seemed to be pretty rare in the codebase, and it allows for libcore to not depend on liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not supported. If this is undesirable, librand can depend on liballoc and regain these implementations. * The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`, but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice structure now has a lifetime associated with it. cc #13851 [breaking-change]
…ent-workspaces-to-have-proc-macros, r=Veykril fix: allow new, subsequent `rust-project.json`-based workspaces to get proc macro expansion As detailed in rust-lang/rust-analyzer#14417 (comment), `rust-project.json` workspaces added after the initial `rust-project.json`-based workspace was already indexed by rust-analyzer would not receive procedural macro expansion despite `config.expand_proc_macros` returning true. To fix this issue: 1. I changed `reload.rs` to check which workspaces are newly added. 2. Spawned new procedural macro expansion servers based on the _new_ workspaces. 1. This is to prevent spawning duplicate procedural macro expansion servers for already existing workspaces. While the overall memory usage of duplicate procedural macro servers is minimal, this is more about the _principle_ of not leaking processes 😅. 3. Launched procedural macro expansion if any workspaces are `rust-project.json`-based _or_ `same_workspaces` is true. `same_workspaces` being true (and reachable) indicates that that build scripts have finished building (in Cargo-based projects), while the build scripts in `rust-project.json`-based projects have _already been built_ by the build system that produced the `rust-project.json`. I couldn't really think of structuring this code in a better way without engaging with rust-lang/rust-analyzer#7444.
This commit shuffles around some of the
rand
code, along with somereorganization. The new state of the world is as follows:
std::rand
. This interface willeventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
will return an infinite stream of random values. Previous behavior can be
regained with
rng.gen_iter().take(n).collect()
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
rng.gen_ascii_chars().take(n).collect()
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the
Rand
trait so they can begenerated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
Box<T>
and@T
were removed. These seemed to bepretty rare in the codebase, and it allows for libcore to not depend on
liballoc. Additionally, other pointer types like Rc and Arc were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
Vec<Weighted<T>>
,but rather a
&mut [Weighted<T>]
. This means that the WeightedChoicestructure now has a lifetime associated with it.
cc #13851
[breaking-change]