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 Pair more extensible #1101

Closed
wants to merge 1 commit into from
Closed

Conversation

ethanfrey
Copy link
Member

Closes #1100

@@ -2,7 +2,8 @@ use crate::errors::StdError;
use std::convert::TryFrom;

/// A Key-Value pair, returned from our iterators
pub type Pair<V = Vec<u8>> = (Vec<u8>, V);
/// (since it is less common to use, and never used with default V, we place K second)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really want this? It makes sense, but it feels unnatural.

I did something similar with Prefix, but in that case V was mandatory, and putting K second was the way to make it optional.

Then I realized that if we make V optional (i.e. with a default), the changes to support the "natural" order are relatively straightforward.

Copy link
Contributor

@maurolacy maurolacy Sep 22, 2021

Choose a reason for hiding this comment

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

I'll publish that into my draft PR.

In my opinion it would be better to try and support the K, V order. I don't know about the changes needed in Pair's current usage, though.

Copy link
Member Author

Choose a reason for hiding this comment

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

We have Pair used in many places throughout the code (especially in storage-plus)
I assume this will be the most common usage.

I was trying to avoid breakage. You can make the version with breakage and see which we like more.

Copy link
Member

Choose a reason for hiding this comment

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

I'd also favour a version where the generic arguments are in the same order as the two fields. If this was a struct with two fields, the order would not matter that much. But here we have unnamed positional fields.

I see Ethan's point as well. We try to do a lot of different things with one symbol. In cosmwasm_std we don't need the generic arguments at all because all is binary. Then we build higher and higher and using the same type. I'm not surprised this ends up non-ideal. But at the end it is probably more beginner friendly to only have one type.

By the way, since this is a generic pair, we probably should rename the arguments to A and B for .0 and .1.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please see #1106.

Copy link
Member Author

Choose a reason for hiding this comment

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

By the way, since this is a generic pair, we probably should rename the arguments to A and B for .0 and .1.

This will be super confusing. When I get it I usually write something like |(k, v)| Ok(String::from_utf8(&k), from_slice(&v))

Having A and B would make it more confusing.

This was previously named KV before being renamed to Pair and it is not intended to be generic, but return data from an Storage Iterator.

Copy link
Contributor

@maurolacy maurolacy left a comment

Choose a reason for hiding this comment

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

Lgtm.

@ethanfrey
Copy link
Member Author

Discussed in #1106

Not going to change it, but use a new type in storage-plus where we want a generic K. This type works well for this one use case

@ethanfrey ethanfrey closed this Sep 27, 2021
@webmaster128 webmaster128 deleted the 1100-make-pair-extensible branch September 27, 2021 14:12
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.

Generalize std::Pair
3 participants