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

Impl Distribution<u8> for Alphanumeric #935

Closed
wants to merge 1 commit into from
Closed

Commits on Jan 21, 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 committed Jan 21, 2020
    Copy the full SHA
    9f98d8f View commit details
    Browse the repository at this point in the history