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

Unify length overflow checks for builders #2178

Merged
merged 3 commits into from
Sep 25, 2022

Conversation

mkrasnitski
Copy link
Collaborator

@mkrasnitski mkrasnitski commented Sep 24, 2022

No description provided.

@github-actions github-actions bot added the builder Related to the `builder` module. label Sep 24, 2022
@kangalio
Copy link
Collaborator

kangalio commented Sep 24, 2022

I think there should be a function to do the checking fn check_overflow(len: usize, max: usize, err: fn(usize) -> ModelError) -> Result<()>, called e.g. like check_overflow(length, crate::constants::EMBED_MAX_LENGTH, ModelError::EmbedTooLarge)?;. It

  • reduces code
  • allows for comments on the saturating_sub technique (it took me a few seconds to get how it works; would have been easier with comments)
  • allows to replace overflow check back to the old code because it optimizes better if we'd care about that

Some overflow errors (EmbedAmount) don't contain the overflow number, but we're on next anyways so we can fix the inconsistency, and then use our new function for those errors as well

@mkrasnitski
Copy link
Collaborator Author

A better API might be fn check_overflow(len: usize, max: usize) -> StdResult<(), usize>, which can then be combined with .map_err. This way we can avoid requiring that every overflow error type wrap the overflow amount.

@github-actions github-actions bot added the utils Related to the `utils` module. label Sep 24, 2022
@mkrasnitski mkrasnitski changed the title Use saturating_sub when calculating length overflow Unify length overflow checks for builders. Sep 24, 2022
@mkrasnitski mkrasnitski changed the title Unify length overflow checks for builders. Unify length overflow checks for builders Sep 24, 2022
@kangalio
Copy link
Collaborator

.map_err(|overflow| Error::Model(ModelError::EmbedTooLarge(overflow))) is longer than , ModelError::EmbedTooLarge though, plus why not add the overflow amount to errors?

In fact this is what delights me about programming; code simplification naturally leading you to find and fix API inconsistencies and paper cuts.

@mkrasnitski
Copy link
Collaborator Author

mkrasnitski commented Sep 24, 2022

My thoughts are that embeds and stickers are limited to such low numbers, that indicating to the user how much they've gone over the limit isn't very useful. Nobody is going to be trying to send 15 embeds in the same message. It makes much more sense when limiting text length, because there can be a lot more of it, and the limit is high, therefore it's useful to tell the user exactly by how much they've gone over.

EDIT: Also, TIL that Enum::Variant(ty1, ty2, ...) implements Fn(ty1, ty2, ...) -> Enum.

@kangalio
Copy link
Collaborator

I feel different but it's not objective and not worth bikeshedding over

@mkrasnitski
Copy link
Collaborator Author

Yes, and if those changes were to be made, it should be done in a separate PR. The main goal of this PR is ensuring overflow checks are always correctly performed.

@arqunis arqunis merged commit 2f29b95 into serenity-rs:next Sep 25, 2022
@mkrasnitski mkrasnitski deleted the saturating_sub branch September 25, 2022 13:55
mkrasnitski added a commit to mkrasnitski/serenity that referenced this pull request Oct 1, 2022
kangalio pushed a commit to kangalio/serenity that referenced this pull request Oct 2, 2022
mkrasnitski added a commit to mkrasnitski/serenity that referenced this pull request Nov 7, 2022
arqunis pushed a commit to arqunis/serenity that referenced this pull request Nov 13, 2022
mkrasnitski added a commit to mkrasnitski/serenity that referenced this pull request Feb 28, 2023
mkrasnitski added a commit to mkrasnitski/serenity that referenced this pull request May 18, 2023
mkrasnitski added a commit to mkrasnitski/serenity that referenced this pull request May 30, 2023
mkrasnitski added a commit to mkrasnitski/serenity that referenced this pull request Sep 21, 2023
mkrasnitski added a commit to mkrasnitski/serenity that referenced this pull request Oct 17, 2023
mkrasnitski added a commit to mkrasnitski/serenity that referenced this pull request Oct 24, 2023
arqunis pushed a commit to arqunis/serenity that referenced this pull request Oct 24, 2023
arqunis pushed a commit to arqunis/serenity that referenced this pull request Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builder Related to the `builder` module. utils Related to the `utils` module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants