Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@ pub trait RelationshipSourceCollection {
where
Self: 'a;

/// Creates a new empty instance.
fn new() -> Self;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this always be equivalent to Default::default()? If so, then I might vote for removing this method and putting Default as a supertrait.

... Ah, that wouldn't work for Entity.

It might help to have a comment explaining how this is different from Default::default(), although I'm not quite sure what to write there.


/// Returns an instance with the given pre-allocated entity `capacity`.
///
/// Some collections will ignore the provided `capacity` and return a default instance.
fn with_capacity(capacity: usize) -> Self;

/// Reserves capacity for at least `additional` more entities to be inserted.
///
/// Not all collections support this operation, in which case it is a no-op.
fn reserve(&mut self, additional: usize);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to offer default implementations of the optional methods? Even with_capacity could default to returning Self::new().

On the other hand, Entity is the only collection that doesn't implement them, so it wouldn't really save much.

Suggested change
fn reserve(&mut self, additional: usize);
fn reserve(&mut self, _additional: usize) {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using with_capacity in new or vice versa would require adding a Self: Sized bound. I decide to not bother and just implement the method manually everywhere.


/// Adds the given `entity` to the collection.
///
/// Returns whether the entity was added to the collection.
Expand All @@ -41,6 +51,11 @@ pub trait RelationshipSourceCollection {
/// Clears the collection.
fn clear(&mut self);

/// Attempts to save memory by shrinking the capacity to fit the current length.
///
/// This operation is a no-op for collections that do not support it.
fn shrink_to_fit(&mut self);

/// Returns true if the collection contains no entities.
#[inline]
fn is_empty(&self) -> bool {
Expand All @@ -62,6 +77,14 @@ pub trait RelationshipSourceCollection {
impl RelationshipSourceCollection for Vec<Entity> {
type SourceIter<'a> = core::iter::Copied<core::slice::Iter<'a, Entity>>;

fn new() -> Self {
Vec::new()
}

fn reserve(&mut self, additional: usize) {
Vec::reserve(self, additional);
}

fn with_capacity(capacity: usize) -> Self {
Vec::with_capacity(capacity)
}
Expand Down Expand Up @@ -94,6 +117,10 @@ impl RelationshipSourceCollection for Vec<Entity> {
self.clear();
}

fn shrink_to_fit(&mut self) {
Vec::shrink_to_fit(self);
}

fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
self.extend(entities);
}
Expand All @@ -102,6 +129,14 @@ impl RelationshipSourceCollection for Vec<Entity> {
impl RelationshipSourceCollection for EntityHashSet {
type SourceIter<'a> = core::iter::Copied<crate::entity::hash_set::Iter<'a>>;

fn new() -> Self {
EntityHashSet::new()
}

fn reserve(&mut self, additional: usize) {
self.0.reserve(additional);
}

fn with_capacity(capacity: usize) -> Self {
EntityHashSet::with_capacity(capacity)
}
Expand All @@ -128,6 +163,10 @@ impl RelationshipSourceCollection for EntityHashSet {
self.0.clear();
}

fn shrink_to_fit(&mut self) {
self.0.shrink_to_fit();
}

fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
self.extend(entities);
}
Expand All @@ -136,6 +175,14 @@ impl RelationshipSourceCollection for EntityHashSet {
impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
type SourceIter<'a> = core::iter::Copied<core::slice::Iter<'a, Entity>>;

fn new() -> Self {
SmallVec::new()
}

fn reserve(&mut self, additional: usize) {
SmallVec::reserve(self, additional);
}

fn with_capacity(capacity: usize) -> Self {
SmallVec::with_capacity(capacity)
}
Expand Down Expand Up @@ -168,6 +215,10 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
self.clear();
}

fn shrink_to_fit(&mut self) {
SmallVec::shrink_to_fit(self);
}

fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
self.extend(entities);
}
Expand All @@ -176,10 +227,16 @@ impl<const N: usize> RelationshipSourceCollection for SmallVec<[Entity; N]> {
impl RelationshipSourceCollection for Entity {
type SourceIter<'a> = core::iter::Once<Entity>;

fn with_capacity(_capacity: usize) -> Self {
fn new() -> Self {
Entity::PLACEHOLDER
}

fn reserve(&mut self, _: usize) {}

fn with_capacity(_capacity: usize) -> Self {
Self::new()
}

fn add(&mut self, entity: Entity) -> bool {
*self = entity;

Expand Down Expand Up @@ -211,6 +268,8 @@ impl RelationshipSourceCollection for Entity {
*self = Entity::PLACEHOLDER;
}

fn shrink_to_fit(&mut self) {}

fn extend_from_iter(&mut self, entities: impl IntoIterator<Item = Entity>) {
if let Some(entity) = entities.into_iter().last() {
*self = entity;
Expand Down