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

Add support for rand's Uniform distribution to all Vec types #580

Conversation

LikeLakers2
Copy link
Contributor

@LikeLakers2 LikeLakers2 commented Nov 1, 2024

Closes #579.

This PR makes the following changes to the glam crate:

  • Adds support for rand's Uniform distribution to all Vec types
  • Reorganizes the imports for the impl_rand.rs file into their relevant macros, easing readability and maintainability

This is still a Work-In-Progress

I have yet to do the following:

  • Add tests for Float Vec types

I suspect that the code quality could also be improved in some places. If you spot any way to improve it, let me know! (or if you're bitshifter, feel free to commit directly to my PR!)

Some notes

The UniformVec* structs used to handle uniform distribution are part of a non-public module, and thus cannot be navigated to in the documentation. While I can rectify this upon request, I don't believe it's necessary, since the way rand intends for the user to perform Uniform distribution is through the rand::distributions::Uniform type, rather than using the backend types directly.

I have not added (de)serialization support to the UniformVec* structs. However, rand offers (de)serialization of its distribution structs, so I am unsure if (de)serialization is wanted for UniformVec*.

…hich will be proxies to UniformInt's impls; plus, remove UniformFloatVec*
…cro, and additionally change a bunch of vec-type constructors to use the From trait, so that it works across all vec types
…le certain traits on `rand::distributions::Uniform`
This was needed with how I was originally building the impls - but I never thought to remove it afterward.
Float vector types seem to fail when running these tests, due to a range overflow, so more work is needed
@bitshifter
Copy link
Owner

This looks OK to me so far.

… of the function

We may be able to expand this macro to be used for both int and float purposes, although for now it only covers ints
@LikeLakers2
Copy link
Contributor Author

LikeLakers2 commented Nov 3, 2024

@bitshifter Thank you. I'm working on cleaning up the code currently, and I still need to make tests for float types; but the main functionality is finished.

* Reuse `int_rng` and `vec_rng` to reduce the amount of code generated
* Clarify the __repeat_code comments
* `test_uniform` now generates tests for both ref and non-ref versions
* Spread macro parameters as we'll be going over 80 characters
…tests for float types; also implements a new `upper_range_multiplier` parameter on the macro to sidestep the "Range overflow" issue I was getting before with floats
@LikeLakers2
Copy link
Contributor Author

LikeLakers2 commented Nov 5, 2024

I believe this PR is ready for review and merging. All vec types (except Vec3A) now have uniform distribution support (and a basic test for each).

P.S. After this PR is merged, I will probably look at refactoring/rewriting impl_rand.rs. I feel I can improve the code quality of the file as a whole by doing so - but I also feel such a large project is out-of-scope for this PR. Plus, I'm a bit tired from working on this PR. So I'll leave such a refactor for another time.

@LikeLakers2 LikeLakers2 marked this pull request as ready for review November 5, 2024 04:17
@bitshifter bitshifter merged commit bc0d551 into bitshifter:main Nov 5, 2024
18 checks passed
@bitshifter
Copy link
Owner

Thanks for this!

@LikeLakers2 LikeLakers2 deleted the features/rand-uniform-distribution-for-vecs branch November 5, 2024 17:59
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.

Add support for rand's uniform distribution, when using the rand feature
2 participants