Skip to content

Conversation

@Swiftb0y
Copy link
Contributor

@Swiftb0y Swiftb0y commented Jun 7, 2025

This allows precomputing a set of colors ahead of time (for people that are into that). To avoid generating the same colors for each instance, users can leverage the const_random crate to properly seed their generator.

I don't actually know how useful this is. I just saw a "constexpr all the things" opportunity and wanted to see if there was anything obvious preventing this.

One usecase I could think about is users wanting to generate a couple colors ahead of time, give each one semantic meaning and then use their meaning consistently. Something like this:

struct ColorPalette {
    types: Color,
    identifiers: Color,
    other: Color,
}
const COLORS: ColorPalette = {
    let mut gen = ColorGenerator::new();
    ColorPalette {
        types: gen.next(),
        identifiers: gen.next(),
        others: gen.next(),
    }
}

@zesterer
Copy link
Owner

zesterer commented Jun 8, 2025

This is neat, great idea! It never occurred to me that this even could be made const.

@zesterer
Copy link
Owner

zesterer commented Jun 8, 2025

You mentioned const-random: was this PR intended to provide integration with that?

Also, it looks like CI might need some fixing.

@Swiftb0y
Copy link
Contributor Author

Swiftb0y commented Jun 8, 2025

You mentioned const-random: was this PR intended to provide integration with that?

I didn't want to pull in extra dependencies unprompted, especially proc macros like const-random that affect the build time. If you think it makes sense to build in support natively (maybe behind a feature flag?) I can do that.

Also, it looks like CI might need some fixing.

I'll take a look. I only ran cargo test locally.

@Swiftb0y Swiftb0y force-pushed the feat/const-colorgenerator branch from b745c01 to 7f21034 Compare June 8, 2025 09:50
@zesterer
Copy link
Owner

zesterer commented Jun 8, 2025

Ah, still some non-const stuff. Just min/max though, so shouldn't be too difficult to fix with a custom impl.

@Swiftb0y Swiftb0y force-pushed the feat/const-colorgenerator branch from 7f21034 to a6b5d12 Compare June 8, 2025 19:36
@Swiftb0y
Copy link
Contributor Author

Swiftb0y commented Jun 8, 2025

either that or bump the rust version enough again (I overlooked that previously) if thats okay for you (idk if you have a MSRV policy). I'm not sure about the &mut T in const fn, the feature is stabilized but I couldn't find the corresponding compiler version. Maybe the one I just pushed is enough, but I can't tell without trying on CI again (or testing a bunch of versions locally, which I don't have the disk space for). I'd appreciate if you could just keep on approving the workflow runs until CI finally passes.

Swiftb0y added 2 commits June 8, 2025 23:02
This allows precomputing a set of colors ahead of time (for people
that are into that). To avoid generating the same colors for each
instance, users can leverage the `const_random` crate to properly
seed their generator.

I don't actually know how useful this is. I just saw a "constexpr
all the things" opportunity and wanted to see if there was anything
obvious preventing this.

One usecase I could think about is users wanting to generate a
couple colors ahead of time, give each one semantic meaning and
then use their meaning consistently. Something like this:

```rust
struct ColorPalette {
    types: Color,
    identifiers: Color,
    other: Color,
}
const COLORS: ColorPalette = {
    let mut gen = ColorGenerator::new();
    ColorPalette {
        types: gen.next(),
        identifiers: gen.next(),
        others: gen.next(),
    }
}
```
@Swiftb0y Swiftb0y force-pushed the feat/const-colorgenerator branch from a6b5d12 to 67561aa Compare June 8, 2025 21:03
@Swiftb0y
Copy link
Contributor Author

Swiftb0y commented Jun 9, 2025

@zesterer I believe this is ready, I just need another CI run again.

@zesterer zesterer merged commit 67ed903 into zesterer:main Jun 10, 2025
2 of 3 checks passed
@zesterer
Copy link
Owner

Thanks so much!

@Swiftb0y Swiftb0y deleted the feat/const-colorgenerator branch June 10, 2025 17:29
@Swiftb0y
Copy link
Contributor Author

Thank you for your review patience.

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.

2 participants