Skip to content

Replace magic numbers with constants in CollationSpecialPrimaries deserialization#8083

Merged
sffc merged 1 commit into
unicode-org:mainfrom
sffc:cleanup-collator-constants-main
Jun 16, 2026
Merged

Replace magic numbers with constants in CollationSpecialPrimaries deserialization#8083
sffc merged 1 commit into
unicode-org:mainfrom
sffc:cleanup-collator-constants-main

Conversation

@sffc

@sffc sffc commented Jun 16, 2026

Copy link
Copy Markdown
Member

See #8075

Defines NUM_PRIMARIES and COMPRESSIBLE_BYTES_LEN constants on CollationSpecialPrimaries and uses them in the custom Deserialize implementation to avoid magic numbers, making the deserialization logic self-documenting.

This addresses reviewer feedback regarding magic numbers in the collation data handling.

🤖 This pull request was created by an AI agent working with @sffc.

Changelog

  • Replace magic numbers with constants in CollationSpecialPrimaries deserialization.

@sffc sffc marked this pull request as ready for review June 16, 2026 01:09
@sffc sffc requested review from echeran and hsivonen as code owners June 16, 2026 01:09
@sffc

sffc commented Jun 16, 2026

Copy link
Copy Markdown
Member Author

This seems like a good change because it makes the code on main a little bit more parallel with the code on 2.1/2.2

@sffc sffc requested review from Manishearth and robertbastian and removed request for echeran and hsivonen June 16, 2026 01:10
…eserialization

Defines NUM_PRIMARIES and COMPRESSIBLE_BYTES_LEN constants on CollationSpecialPrimaries and uses them in the custom Deserialize implementation to avoid magic numbers, making the deserialization logic self-documenting.

Co-authored-by: Gemini <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@sffc sffc force-pushed the cleanup-collator-constants-main branch from 04bd62f to 95d5f95 Compare June 16, 2026 01:24
@sffc sffc enabled auto-merge (squash) June 16, 2026 02:37
@sffc sffc merged commit 498fb39 into unicode-org:main Jun 16, 2026
34 checks passed
Comment on lines 642 to +644
// `variant_count` isn't stable yet:
// https://github.com/rust-lang/rust/issues/73662
.split_at_checked(MaxVariable::Currency as usize + 1)
.split_at_checked(CollationSpecialPrimaries::NUM_PRIMARIES)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: the "variant_count isn't stable yet" comment should be moved to the constant. it would also make more sense for the constant to live on MaxVariable, and be called something like VARIANT_COUNT, or fn variant_count().

#[cfg(feature = "serde")]
impl CollationSpecialPrimaries<'_> {
/// The number of real special primaries (Space, Punctuation, Symbol, Currency).
pub(crate) const NUM_PRIMARIES: usize = MaxVariable::Currency as usize + 1; // 4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happier with this way of variant counting if this lived next to the enum

Comment on lines +618 to +619
/// The length of the compressible bytes array (256 bits packed in 16 u16s).
pub(crate) const COMPRESSIBLE_BYTES_LEN: usize = 16;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// The length of the compressible bytes array (256 bits packed in 16 u16s).
pub(crate) const COMPRESSIBLE_BYTES_LEN: usize = 16;
/// The length of the compressible bytes array (256 bits packed in `u16`s).
pub(crate) const COMPRESSIBLE_BYTES_LEN: usize = 256 / u16::BITS;

@robertbastian robertbastian Jun 16, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that said, the / u16::BITS could also be moved to the use site, with this just being 256, the number of bytes that are compressible, without worrying about how they are packed

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.

3 participants