-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Conversation
Part of #8719
IMHO we have now reached the point where this should be abstracted. IMHO I don't see any point in copying this for every type in the std library (I know that we don't need every type). However this is mainly code copying. Which should be doable with some generic container or a macro that generates the stuff. |
I'm not sure how well it can be automated. The desired abstraction involves
Knowing which types are appropriate, and which methods can increase the size, seems to require human intervention. That said, once this is in, we'll have bounded variants for lists, sets, and maps. Those should suffice for all reasonable storage cases. I'm not anticipating adding more PRs like this soon. |
|
||
/// Remove an item from the set, returning whether it was previously in the set. | ||
/// | ||
/// The item may be any borrowed form of the set's item type, but the ordering on the borrowed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL 👍
} | ||
} | ||
|
||
impl<T, S> From<BoundedBTreeSet<T, S>> for BTreeSet<T> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might miss this in BoundedVec
and BoundedBTreeMap
, good one!
I don't have as strong opinion about BtreeMap and Set. On one hand they are types that we support in storage and we are a library, on the other hand we don't really use them (for good reasons) and we shouldn't overkill it. Either way I am happy with this PR. About your comment on abstraction: I think what we do need is a struct AttributeVec<T, Attr: Attribute>(Vec<T>);
/// An attribute to a container like a Vec
trait Attribute {
fn pre_insert();
fn post_insert();
fn pre_delete();
fn post_delete();
// and some other hooks/utilities that allow an attribute to control how a container mutates.
}
/// An attribute that ensures a container's length never goes beyond a limit.
struct Bounded;
impl Attribute for Bounded {
fn pre_insert() { /* ensure bound */}
}
/// An attribute that ensures a container is always sorted.
struct Sorted;
impl Attribute for Sorted {
fn post_insert() { /* do sort! */}
fn post_delete() { /* do sort! */}
}
// and we can have other attributes, and attributes can be combined by aggregating them into a tuple.
#[impl_trait_for_tuples(18)]
if Attribute for Tuple { /* ... */ }
///
type BoundedVec<T> = AttributeVec<T, Bounded>;
type SortedVec<T> = AttributeVec<T, Sorted>;
type BoundedSortedVec<T> = AttributeVec<T, (Bounded, Sorted)>; But to be honest there are a lot of empty pieces in my brain dump above, and I am worried that this would add so much complexity that we would regret it and just go back to the simpler way of copy-pasta. |
@kianenigma No commands could be detected from your comment. |
Co-authored-by: Kian Paimani <[email protected]>
Hello? |
do you love me cla bot? |
bot merge |
Trying merge. |
* Add `BoundedBTreeSet` Part of paritytech#8719 * fix copy-pasta errors Co-authored-by: Kian Paimani <[email protected]> Co-authored-by: Kian Paimani <[email protected]>
Part of #8719