Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Introduce HexWriter#156

Merged
apoelstra merged 1 commit intorust-bitcoin:masterfrom
RCasatta:hex_writer
Jun 6, 2022
Merged

Introduce HexWriter#156
apoelstra merged 1 commit intorust-bitcoin:masterfrom
RCasatta:hex_writer

Conversation

@RCasatta
Copy link
Copy Markdown
Contributor

@RCasatta RCasatta commented Jun 1, 2022

This struct create an hex string from a writer, in cases where you have
an encodable object this allows skipping the creation of the intermediate Vec<u8>

Useful in rust_bitcoin serialize_hex https://github.com/rust-bitcoin/rust-bitcoin/blob/0e82376bf85137494ff220608e80b9c78c1932b5/src/consensus/encode.rs#L149 but needs rust-bitcoin/rust-bitcoin#1027 so that result string size is known ahead of time

test hex::benches::bench_to_hex                   ... bench:       2,669 ns/iter (+/- 233)
test hex::benches::bench_to_hex_writer            ... bench:       2,034 ns/iter (+/- 167)

impl HexWriter {
/// Creates a new [`HexWriter`] specify the `capacity` of the inner `String` that will contain
/// the hex value
pub fn new(capacity: usize) -> Self {
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 new should take no parameters and we should have a separate with_capacity constructor which takes a capacity.

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, since the string must be created with the capacity then we should only have a single constructor. Seems to me that new is correct here.

/// Note that to achieve better perfomance than [`ToHex`] the struct must be
/// created with the right `capacity` of the end hex string result so that the
/// inner `String` doesn't re-allocate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What mentioned @tcharding is the reason why I avoided having the new without parameters. @apoelstra do you think it's okay?

@apoelstra
Copy link
Copy Markdown
Member

concept ACK, though CI is failing

@tcharding
Copy link
Copy Markdown
Member

tcharding commented Jun 1, 2022

CI fail is because you include test in the cfg feature gating but io is not imported for test. Since I did the change to see if tests pass here it is:

#[cfg(any(test, feature = "std"))]
use std::io;

Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

With the import fix, ACK.

this structs create an hex string from a writer, in cases where you have
an encodable object it allows skipping the creation of the intermediate Vec<u8>
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 7e79628

@apoelstra
Copy link
Copy Markdown
Member

@tcharding are your nits addressed?

@tcharding
Copy link
Copy Markdown
Member

Yep, thanks for checking with me!

Copy link
Copy Markdown
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

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

ACK 7e79628

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants