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

Remove bounds on Config trait that aren't strictly necessary #389

Merged
merged 3 commits into from
Jan 13, 2022

Conversation

jsdw
Copy link
Collaborator

@jsdw jsdw commented Jan 12, 2022

We use types implementing the Config trait in Subxt, but only at compile-time; we never instantiate an instance of them. And yet, the Config trait has various bounds on it (Debug, Clone, Copy, Send, Sync etc).

This PR removes those bounds (well, mostly; 'static is still there for now and should be able to go away eventually, but that should never complicate anything in real use).

The reason I think that these bounds existed in the first place is that Rust's inbuilt "derive" mechanism is quite primitive (eg deriving Clone will derive something like impl <T: Clone> Clone for MyType<T>; the T also needs to be Clone). As a result, we ended up with a bunch of additional bounds on the Config type so that it could be passed around as a generic type T and would still satisfy these derives.

Removing the bounds on Config has pros and cons:

Pros:

  • More technically correct; Config is never instantiated, and so it shouldn't need any of these bounds.
  • Makes it a little easier for users to implement Config on their own types; they don't need to pointlessly derive anything.

Cons:

  • Introduces derivative crate to allow ignoring the T when deriving various traits on things that use Config. (we could manually derive the traits everywhere, but that would be needlessly verbose). This additional complexity will apply to anything else that takes a T: Config type.
  • Adds a PhantomDataSendSync<T> type (which is just PhantomData + Send + Sync) to ensure that the lack of Send + Sync on Config doesn't translate to a lack of Send + Sync on types using the config.

I think I lean towards being more "correct" at the cost of the additional complexity (and from a user facing POV, they won't see any of the added complexity, but will benefit a little from Config being easier to implement.

That said, I'm curious to hear people's opinions on the matter!

@jsdw jsdw marked this pull request as ready for review January 13, 2022 10:54
Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

I think this is a bit of a hack and it adds a fair amount of line noise; the derivative crate seems well maintained and is stable.
I do think this is better than before overall though and I think it's fair to err on the side of ergonomics here.

Code lgtm.

Comment on lines +35 to +37
// Note: the 'static bound isn't strictly required, but currently deriving TypeInfo
// automatically applies a 'static bound to all generic types (including this one),
// and so until that is resolved, we'll keep the (easy to satisfy) constraint here.
Copy link
Contributor

Choose a reason for hiding this comment

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

That isn't very likely to ever occur I think?

@dvdplm dvdplm merged commit 96fe1d6 into master Jan 13, 2022
@dvdplm dvdplm deleted the jsdw-remove-config-constraints branch January 13, 2022 16:07
0623forbidden pushed a commit to DEIPworld/substrate-subxt that referenced this pull request Feb 15, 2022
…ech#389)

* Use Derivative to skip bounds on T when they aren't necessary, and remove unnecessary bounds on Config

* loosen a couple more derive bounds

* Use PhantomDataSendSync to avoid accidentally removing Send+Sync bounds
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