-
Notifications
You must be signed in to change notification settings - Fork 431
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 a way to derive Standard
and Uniform
distributions for a struct
#1524
Comments
The We could, then, add derive support. It would not be part of the It should be obvious that all such fields would be sampled independently, thus e.g. samples of On the above snippets:
|
I meant to post this yesterday: Shortly after making this feature request, I decided to start work on https://github.com/LikeLakers2/michis_rand_distr_derive, which is a proc-macro crate for deriving However, its primary use is as derive-macro practice for myself - and in any case, it's not finished. Still, I wonder if it may be useful to some folks? @dhardy Thanks for the comments. RE the snippets:
Just to make sure I understand: When you say "fake name", you're referring to
I do admit that a derive macro doing more than implementing a trait is odd. However, I'm not sure an attribute macro: #[generate_uniform_support]
struct Vec3(f32, f32, f32); or a function-like macro: generate_uniform_support! {
struct Vec3(f32, f32, f32);
} would be any better, since several items ( |
I can see the general use case for creating test instances of structs. I also don't think that people would care about the details of the resulting distribution (cube vs sphere), they just want something "random" For this usecase a Probably this would be functionality for it's own crate outside of the scope of rand. But I also see the appeal of rand being the one stop thing for everything random. |
They're not. I guess what we could do is something like this: [#derive_distr(Uniform, sampler=Vec3Uniform)]`
struct Vec3(f32, f32, f32); in order to let users supply a name... but that is overly complicated (also less obvious what it does just reading it).
You are referring to something like the old |
Yes. I think this limitation is fine if someone just wants something random. If you need more control you implement it yourself. |
Background
What is your motivation?
rand
offers theStandard
andUniform
distributions, for making random samples of a primitive or struct. However, to implement them for a new type, the impls (impl Distribution<T> for Standard
forStandard
;impl SampleUniform for T
andimpl UniformSampler for TUniformSampler
forUniform
) must currently be hand-written.What type of application is this?
No specific type of application, though I write this after having to manually implement
Uniform
distribution support for theglam
crate.Feature request
I propose that derives be added for the
Standard
andUniform
distributions. When used on a struct where all fields already implement support for that type of distribution, it will generate the needed code to useStandard
/Uniform
distributions with that struct.A derive for the
Standard
distribution would probably look like this (click the arrow to expand):Standard distribution derive
This derive would generate code similar to the following:
A derive for the
Uniform
distribution would be much the same, albeit with the addition of a struct being generated for theUniformSampler
impl:Uniform distribution derive
generates the following code:
If I've missed any details, or if there's any questions you have for me regarding this suggestion, please don't hesitate to let me know.
The text was updated successfully, but these errors were encountered: