Skip to content

Move script types to primitives#3431

Merged
apoelstra merged 2 commits intorust-bitcoin:masterfrom
tcharding:10-01-mv-script-to-primitives
Oct 14, 2024
Merged

Move script types to primitives#3431
apoelstra merged 2 commits intorust-bitcoin:masterfrom
tcharding:10-01-mv-script-to-primitives

Conversation

@tcharding
Copy link
Copy Markdown
Member

First patch removes bytes_to_asm_fmt as requested by Kix here: #3194 (comment)

Second patch does the move. The move is minimal but there is quite a bit of code moved in script/mod.rs - I believe it is as minimal as required as well.

@github-actions github-actions bot added C-bitcoin PRs modifying the bitcoin crate C-primitives labels Oct 1, 2024
@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 1, 2024

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

@github-actions github-actions bot added the API break This PR requires a version bump for the next release label Oct 1, 2024
@tcharding tcharding force-pushed the 10-01-mv-script-to-primitives branch from 9d05c21 to 51f6bb0 Compare October 1, 2024 03:43
@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 1, 2024

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

@tcharding tcharding force-pushed the 10-01-mv-script-to-primitives branch from 51f6bb0 to 5758abf Compare October 1, 2024 04:12
@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 1, 2024

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

Copy link
Copy Markdown
Contributor

@jamillambert jamillambert left a comment

Choose a reason for hiding this comment

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

As far as I can tell both the inline of bytes_to_asm_fmt and the move to primitives are good. I checked locally that the large sections of code that were moved were unchanged, except for the necessary changes in use statements etc. and all tests pass.
Although I didn’t verify or know how to verify if everything that needed to be moved, and only those items, have been moved.

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.

In 84af64671aa72cc39f5bf67546496d827469601b:

It's fine to unwrap here but the text in the expect message makes no sense. The reason is that "the fmt::Display impl for Self does not return an error". It has nothing to do with memory or writers.

Also, the commit message says you don't know why this method used to exist but you think it is from when Script was Sized. It has nothing to do with Sized. It is because the Display impl used to add a Script(...) wrapper to its output and users (Steven) wanted to get the text out without that noise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh I thought the Write trait has an error for I/O failures but writes to a String (eg writing to memory) never fail.

Thanks for clarifying the other thing, will fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'd like to clear this up because if I see this line in code write!(&mut buf, "{}", self).unwrap(); I have to ask myself "why is it ok to unwrap?", so I think using expect("exactly correct reason why this won't panic") is better.

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.

In this case, probably the best thing to write is "if this returns an error, to_string will panic, which would be a bug in its own right".

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.

Basically, I don't think you're really justified in having this unwrap here...but the implementation of ToString on T: Display in core has the same unwrap, and so everybody who implements fmt::Display has responded by bending over backward to never actually return a fmt::Result::Err, so we're allowed to pretend like the format functions are infallible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Cheers, turns out I'm a wombat anyways, we can just call self.to_string() and bypass the whole issue - face palm.

@apoelstra
Copy link
Copy Markdown
Member

5758abf58e29d8141482d783bceff0bf1f9c4d3a looks good other than the one comment.

@tcharding tcharding force-pushed the 10-01-mv-script-to-primitives branch from 5758abf to 6c115f4 Compare October 1, 2024 21:29
@tcharding
Copy link
Copy Markdown
Member Author

Fixed the commit message but did not change the expect message.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 1, 2024

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

@tcharding
Copy link
Copy Markdown
Member Author

I checked locally that the large sections of code that were moved were unchanged

Hey @jamillambert I recently learned of some new git configuration options that make reviewing codes moves like this easier. It makes patches be colorized so that one doesn't have to manually check that large sections of code are unchanged.

Add this to your ~/.gitconfig file


[diff]
	colorMoved = zebra
        colorMovedWs = allow-indentation-change

@tcharding tcharding force-pushed the 10-01-mv-script-to-primitives branch from 6c115f4 to c2773fd Compare October 10, 2024 00:08
@github-actions
Copy link
Copy Markdown

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

@tcharding tcharding force-pushed the 10-01-mv-script-to-primitives branch from c2773fd to ef11581 Compare October 10, 2024 00:28
@github-actions
Copy link
Copy Markdown

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

@tcharding
Copy link
Copy Markdown
Member Author

tcharding commented Oct 13, 2024

Face palm, its 4am here and I got out of bed thinking that I really should get this PR done, only to find I did it 4 days ago.

EDIT: Its #3406 that got me out of bed, and it does still need doing :) My review request stand still though please.

Review please @apoelstra.

@apoelstra
Copy link
Copy Markdown
Member

Source commit: 4894aadd93b48349b511609311faea6acd81ab6e
Source: /nix/store/j2c1c8r4zfq46c673jw2cqjrf6ik34s3-source
unpacking source archive /nix/store/kr6vgwlly12xkxjp2ikj43vr928ga9z5-source
source root is source
Running phase: patchPhase
@nix { "action": "setPhase", "phase": "patchPhase" }
Running phase: updateAutotoolsGnuConfigScriptsPhase
@nix { "action": "setPhase", "phase": "updateAutotoolsGnuConfigScriptsPhase" }
Running phase: configurePhase
@nix { "action": "setPhase", "phase": "configurePhase" }
Running cd .
Running phase: buildPhase
@nix { "action": "setPhase", "phase": "buildPhase" }
Building src/lib.rs (bitcoin)
Running rustc --crate-name bitcoin src/lib.rs --out-dir target/lib -L dependency=target/deps --cap-lints allow -L native=/nix/store/np4jc4v8lq73r4laf1v92dddx6z2vvg2-rust_secp256k1-sys-0.10.0-lib/lib/secp256k1-sys.out -C debu>
error[E0599]: no method named `to_string` found for reference `&borrowed::Script` in the current scope
   --> src/blockdata/script/borrowed.rs:491:50
    |
491 |         fn to_asm_string(&self) -> String { self.to_string() }
    |                                                  ^^^^^^^^^ method not found in `&borrowed::Script`
    |
    = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
    |
3   | use crate::alloc::string::ToString;
    |

error: aborting due to previous error

For more information about this error, try `rustc --explain E0599`.

Sorry. I probably ran this several days ago and forgot to dig into and repost the failure.

This function does not need to exist anymore because it is exactly the
same as what is produced by the `Display` impl.
Move the `Script` and `ScriptBuf` types to `primitives`, nothing else.
@tcharding tcharding force-pushed the 10-01-mv-script-to-primitives branch from ef11581 to d649c06 Compare October 13, 2024 22:02
@tcharding
Copy link
Copy Markdown
Member Author

Bother, I fixed that the other day and put the fix in the wrong commit. Added import of ToString to the first commit.

@github-actions
Copy link
Copy Markdown

🚨 API BREAKING CHANGE DETECTED

To see the changes click details on "Check semver breaks / PR Semver - stable toolchain" job then expand "Run semver checker script" and scroll to the end of the section.

@apoelstra
Copy link
Copy Markdown
Member

Will also one-ack merge this since it has no comments from other maintainers and has been open for two weeks (and Kix appears to be offline anyway).

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 d649c06 successfully ran local tests

@apoelstra apoelstra merged commit 1b23d1c into rust-bitcoin:master Oct 14, 2024
@tcharding tcharding deleted the 10-01-mv-script-to-primitives branch October 14, 2024 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API break This PR requires a version bump for the next release C-bitcoin PRs modifying the bitcoin crate C-primitives

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants