Skip to content

Commit

Permalink
Merge pull request #40 from lun3x/remove_clone
Browse files Browse the repository at this point in the history
Remove clone requirement
  • Loading branch information
lun3x authored Aug 30, 2023
2 parents 8694978 + f24ff54 commit 994ab48
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 16 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ Current implementation supports:

This crate provides a derive macro `MultiIndexMap`, which when applied to the struct representing an element will generate a map to store and access these elements.
Annotations are used to specify which fields to index. Currently `hashed_unique`, `hashed_non_unique`, `ordered_unique`, and `ordered_non_unique` are supported.
The element must implement `Clone`.
The types of all indexed fields must implement `Clone`.
If the MultiIndexMap needs to be cloned, `Clone` must be implemented manually, see `examples/main.rs` for an example of how to do this.

## Example

```rust
use multi_index_map::MultiIndexMap;

#[derive(MultiIndexMap, Clone, Debug)]
#[derive(MultiIndexMap, Debug)]
struct Order {
#[multi_index(hashed_unique)]
order_id: u32,
Expand Down
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 0.8.0 (2023-08-30)
==========================

- Remove `Clone` requirement on elements, now only the indexed fields must implement Clone. This should be helpful when storing non-Clonable types in un-indexed fields.
- If the MultiIndexMap does need to be Cloned, this must be implemented manually, however this should be fairly simple to do next to where the element is defined. See `examples/main.rs`.

Version 0.7.1 (2023-08-30)
==========================

Expand Down
4 changes: 2 additions & 2 deletions multi_index_map/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multi_index_map"
version = "0.7.1"
version = "0.8.0"
edition = "2021"
authors = ["Louis Wyborn <louiswyborn@gmail.com>"]
rust-version = "1.62"
Expand All @@ -14,7 +14,7 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
multi_index_map_derive = { version = "0.7.1", path = "../multi_index_map_derive" }
multi_index_map_derive = { version = "0.8.0", path = "../multi_index_map_derive" }

# Used as the backing store of all the elements.
slab = { version = "0.4" }
Expand Down
12 changes: 12 additions & 0 deletions multi_index_map/benches/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ pub struct TestElementWithOnlyIndexedFields {
field_ordered_non_unique: u32,
}

impl Clone for MultiIndexTestElementWithOnlyIndexedFieldsMap {
fn clone(&self) -> Self {
Self {
_store: self._store.clone(),
_field_hashed_unique_index: self._field_hashed_unique_index.clone(),
_field_hashed_non_unique_index: self._field_hashed_non_unique_index.clone(),
_field_ordered_unique_index: self._field_ordered_unique_index.clone(),
_field_ordered_non_unique_index: self._field_ordered_non_unique_index.clone(),
}
}
}

const BENCH_SIZES: &[u32] = &[100u32, 1000u32, 10000u32, 100000u32];

fn insert_benchmark(c: &mut Criterion) {
Expand Down
12 changes: 12 additions & 0 deletions multi_index_map/examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ mod inner {
pub(crate) trader_name: String,
pub(crate) note: String,
}

// Manually implement Clone, this can be auto generated correctly by rust-analyzer
impl Clone for MultiIndexOrderMap {
fn clone(&self) -> Self {
Self {
_store: self._store.clone(),
_order_id_index: self._order_id_index.clone(),
_timestamp_index: self._timestamp_index.clone(),
_trader_name_index: self._trader_name_index.clone(),
}
}
}
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/capacity_manipulations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use multi_index_map::MultiIndexMap;

#[derive(MultiIndexMap, Clone, Debug)]
#[derive(MultiIndexMap, Debug)]
struct TestElement {
#[multi_index(hashed_unique)]
field1: i32,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/get_and_modify_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use multi_index_map::MultiIndexMap;
#[derive(Hash, PartialEq, Eq, Clone)]
struct TestNonPrimitiveType(u64);

#[derive(MultiIndexMap, Clone)]
#[derive(MultiIndexMap)]
struct TestElement {
#[multi_index(hashed_non_unique)]
field1: usize,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/hashed_non_unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use multi_index_map::MultiIndexMap;
#[derive(Hash, PartialEq, Eq, Clone, Debug)]
struct TestNonPrimitiveType(u64);

#[derive(MultiIndexMap, Clone, Debug)]
#[derive(MultiIndexMap, Debug)]
struct TestElement {
#[multi_index(hashed_non_unique)]
field1: TestNonPrimitiveType,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/hashed_unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use multi_index_map::MultiIndexMap;
#[derive(Hash, PartialEq, Eq, Clone)]
struct TestNonPrimitiveType(u64);

#[derive(MultiIndexMap, Clone)]
#[derive(MultiIndexMap)]
struct TestElement {
#[multi_index(hashed_unique)]
field1: TestNonPrimitiveType,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/iter_after_modify.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use multi_index_map::MultiIndexMap;

#[derive(MultiIndexMap, Debug, Clone)]
#[derive(MultiIndexMap, Debug)]
pub(crate) struct Order {
#[multi_index(hashed_unique)]
pub(crate) order_id: u32,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/mixed_non_unique.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use multi_index_map::MultiIndexMap;

#[derive(MultiIndexMap, Clone)]
#[derive(MultiIndexMap)]
struct MultipleOrderedNonUniqueStruct {
#[multi_index(ordered_non_unique)]
field1: u32,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/ordered_non_unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use multi_index_map::MultiIndexMap;
#[derive(Hash, PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
struct TestNonPrimitiveType(u64);

#[derive(MultiIndexMap, Clone, Debug)]
#[derive(MultiIndexMap, Debug)]
struct TestElement {
#[multi_index(ordered_non_unique)]
field1: TestNonPrimitiveType,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/ordered_unique.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use multi_index_map::MultiIndexMap;
#[derive(Hash, PartialEq, Eq, Clone, PartialOrd, Ord)]
struct TestNonPrimitiveType(u64);

#[derive(MultiIndexMap, Clone)]
#[derive(MultiIndexMap)]
struct TestElement {
#[multi_index(ordered_unique)]
field1: TestNonPrimitiveType,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/reverse_iter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use multi_index_map::MultiIndexMap;
#[derive(MultiIndexMap, Clone, PartialEq, Debug)]
#[derive(MultiIndexMap, PartialEq, Debug)]
struct TestElement {
#[multi_index(ordered_non_unique)]
field1: usize,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map/tests/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use multi_index_map::MultiIndexMap;
#[derive(Hash, PartialEq, Eq, Clone)]
struct TestNonPrimitiveType(u64);

#[derive(MultiIndexMap, Clone, PartialEq, Debug)]
#[derive(MultiIndexMap, PartialEq, Debug)]
struct TestElement {
#[multi_index(hashed_non_unique)]
field1: i32,
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multi_index_map_derive"
version = "0.7.1"
version = "0.8.0"
edition = "2021"
authors = ["Louis Wyborn <louiswyborn@gmail.com>"]
rust-version = "1.62"
Expand Down
2 changes: 1 addition & 1 deletion multi_index_map_derive/src/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ pub(crate) fn generate_expanded(
lookup_table_fields_reserve: impl Iterator<Item = proc_macro2::TokenStream>,
) -> proc_macro2::TokenStream {
quote! {
#[derive(Default, Clone)]
#[derive(Default)]
#element_vis struct #map_name {
_store: ::multi_index_map::slab::Slab<#element_name>,
#(#lookup_table_fields)*
Expand Down

0 comments on commit 994ab48

Please sign in to comment.