Use Display
trait to auto-populate the toString method and description
fields
#551
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is one of my favourite little PRs in a long time.
We cannot write an explicit toString() method on our objects because the name clashes with the
toString()
method on the JVM, and alternatively we cannot add adescription
field on objects because it clashes with the field in Swift. This means that for objects that have a requirement for this toString() method, we actually add a method calledasString()
. It works, but it's uglier than it needs to be and feels a little odd (why is the toString() not implemented on this type?, a developer might ask).But as of the 0.26 release of uniffi, if you define a Display trait on a type, it will automatically use that for the toString() method in Kotlin and the
description
field in Swift! This means of course that we can remove the odd-lookingasString()
(which achieves the same thing), but also that this is can be derived automatically for us by Rust! Here's how it looks in the UDL:Old printout in Kotlin:
New printout:
Note that the
toString()
method is also applied automatically in many places, removing the need to actually write it out explicitly.In Swift it's just as good. The difference is that you would have gotten an error if you tried to use the
description
field before:You now get:
The structs currently affected (and made better by this change) are:
Address
DescriptorSecretKey
DescriptorPublicKey
Mnemonic
Descriptor
But I suspect there might be many more that could use a clean
toString()
implementation which we did not have a clean way to provide.Changelog notice
TODO
Checklists
All Submissions:
cargo fmt
andcargo clippy
before committingNew Features: