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

Use swap hack rather than hidden public API #7

Draft
wants to merge 8 commits into
base: 08-13-script-buf
Choose a base branch
from

Conversation

Kixunil
Copy link

@Kixunil Kixunil commented Aug 17, 2024

I felt like writing up the change. You probably want to incorporate it into previous history rather than leaving things change back and forth.

A public API even if hidden has potential compatibility risks that we want to avoid. We could come up with better API but we can simply workaround it by temporarily swapping the script with an empty one, then modifying the vec and then swapping it back.

tcharding and others added 8 commits August 16, 2024 20:11
In preparation for moving the `ScriptBuf` as a plain old datatype to
`primitives`; separate the POD methods into their own impl block.

Refactor only, no logic changes.
Before we can move the `ScriptBuf` to `primitives` we need a few ways to
mutate the inner vector, currently done by accessing `.0`.

Add `push`, `pop`, and `extend_from_slice`. Hide them from the docs and
comment that they are not really supposed to be used.
In preparation for adding a private `ScriptBufExtPriv` trait, move the
private methods to a separate impl block.

Make the `push_slice_no_op` method have the same visibility as the
others so that the change in scope does not get hidden in the upcoming
patch that introduces the trait.

(Note this is basically just a code move and the diff shows move of other
methods not the private ones.)
As we did before with `Script`; `rustfmt` does not indent stuff in
macros so in preparation for adding extension traits using a macro
temporarily wrap the impl blocks in modules so we can run the formatter
in a patch on its own.
Run `cargo +nightly fmt`, no other changes.
The `define_extension_trait` cannot be easily extended to handle our
current usage of `AsRef<PushBytes>` generic. We can achieve almost the
same thing with the `impl` syntax.

Note that this is a breaking change because with this change turbo fish
syntax is no longer possible.
In preparation for moving the `ScriptBuf` type to the `primitives`
crate; introduce two `ScriptBuf` extension traits, one public and one
private.

Note, the private extension trait has `pub(crate)` where as before this
patch was applied we use `pub(in crate::script)`. This is because the
macro doesn't handle the latter syntax.

Note also, that the build failure when `ScriptBuf::from_hex` is not
found may be confusing because of the history of `from_hex` - users may
go looking for a `FromHex` trait. We should keep this in mind when
documenting how to use all the new extension traits.
A public API even if hidden has potential compatibility risks that we
want to avoid. We could come up with better API but we can simply
workaround it by temporarily swapping the script with an empty one, then
modifying the vec and then swapping it back.
Comment on lines 244 to 245
self.as_byte_vec().pop();
self.push_opcode(opcode);
Copy link
Owner

Choose a reason for hiding this comment

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

That's some trickery - nice one!

@tcharding tcharding force-pushed the 08-13-script-buf branch 4 times, most recently from 836a226 to 2bb90b8 Compare August 19, 2024 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants