Skip to content

Conversation

@apoelstra
Copy link
Member

This may be too much of a breaking change and an instance of rust-bitcoin/rust-bitcoin#3166, but the existing traits are really annoying to use. But this change is not essential to any of my other work so feel free to concept NACK it.

There are two changes here:

  • This drops the TranslatePk trait which had no business existing. It attempts to be generic over "things whose keys can be translated", but is missing impls (on keys themselves, for example) and anyway it is basically impossible to usefully be generic over this trait since it has an associated Output type which is unconstrained except by a comment saying "this must be Self<Q>. Really, the only purpose of this trait is to force users to write use miniscript::TranslatePk every time they want access to the translate_pk method.
  • It moves two of the generics in Translator from generics to associated types. This makes it far more ergonomic to implement and require the trait, since you no longer need to write 3 separate generics everywhere when two are implied. (Actually, all three are implied, including the source Pk type, but in practice users want to constrain this to match an existing key type that they've got. So I left it as a generic rather than an associated type.)

Copy link
Member Author

@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.

Successfully ran local tests on 269f44d.

sanket1729
sanket1729 previously approved these changes Aug 30, 2024
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.

ACK 269f44d.

Definitely makes the translate API easier to use.

@sanket1729
Copy link
Member

@apoelstra, the integration tests also need to be updated.

Copy link
Member Author

@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.

Successfully ran local tests on 380a0d3.

@apoelstra
Copy link
Member Author

Current CI failure is because of bad doctest links. My local CI should be catching this. Will investigate..

@apoelstra
Copy link
Member Author

Got it. I was forcing no-std on any feature list that didn't have std in it, including the default-only list. But I only run the doc-link check (and clippy and fmt and a couple other things) when the feature-list is exclusiely default (otherwise I'd be running these checks for every feature combination, which is pointless because none of these checks use the feature list).

Will confirm that my local checks fail here, and then fix the PR.

We actually just empty it and deprecate it in an attempt to minimize
breakage. People *implementing* the trait will break anyway because the
next commit will have me changing the Translator trait. But the typical
case is that somebody imports the trait, then calls .translate_pk on
some object, and we want that usecase to continue working with only
warnings.
This is an annoying breaking change for users of the Translator trait
but I think it greatly improves the ergonomics of using the trait.
Rather than having it be parameterized over 3 types, it is now
parameterized over just one (the "source pk type").

This matches how this trait is used in practice -- you typically have a
miniscript/policy/whatever with a keytype Pk, and you want to use a
translator from Pk to "whatever the translator maps to" with "whatever
error the translator yields". So the only type parameter you really need
to type is Pk; the others are irrelevant, and making the user name and
type them is annoying.

Since this eliminates the need to explicitly write out the error types
except when actually implementing the trait, this also changes a ton of
error types from () to Infallible, which is more efficient and correct.
@apoelstra apoelstra force-pushed the 2024-08--translate-pk branch from 380a0d3 to 36b0659 Compare August 30, 2024 22:18
Copy link
Member Author

@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.

Successfully ran local tests on 36b0659.

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.

ACK 36b0659

@apoelstra apoelstra merged commit 1a3605d into rust-bitcoin:master Sep 1, 2024
@apoelstra apoelstra deleted the 2024-08--translate-pk branch September 1, 2024 13:29
heap-coder added a commit to heap-coder/rust-miniscript that referenced this pull request Sep 27, 2025
…d clean up `Translator` trait

36b065981a10c30f37548f5df792885d5397730f translator: make target pk and error associated types (Andrew Poelstra)
c330d0b37adb8e07dda75ab41cd57fa15728bbfc remove the TranslatePk trait (Andrew Poelstra)
6e1d6bbc9cb000f8473ab62017912f316740b1c8 descriptor: stop using TranslatePk in mod.rs (Andrew Poelstra)
b80296b1a7943be8f51f27673ac818e6c623ff60 descriptor: stop using TranslatePk in tr.rs (Andrew Poelstra)
f89cf257564fd4c0c0e41742efc62b7fbb739eb7 descriptor: stop using TranslatePk in sh.rs (Andrew Poelstra)
4654b98d9d5e90083d6cf01d718d1df7d3302b1e descriptor: stop using TranslatePk in segwitv0.rs (Andrew Poelstra)
9c91586d2668785cf601e13458260be49e3628e1 descriptor: stop using TranslatePk in bare.rs (Andrew Poelstra)
e73a141dd69e7a409419ee78d61023399ea71c04 miniscript: stop using TranslatePk (Andrew Poelstra)

Pull request description:

  This may be too much of a breaking change and an instance of rust-bitcoin/rust-bitcoin#3166, but the existing traits are *really* annoying to use. But this change is not essential to any of my other work so feel free to concept NACK it.

  There are two changes here:

  * This drops the `TranslatePk` trait which had no business existing. It attempts to be generic over "things whose keys can be translated", but is missing impls (on keys themselves, for example) and anyway it is basically impossible to usefully be generic over this trait since it has an associated `Output` type which is unconstrained except by a comment saying "this must be `Self<Q>`. Really, the only purpose of this trait is to force users to write `use miniscript::TranslatePk` every time they want access to the `translate_pk` method.
  * It moves two of the generics in `Translator` from generics to associated types. This makes it far more ergonomic to implement and require the trait, since you no longer need to write 3 separate generics everywhere when two are implied. (Actually, all three are implied, including the source `Pk` type, but in practice users want to constrain this to match an existing key type that they've got. So I left it as a generic rather than an associated type.)

ACKs for top commit:
  sanket1729:
    ACK 36b065981a10c30f37548f5df792885d5397730f

Tree-SHA512: 0f7989925af2f9857146eb00e41a81ec1bbaf4c94b0003230835d3979ccb7913d8c958f1293f501e36ddcbaf81e8a88ada261371d9b79c4c85fec3ee195ffc07
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.

2 participants