Skip to content

Commit

Permalink
Cleanup type collection
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Apr 30, 2024
1 parent a542d55 commit 029e296
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/type_collection.rs
@@ -1,4 +1,7 @@
use std::collections::HashMap;
use std::{
borrow::{Borrow, Cow},

Check warning on line 2 in src/type_collection.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `Cow`

warning: unused import: `Cow` --> src/type_collection.rs:2:22 | 2 | borrow::{Borrow, Cow}, | ^^^ | = note: `#[warn(unused_imports)]` on by default
collections::HashMap,
};

use crate::{NamedDataType, NamedType, SpectaID, TypeMap};

Expand All @@ -18,8 +21,8 @@ impl Default for TypeCollection {

impl TypeCollection {
/// Join another type collection into this one.
pub fn join(&mut self, collection: TypeCollection) -> &mut Self {
self.types.extend(collection.types);
pub fn extend(&mut self, collection: impl Borrow<TypeCollection>) -> &mut Self {
self.types.extend(collection.borrow().types.iter());
self
}

Expand All @@ -31,7 +34,7 @@ impl TypeCollection {
}

/// Export all the types in the collection.
pub fn export(&mut self, mut type_map: &mut crate::TypeMap) {
pub fn export(&self, mut type_map: &mut crate::TypeMap) {
for (sid, export) in self.types.iter() {
let dt = export(&mut type_map);

Check warning on line 39 in src/type_collection.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/type_collection.rs:39:29 | 39 | let dt = export(&mut type_map); | ^^^^^^^^^^^^^ help: change this to: `type_map` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` implied by `#[warn(clippy::all)]`
type_map.insert(*sid, dt);
Expand Down
10 changes: 8 additions & 2 deletions tests/type_collection.rs
Expand Up @@ -35,10 +35,16 @@ fn type_collection_merge() {
let mut type_map = TypeMap::default();
TypeCollection::default()
.register::<D>()
.join(a)
.join(b)
.extend(a)
.extend(b)
.export(&mut type_map);
assert_eq!(type_map.len(), 4);

// Check it compile with any valid arg
TypeCollection::default()
.extend(&TypeCollection::default())
.extend(&mut TypeCollection::default())
.extend(TypeCollection::default());
}

#[test]
Expand Down

0 comments on commit 029e296

Please sign in to comment.