8000 Add VarInt from implementations by way of macro by tcharding · Pull Request #2024 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add VarInt from implementations by way of macro #2024

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

Merged

Conversation

tcharding
Copy link
Member

Throughout the codebase we cast values to u64 when constructing a VarInt. We can make the code marginally cleaner by adding From<T> impls for all unsigned integer types less than or equal to 64 bits. Also allows us to (possibly unnecessarily) comment the cast in a single place.

Throughout the codebase we cast values to `u64` when constructing a
`VarInt`. We can make the code marginally cleaner by adding `From<T>`
impls for all unsigned integer types less than or equal to 64 bits.
Also allows us to (possibly unnecessarily) comment the cast in a single
place.
@tcharding
Copy link
Member Author
tcharding commented Aug 24, 2023

If this is agreeable we could use the same idea (along with custom traits when necessary) for any constructor in our codebase that takes a u64 eg, Weight::from_wu. That would get rid of from_wu_usize in #2010.

EDIT: hmm maybe using a custom trait is too burdensome on users (importing the trait)?

@apoelstra
Copy link
Member

@tcharding I like this idea for VarInt, which is just an encoding wrapper around an arbitrary number. But I don't think it makes sense for other types like Weight which have a real meaning.

@tcharding
Copy link
Member Author
tcharding commented Aug 24, 2023

Just checking it was clear from what I wrote before, I didn't mean adding From impls I meant add a trait FromWu and blanket implement that for all unsigned ints less that 64 bits.

Copy link
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.

utACK 0419fa2

@@ -436,26 +451,26 @@ impl Decodable for VarInt {
if x < 0x100000000 {
Err(self::Error::NonMinimalVarInt)
} else {
Ok(VarInt(x))
Ok(VarInt::from(x))
Copy link
Contributor

Choose a reason for hiding this comment

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

nit this does not seem to be a cast here.

@yancyribbens
Copy link
Contributor

It seems like a lot of code to change:

VarInt(x as u64)
to
VarInt::from(x).

Interesting idea though. I suppose this could help future proof for accidentally truncating u128 as u64 if that's a thing that could happen.

Copy link
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 0419fa2

@apoelstra apoelstra merged commit 407dec0 into rust-bitcoin:master Aug 24, 2023
@tcharding tcharding deleted the 08-24-var-int-usize-constructor branch August 24, 2023 23:28
@stevenroose
Copy link
Collaborator

I don't really see what the added value is/was for this MR.. I don't see how the new version is better.

On several occasions I have thought the VarInt type is weird as it's really never stored. Our WriteExt and ReadExt could just have methods emit_varint and read_varint methods instead, using u64 in the interface.

If anyone likes this, I could try this out.

@apoelstra
Copy link
Member

@stevenroose yeah, that may be the better approach here. We may even be able to genericize it so there was a trait that applied to all numeric types allowing them to be emit_varinted. But ideally not a trait that you need to import everywhere it's used.

@stevenroose
Copy link
Collaborator

@stevenroose yeah, that may be the better approach here. We may even be able to genericize it so there was a trait that applied to all numeric types allowing them to be emit_varinted. But ideally not a trait that you need to import everywhere it's used.

Did this here: #2133

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
0