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

std: Recreate a rand module #14427

Merged
merged 1 commit into from
May 30, 2014
Merged

std: Recreate a rand module #14427

merged 1 commit into from
May 30, 2014

Conversation

alexcrichton
Copy link
Member

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 and Arc 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]

@huonw
Copy link
Member

huonw commented May 25, 2014

cc me

@huonw
Copy link
Member

huonw commented May 26, 2014

Rand implementations for Box 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 and Arc were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.

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.

Copy link
Member

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.

Copy link
Member Author

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?

Copy link
Member

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.

@alexcrichton
Copy link
Member Author

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();
Copy link
Member

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?

@brson
Copy link
Contributor

brson commented May 28, 2014

I removed my r+ since it appears @huonw is still reviewing.

@brson
Copy link
Contributor

brson commented May 28, 2014

Does this leave a way to seed RNG's with a known value, or does it require seeding from another RNG?

@alexcrichton
Copy link
Member Author

Yes, SeedableRng::from_seed can be used if you have a known seed.

@alexcrichton
Copy link
Member Author

Updated with @huonw's comments.

/// ```
fn gen_vec<T: Rand>(&mut self, len: uint) -> Vec<T> {
Copy link
Member

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...

@huonw
Copy link
Member

huonw commented May 28, 2014

r=me with a rebase and the two minor doc comments.

@alexcrichton
Copy link
Member Author

I went ahead an implemented Rand for the RNG types, I find it a cleaner solution than adding two new methods to SeedableRng which don't have much to do with the Seed type parameter.

/// 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() })
Copy link
Member

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]
bors added a commit that referenced this pull request May 29, 2014
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]
@bors bors closed this May 30, 2014
@bors bors merged commit 925ff65 into rust-lang:master May 30, 2014
@alexcrichton alexcrichton deleted the librand branch May 30, 2014 05:31
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 5, 2023
…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.
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.

4 participants