Skip to content

Commit

Permalink
Merge pull request #9 from Sajjon/is_id_vec
Browse files Browse the repository at this point in the history
Add `IsIdentifiableVecOfVia` trait and `newtype_identified_vec` macro
  • Loading branch information
Sajjon authored Dec 16, 2023
2 parents 7caf3e2 + ce86fe2 commit 64cf8a4
Show file tree
Hide file tree
Showing 21 changed files with 1,324 additions and 445 deletions.
40 changes: 29 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 3 additions & 24 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
[package]
name = "identified_vec"
version = "0.1.4"
edition = "2021"
authors = ["Alexander Cyon <[email protected]>"]
description = "Like HashSet but retaining INSERTION order and without `Hash` requirement on the Element type."
license = "MIT"
readme = "README.md"
repository = "https://github.com/Sajjon/identified_vec"
keywords = ["identifiable", "vec", "orderset", "set", "hashset"]
categories = ["data-structures"]
[workspace]

[features]
serde = ["dep:serde"]
id_prim = []
resolver = "2"

[dependencies]
serde = { version = "1.0.193", optional = true }
thiserror = "1.0.50"

[dev-dependencies]
identified_vec = { path = ".", features = ["id_prim", "serde"] }
serde = "1.0.193"
serde_json = "1.0.108"
rand = "0.8.5"
maplit = "1.0.2"
members = ["identified_vec", "identified_vec_macros", "tests"]
19 changes: 1 addition & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You can create an identified vec with any element type that implements the `Iden

```rust
extern crate identified_vec;
use identified_vec::{IdentifiedVec, Identifiable, IdentifiedVecOf};
use identified_vec::{IsIdentifiableVec, IdentifiedVec, Identifiable, IdentifiedVecOf};
use std::cell::RefCell;

#[derive(Eq, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -133,23 +133,6 @@ assert_eq!(
);
```

## `get_mut`

```rust
// or mutate with `get_mut(id)`
*users.get_mut(&"u_1337").unwrap().name.get_mut() = "Yoda";
assert_eq!(
users.elements(),
[
User::new("u_42", "Tom Mervolo Dolder"),
User::new("u_1337", "Yoda"),
User::new("u_237", "Marie Curie"),
]
.iter()
.collect::<Vec<&User>>()
);
```

Or you can provide a closure that describes an element's identity:

```rust
Expand Down
184 changes: 184 additions & 0 deletions identified_vec/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions identified_vec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "identified_vec"
version = "0.1.5"
edition = "2021"
authors = ["Alexander Cyon <[email protected]>"]
description = "Like HashSet but retaining INSERTION order and without `Hash` requirement on the Element type."
license = "MIT"
readme = "README.md"
repository = "https://github.com/Sajjon/identified_vec"
keywords = ["identifiable", "vec", "orderset", "set", "hashset"]
categories = ["data-structures"]

[features]
default = ["id_prim"]
serde = ["dep:serde"]
id_prim = []

[dependencies]
serde = { version = "1.0.193", optional = true }
thiserror = "1.0.50"
10 changes: 10 additions & 0 deletions identified_vec/src/conflict_resolution_choice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// Representation of a choice in a conflict resolution
/// where two elements with the same Self::ID exists, if `ChooseFirst`,
/// is specified the first/current/existing value will be used, but
/// if `ChooseLast` is specified then the new/last will be replace
/// the first/current/existing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ConflictResolutionChoice {
ChooseFirst,
ChooseLast,
}
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions identified_vec/src/identified_vec_into_iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::is_identifiable_vec::IsIdentifiableVec;
use crate::vec::IdentifiedVec;
use std::fmt::Debug;
use std::hash::Hash;

/// An owning iterator over the items of an `IdentifiedVec`.
pub struct IdentifiedVecIntoIterator<I, E>
where
I: Eq + Hash + Clone + Debug,
{
identified_vec: IdentifiedVec<I, E>,
}

impl<I, E> IdentifiedVecIntoIterator<I, E>
where
I: Eq + Hash + Clone + Debug,
{
pub fn new(identified_vec: IdentifiedVec<I, E>) -> Self {
Self { identified_vec }
}
}

impl<I, E> Iterator for IdentifiedVecIntoIterator<I, E>
where
I: Eq + Hash + Clone + Debug,
{
type Item = E;

fn next(&mut self) -> Option<Self::Item> {
if self.identified_vec.len() == 0 {
return None;
}
let result = self.identified_vec.remove_at(0);
Some(result)
}
}
Loading

0 comments on commit 64cf8a4

Please sign in to comment.