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

Alphanumeric samples bytes instead of chars #1012

Merged
merged 3 commits into from Aug 5, 2020

Commits on Aug 1, 2020

  1. Impl Distribution<u8> for Alphanumeric

    Sampling a random alphanumeric string by collecting chars (that are known to be ASCII) into a String involves re-allocation as String is encoding to UTF-8, via the example:
    
    ```rust
    let chars: String = iter::repeat(())
            .map(|()| rng.sample(Alphanumeric))
            .take(7)
            .collect();
    ```
    
    I wanted to get rid of the clearly unnecessary re-allocations in my applications, so I needed to be able to access to the ASCII characters as simple bytes. It seems like that was already what was going on inside Alphanumeric however, it was just internal.
    
    This PR changes the `Distribution<char>` impl to provide `u8`s (which it generates internally) instead, and implements the previous `Distribution<char>` using it. One could then, for example, do this:
    
    ```rust
    let mut rng = thread_rng();
    let bytes = (0..7).map(|_| rng.sample(ByteAlphanumeric)).collect();
    let chars = unsafe { String::from_utf8_unchecked(bytes) };
    ```
    qoh authored and vks committed Aug 1, 2020
    Configuration menu
    Copy the full SHA
    ebf4e66 View commit details
    Browse the repository at this point in the history
  2. Alphanumeric samples bytes instead of chars

    The corresponds more closely to the internally used types and can be
    easily converted to a `char` via `From` and `Into`, while being more
    flexible to use.
    
    This is a breaking change.
    vks committed Aug 1, 2020
    Configuration menu
    Copy the full SHA
    2c74527 View commit details
    Browse the repository at this point in the history
  3. Fix benchmarks

    vks committed Aug 1, 2020
    Configuration menu
    Copy the full SHA
    fe7b509 View commit details
    Browse the repository at this point in the history