Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Relax BoundedVec trait restrictions #8749

Merged
3 commits merged into from
May 7, 2021

Conversation

coriolinus
Copy link
Contributor

A normal Vec<T> can do many things without any particular trait
bounds on T. This PR relaxes the bounds on BoundedVec<T, S>
to give it similar capabilities.

coriolinus added 2 commits May 6, 2021 14:40
A normal `Vec<T>` can do many things without any particular trait
bounds on `T`. This commit relaxes the bounds on `BoundedVec<T, S>`
to give it similar capabilities.
@coriolinus coriolinus added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed. labels May 6, 2021
@coriolinus coriolinus self-assigned this May 6, 2021
#[derive(Encode, Decode, crate::DefaultNoBound, crate::CloneNoBound, crate::DebugNoBound)]
pub struct BoundedVec<T: BoundedVecValue, S: Get<u32>>(Vec<T>, PhantomData<S>);
#[derive(Encode, Decode)]
pub struct BoundedVec<T, S>(Vec<T>, PhantomData<S>);
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the benefit of relaxing S? I can't see why and how that should ever be anything other than Get<_>? is it just to avoid writing it in a few places? I'd argue that's not worth it given you now need to split the impl<T, S> BoundedVec<T, S> into two chunks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean, yes, it's so that we don't have to write it down in as many places. Agree that S is never going to be anything other than Get<_>, but there are quite a few impls which just don't care about S, so there's not much point in enforcing the restriction there.

I suspect, but cannot prove, that more-general generics are slightly faster to compile: the compiler doesn't need to do as much work proving that the bounds are satisfied.

Copy link
Contributor

Choose a reason for hiding this comment

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

In the meantime I looked a bit more in the std-library, and it indeed seems like the correct paradigm is to name as few generics as possible, so you are indeed aligned with the idiomatic way of writing this kind of stuff.

What I don't get and disagree with, is that it seems like this rule of thumb is stretched the extent that it even doesn't make sense anymore 😬

But anyway, thanks for the explanation. I am unfamiliar with some idioms.

Copy link
Member

Choose a reason for hiding this comment

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

The point is, never bound a generic to a trait in the declaration if that is not required by the declaration. There are some exceptions of this rule, but here it is completely fine to drop it.

Copy link
Contributor

@kianenigma kianenigma left a comment

Choose a reason for hiding this comment

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

LGTM

@shawntabrizi
Copy link
Member

bot merge

@ghost
Copy link

ghost commented May 7, 2021

Trying merge.

@ghost ghost merged commit de24234 into master May 7, 2021
@ghost ghost deleted the prgn-relax-bounded-vec-trait-restrictions branch May 7, 2021 20:04
@jakoblell jakoblell added D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited and removed D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed. labels May 31, 2021
nazar-pc pushed a commit to autonomys/substrate that referenced this pull request Aug 8, 2021
* requiring users to maintain an unchecked invariant is unsafe

* relax trait restrictions on BoundedVec<T, S>

A normal `Vec<T>` can do many things without any particular trait
bounds on `T`. This commit relaxes the bounds on `BoundedVec<T, S>`
to give it similar capabilities.
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants