linera-protocol icon indicating copy to clipboard operation
linera-protocol copied to clipboard

Create a `WitLoadStore` trait alias

Open jvff opened this issue 1 year ago • 5 comments

Motivation

It's very common to implement all three WitType, WitLoad and WitStore traits for types. Unfortunately, that leads to a verbose #[derive(WitType, WitLoad, WitStore)] which, together with other derived traits can make the attribute occupy a lot of visual space.

Proposed Solution

  • Create a trait WitLoadStore : WitLoad + WitStore {} trait alias in linera-witty beside the other traits.
  • Add a blanket implementation impl<T> WitLoadStore for T where T: WitLoad + WitStore {}.
  • Create a new derive macro for the WitLoadStore trait in linera-witty-macros that generates the WitType, WitLoad and WitStore trait implementations.

jvff avatar Apr 09 '24 00:04 jvff

Mmm @afck suggested trait Witty: WitType + WitLoad + WitStore. In general, trait aliases tend to denormalize the code (i.e. introduce degrees of freedom).

ma2bd avatar Apr 09 '24 00:04 ma2bd

We can discuss the name of the trait. I think Witty is too generic, and may cause confusion because it may seem it includes other traits present in the crate (like the WitInterface trait that allows generating WIT interface snippets).

jvff avatar Apr 09 '24 01:04 jvff

I'm not sure I understand the argument about denormalizing the code. I thought that we didn't introduce any more degrees of freedom because we constrained on both sides, with the trait dependencies on one (WitLoadStore: WitLoad + WitStore and WitType indirectly) and the blanket implementation on the other. So I think it's not possible to implement WitLoadStore manually, because any attempt would clash with the blanket implementation.

jvff avatar Apr 09 '24 01:04 jvff

re: degrees of freedom: Some developers will use the aliases and others won't.

Also, strictly speaking, these are not trait aliases.

ma2bd avatar Apr 09 '24 02:04 ma2bd

I'm fine with the name WitLoadStore, too!

If you prefer not to have the new trait, we could also add only the derive macro?

afck avatar Apr 09 '24 08:04 afck