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

Make Uint64, Uint128, Int64, Int128 usable as keys #86

Merged
merged 2 commits into from
Sep 24, 2024
Merged

Conversation

uint
Copy link
Contributor

@uint uint commented Sep 23, 2024

This doesn't break the API in a very serious way.

I think if we want to do the same for 256-bit and 512-bit values, we'll need to break API by adding larger variants to this enum. I guess we might as well (in another PR) since we're likely to release cw-storage-plus 3.0.0.

@uint uint requested a review from webmaster128 September 23, 2024 15:59
src/int_key.rs Outdated

#[inline]
fn to_cw_bytes(&self) -> Self::Buf {
self.$tt().to_cw_bytes()
Copy link
Member

Choose a reason for hiding this comment

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

Can we implement this with

$tt::from(self)

i.e. using the From implementation to get the primitive type. Feels more stable than this somewhat strange getter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This conversion doesn't seem to be present for signed types, probably because the conversion isn't trivial

Copy link
Member

Choose a reason for hiding this comment

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

Ah, no, I mean the first one to primitive type. So this in total:

$tt::from(self).to_cw_bytes()

But it seems like exporters like there is no From for Uint128 -> uint128 and similar. This would be nice to have for sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I did try that. I wanted a quick macro that can cover all the integer types from cosmwasm_std, but I can change it around if you feel it'd be easier for maintenance/understanding

Copy link
Member

Choose a reason for hiding this comment

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

I am wondering about a different approach which is more elegant, also looking at 256-bit and 512-bit values for which no primitive type exists.

What to_cw_bytes/from_cw_bytes does for signed integers is simply flip the first bit of the BE representation (most significant bit). So instead of going through the primitive type we can just:

  • For unsigned integers use self.to_be_bytes()
  • For signed integers use let bytes = self.to_be_bytes() and the most significant bit of bytes[0]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You may be right! I'll look at it.

I'd still rather merge this PR without 256 and 512 (seems it would require breaking API, see above). I'll do a follow-up when I'm sure the next release will be breaking anyway.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I agree with the split. I just think those iXY() getter functions to get the primary type are ugly and inconsistent and should not be built upon. However, this should not be a blocker

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think I like your solution better too. I'll give it a try.

@uint uint requested a review from webmaster128 September 24, 2024 14:07
@uint uint marked this pull request as ready for review September 24, 2024 14:07
@uint uint merged commit 9b58f53 into main Sep 24, 2024
4 of 5 checks passed
@uint uint deleted the cosmwasm-std-integers branch September 24, 2024 14:54
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