Skip to content

Remove min_value and max_value#1829

Merged
apoelstra merged 3 commits intorust-bitcoin:masterfrom
tcharding:05-02-rm-min-max-value
May 4, 2023
Merged

Remove min_value and max_value#1829
apoelstra merged 3 commits intorust-bitcoin:masterfrom
tcharding:05-02-rm-min-max-value

Conversation

@tcharding
Copy link
Copy Markdown
Member

@tcharding tcharding commented May 2, 2023

The new MSRV (1.48.0) uses associated consts MAX/MIN instead of functions, we had functions to be compliant with the old MSRV.

Remove all methods min_value and max_value including calls to these methods on stdlib types.

PR is now split into three patches:

  • patch 1: Add missing associated consts MIN/MAX as needed
  • patch 2: Use consts instead of method calls
  • patch 3: Deprecate methods min_value and max_value

@tcharding tcharding force-pushed the 05-02-rm-min-max-value branch from 056c219 to 38da9ac Compare May 2, 2023 05:16
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We could leave this function name the same and change it to:
pub const fn max_value() -> Amount { Amount(u64::MAX) }

That way, a depreciation warning for Amount::max_value() can be added instead of making a breaking change. Same for Amount::min_value().

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.

Agreed, we should deprecate for a version or two.

@yancyribbens
Copy link
Copy Markdown
Contributor

Concept Ack, although I think it's better to deprecate Amount::max_value and min_value() first instead of a breaking change.

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented May 2, 2023

Agree with deprecating instead.

@tcharding
Copy link
Copy Markdown
Member Author

No worries, I'll deprecate them and re-spin. Thanks

tcharding added 3 commits May 3, 2023 08:22
Add associated consts for minimum and maximum values to the `Amount` and
`SignedAmount` types.
We currently use the functions `min_value` and `max_value` because the
consts were not available in Rust 1.41.1, however we recently bumped the
MSRV so we can use the consts now.
Our previous MSRV did not support MIN/MAX associated consts so we had
methods min/max_value. Now that our MSRV is Rust 1.48.0 we can use the
consts.

Deprecate min/max_value methods in favor of MIN/MAX associated conts.
@tcharding tcharding force-pushed the 05-02-rm-min-max-value branch from 38da9ac to 6cab7be Compare May 2, 2023 22:28
@yancyribbens
Copy link
Copy Markdown
Contributor

ack 6cab7be

Copy link
Copy Markdown
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

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

ACK 6cab7be

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

Hmm, although I don't like having MAX until upper bound is decided we already have max_value. I'm not sure what to do about it.

/// The minimum value of an amount.
pub const MIN: Amount = Amount::ZERO;
/// The maximum value of an amount.
pub const MAX: Amount = Amount(u64::MAX);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would prefer not to add this until it's resolved whether Amount should have MAX equal to MAX_MONEY.

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 think we should punt on this for this PR. But my feeling is that no, MAX_MONEY feels too "arbitrary" in a language that already makes unchecked overflows quite difficult.

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 also note that Bitcoin Core will parse amounts greater than MAX_MONEY, and if we try to use that as an actual type-enforced maximum, then we won't be able to parse the same amounts, which seems like a discrepancy without a lot of value.

/// The minimum value of an amount.
pub const MIN: SignedAmount = SignedAmount(i64::MIN);
/// The maximum value of an amount.
pub const MAX: SignedAmount = SignedAmount(i64::MAX);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same as above.

@tcharding
Copy link
Copy Markdown
Member Author

tcharding commented May 3, 2023

Hmm, although I don't like having MAX until upper bound is decided we already have max_value. I'm not sure what to do about it.

I understand the sentiment but this PR in no meaningful way changes the current behaviour, this is just a language version thing - const vs method.

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK 6cab7be

@apoelstra
Copy link
Copy Markdown
Member

I'm gonna go ahead and merge, on the basis that the MAX controversy reflects existing code and we should revisit it in a separate issue/PR.

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 6cab7be

@apoelstra apoelstra merged commit ac66410 into rust-bitcoin:master May 4, 2023
@tcharding tcharding deleted the 05-02-rm-min-max-value branch May 4, 2023 22:06
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.

5 participants