Skip to content

Commit

Permalink
merge two crates together, cleanup structure and re-export more.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Dec 17, 2023
1 parent 44f6d8b commit e7c782f
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 78 deletions.
2 changes: 1 addition & 1 deletion 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::{IsIdentifiableVec, IdentifiedVec, Identifiable, IdentifiedVecOf};
use identified_vec::{IsIdentifiedVec, IdentifiedVec, Identifiable, IdentifiedVecOf};
use std::cell::RefCell;

#[derive(Eq, PartialEq, Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::is_identifiable_vec::IsIdentifiableVec;
use crate::vec::IdentifiedVec;
use std::fmt::Debug;
use std::hash::Hash;

use crate::{IdentifiedVec, IsIdentifiedVec};

/// An owning iterator over the items of an `IdentifiedVec`.
pub struct IdentifiedVecIntoIterator<I, E>
where
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::is_identifiable_vec::IsIdentifiableVec;
use crate::vec::IdentifiedVec;
use std::fmt::Debug;
use std::hash::Hash;

use crate::{IdentifiedVec, IsIdentifiedVec};

/// An iterator over the items of an `IdentifiedVec`.
pub struct IdentifiedVecIterator<'a, I, E>
where
Expand Down
2 changes: 2 additions & 0 deletions src/iterators/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod identified_vec_into_iterator;
pub mod identified_vec_iterator;
33 changes: 6 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
//!
//! ```
//! extern crate identified_vec;
//! use identified_vec::{IsIdentifiableVec, IsIdentifiableVecOf, IdentifiedVec, IdentifiedVecOf, Identifiable};
//! use identified_vec::identified_vec::*;
//! use identified_vec::identified_vec_of::*;
//! use std::cell::RefCell;
//!
//! #[derive(Eq, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -110,31 +111,19 @@
//!
//! ```
//! extern crate identified_vec;
//! use identified_vec::{IsIdentifiableVec, IdentifiedVec, IdentifiedVecOf, Identifiable};
//!
//! use identified_vec::identified_vec::*;
//! use identified_vec::identified_vec_of::*;
//! // closure which plucks out an ID from an element.
//! let numbers = IdentifiedVec::<u32, u32>::new_identifying_element(|e| *e);
//! ```
mod conflict_resolution_choice;
mod errors;
mod identifiable_trait;
mod identified_vec_into_iterator;
mod identified_vec_iterator;
mod is_identifiable_vec;
mod is_identifiable_vec_of;
mod is_identified_vec_via;
mod newtype_identified_vec;
mod primitives_identifiable;
mod iterators;
mod vec;
mod vec_of;

pub mod identified_vec {
//! A collection of unique identifiable elements which retains **insertion** order.
pub use crate::conflict_resolution_choice::*;
pub use crate::identified_vec_into_iterator::*;
pub use crate::identified_vec_iterator::*;
pub use crate::is_identifiable_vec::*;
pub use crate::iterators::*;
pub use crate::vec::*;
}

Expand All @@ -144,17 +133,7 @@ pub mod identified_vec_of {
//! `IdentifiedVec<SomeUserID, User>` but also allows you to
//! skip the `id_of_element: fn(&Element) -> ID` closure when
//! initializing a new identified vec.
pub use crate::identifiable_trait::*;
pub use crate::is_identifiable_vec_of::*;
pub use crate::is_identified_vec_via::*;
pub use crate::vec_of::*;

#[cfg(feature = "id_prim")]
pub use crate::primitives_identifiable::*;

pub use crate::errors::*;

pub use crate::newtype_identified_vec::*;
}

pub use crate::identified_vec::*;
Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions src/vec.rs → src/vec/identified_vec.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::conflict_resolution_choice::ConflictResolutionChoice;
use crate::errors::Error;
use crate::identified_vec_into_iterator::IdentifiedVecIntoIterator;
use crate::identified_vec_iterator::IdentifiedVecIterator;
use super::ConflictResolutionChoice;
use crate::iterators::identified_vec_into_iterator::IdentifiedVecIntoIterator;
use crate::iterators::identified_vec_iterator::IdentifiedVecIterator;
use crate::Error;
use std::collections::HashMap;
use std::fmt::{Debug, Display};
use std::hash::{Hash, Hasher};

use crate::is_identifiable_vec::IsIdentifiableVec;
use super::is_identified_vec::IsIdentifiedVec;

/// An ordered collection of identifiable elements.
///
Expand All @@ -26,7 +26,7 @@ use crate::is_identifiable_vec::IsIdentifiableVec;
///
/// ```
/// extern crate identified_vec;
/// use identified_vec::{IdentifiedVec, Identifiable, IdentifiedVecOf, IsIdentifiableVec, IsIdentifiableVecOf};
/// use identified_vec::{IdentifiedVec, Identifiable, IdentifiedVecOf, IsIdentifiedVec, IsIdentifiedVecOf};
/// use std::cell::RefCell;
///
/// #[derive(Eq, PartialEq, Clone, Debug)]
Expand Down Expand Up @@ -124,7 +124,7 @@ use crate::is_identifiable_vec::IsIdentifiableVec;
///
/// ```
/// /// extern crate identified_vec;
/// use identified_vec::{IdentifiedVec, Identifiable, IdentifiedVecOf, IsIdentifiableVec, IsIdentifiableVecOf};
/// use identified_vec::{IdentifiedVec, Identifiable, IdentifiedVecOf, IsIdentifiedVec, IsIdentifiedVecOf};
///
/// let numbers = IdentifiedVec::<u32, u32>::new_identifying_element(|e| *e);
/// ```
Expand Down Expand Up @@ -172,7 +172,7 @@ where
pub(crate) _id_of_element: fn(&E) -> I,
}

impl<I, E> IsIdentifiableVec<E, I> for IdentifiedVec<I, E>
impl<I, E> IsIdentifiedVec<E, I> for IdentifiedVec<I, E>
where
I: Eq + Hash + Clone + Debug,
{
Expand Down Expand Up @@ -328,7 +328,7 @@ where
///
/// ```
/// extern crate identified_vec;
/// use identified_vec::{IsIdentifiableVec, IsIdentifiableVecOf, IdentifiedVec, Identifiable, IdentifiedVecOf};
/// use identified_vec::{IsIdentifiedVec, IsIdentifiedVecOf, IdentifiedVec, Identifiable, IdentifiedVecOf};
///
/// #[derive(Eq, PartialEq, Clone, Debug, Hash)]
/// struct User {
Expand Down Expand Up @@ -550,7 +550,7 @@ where
///
/// ```
/// extern crate identified_vec;
/// use identified_vec::{IsIdentifiableVec, IsIdentifiableVecOf, IdentifiedVec, Identifiable, IdentifiedVecOf};
/// use identified_vec::{IsIdentifiedVec, IsIdentifiedVecOf, IdentifiedVec, Identifiable, IdentifiedVecOf};
///
/// #[derive(Eq, PartialEq, Clone, Debug, Hash)]
/// struct User {
Expand Down
11 changes: 5 additions & 6 deletions src/is_identifiable_vec.rs → src/vec/is_identified_vec.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::conflict_resolution_choice::ConflictResolutionChoice;
use crate::Error;
use crate::IdentifiedVecIterator;
use crate::iterators::identified_vec_iterator::IdentifiedVecIterator;
use crate::{ConflictResolutionChoice, Error};
use std::fmt::Debug;
use std::hash::Hash;

pub trait IsIdentifiableVec<Element, ID>: Sized
pub trait IsIdentifiedVec<Element, ID>: Sized
where
ID: Eq + Hash + Clone + Debug,
{
Expand Down Expand Up @@ -57,7 +56,7 @@ where
///
/// ```
/// extern crate identified_vec;
/// use identified_vec::{IsIdentifiableVec, IsIdentifiableVecOf, IdentifiedVec, Identifiable, IdentifiedVecOf};
/// use identified_vec::{IsIdentifiedVec, IsIdentifiedVecOf, IdentifiedVec, Identifiable, IdentifiedVecOf};
///
/// #[derive(Eq, PartialEq, Clone, Debug, Hash)]
/// struct User {
Expand Down Expand Up @@ -188,7 +187,7 @@ where
///
/// ```
/// extern crate identified_vec;
/// use identified_vec::{IsIdentifiableVec, IsIdentifiableVecOf, IdentifiedVec, Identifiable, IdentifiedVecOf};
/// use identified_vec::{IsIdentifiedVec, IsIdentifiedVecOf, IdentifiedVec, Identifiable, IdentifiedVecOf};
///
/// #[derive(Eq, PartialEq, Clone, Debug, Hash)]
/// struct User {
Expand Down
7 changes: 7 additions & 0 deletions src/vec/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod conflict_resolution_choice;
mod identified_vec;
mod is_identified_vec;

pub use conflict_resolution_choice::*;
pub use identified_vec::*;
pub use is_identified_vec::*;
File renamed without changes.
File renamed without changes.
12 changes: 5 additions & 7 deletions src/vec_of.rs → src/vec_of/identified_vec_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ use std::collections::HashMap;
use std::fmt::Debug;

#[cfg(feature = "serde")]
use crate::errors::IdentifiedVecOfSerdeFailure;
use crate::IsIdentifiableVecOf;
use crate::{
conflict_resolution_choice::ConflictResolutionChoice, is_identifiable_vec::IsIdentifiableVec,
};
use crate::{identified_vec_of::Identifiable, vec::IdentifiedVec};
use super::errors::IdentifiedVecOfSerdeFailure;
use crate::{ConflictResolutionChoice, IdentifiedVec, IsIdentifiedVec, IsIdentifiedVecOf};

#[cfg(feature = "serde")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use super::identifiable_trait::Identifiable;

///////////////////////
/// IdentifiedVecOf ///
///////////////////////
Expand All @@ -28,7 +26,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
/// `Element` implements serde serialization/deserialization of course.
pub type IdentifiedVecOf<Element> = IdentifiedVec<<Element as Identifiable>::ID, Element>;

impl<Element> IsIdentifiableVecOf<Element> for IdentifiedVecOf<Element>
impl<Element> IsIdentifiedVecOf<Element> for IdentifiedVecOf<Element>
where
Element: Identifiable,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::identifiable_trait::Identifiable;
use crate::is_identifiable_vec::IsIdentifiableVec;
use crate::ConflictResolutionChoice;
use crate::{ConflictResolutionChoice, Identifiable, IsIdentifiedVec};

pub trait IsIdentifiableVecOf<Element: Identifiable>:
IsIdentifiableVec<Element, Element::ID>
{
pub trait IsIdentifiedVecOf<Element: Identifiable>: IsIdentifiedVec<Element, Element::ID> {
/// Constructs a new, empty `IdentifiedVec<ID, Element>`, using `id()` on `Element`
/// as id function.
fn new() -> Self;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::conflict_resolution_choice::ConflictResolutionChoice;
use crate::vec::ItemsCloned;
use crate::{
Error, Identifiable, IdentifiedVecIterator, IdentifiedVecOf, IsIdentifiableVec,
IsIdentifiableVecOf,
identified_vec_iterator::IdentifiedVecIterator, ConflictResolutionChoice, Error,
IsIdentifiedVec, IsIdentifiedVecOf, ItemsCloned,
};

use super::{identifiable_trait::Identifiable, identified_vec_of::IdentifiedVecOf};

/// https://stackoverflow.com/a/66537661/1311272
pub trait ViaMarker {}

pub trait IsIdentifiableVecOfVia<Element>:
IsIdentifiableVecOf<Element> + IntoIterator<Item = Element> + ViaMarker
IsIdentifiedVecOf<Element> + IntoIterator<Item = Element> + ViaMarker
where
Element: Identifiable,
{
Expand All @@ -18,7 +18,7 @@ where
fn via(&self) -> &IdentifiedVecOf<Element>;
}

impl<Element, U> IsIdentifiableVecOf<Element> for U
impl<Element, U> IsIdentifiedVecOf<Element> for U
where
U: ViaMarker,
Element: Identifiable,
Expand Down Expand Up @@ -49,7 +49,7 @@ where
}
}

impl<Element, U> IsIdentifiableVec<Element, Element::ID> for U
impl<Element, U> IsIdentifiedVec<Element, Element::ID> for U
where
U: ViaMarker,
Element: Identifiable,
Expand Down
17 changes: 17 additions & 0 deletions src/vec_of/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod errors;
mod identifiable_trait;
mod identified_vec_of;
mod is_identified_vec_of;
mod is_identified_vec_of_via;
mod newtype_identified_vec_of;
mod primitives_identifiable;

pub use errors::*;
pub use identifiable_trait::*;
pub use identified_vec_of::*;
pub use is_identified_vec_of::*;
pub use is_identified_vec_of_via::*;
pub use newtype_identified_vec_of::*;

#[cfg(feature = "id_prim")]
pub use primitives_identifiable::*;
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//! a newtype wrapping an `IdentifiedVecOf` of the item type
//! you pass in, but it gets super powers! It implements the
//! traits `IsIdentifiableVecOfVia`, which implements the trait
//! `IsIdentifiableVecOf`, meaning that the declared newtype,
//! `IsIdentifiedVecOf`, meaning that the declared newtype,
//! gets all the methods and functions of a `IdentifiedVecOf`,
//! and if you use the `"serde"` feature, it is also
//! (de)serializable.
//!
//! You use it like so:
//! ```
//! extern crate identified_vec;
//! use identified_vec::{IsIdentifiableVecOfVia, ViaMarker, IsIdentifiableVec, IsIdentifiableVecOf, IdentifiedVec, IdentifiedVecOf, Identifiable, newtype_identified_vec};
//! use identified_vec::{IsIdentifiableVecOfVia, ViaMarker, IsIdentifiedVec, IsIdentifiedVecOf, IdentifiedVec, IdentifiedVecOf, Identifiable, newtype_identified_vec};
//!
//! newtype_identified_vec!(of: u32, named: Ints);;
//!
Expand All @@ -24,8 +24,8 @@ macro_rules! newtype_identified_vec {
#[derive(std::fmt::Debug, Clone, Eq, PartialEq)]
pub struct $struct_name(IdentifiedVecOf<$item_ty>);

impl ViaMarker for $struct_name {}
impl IsIdentifiableVecOfVia<$item_ty> for $struct_name {
impl identified_vec::ViaMarker for $struct_name {}
impl identified_vec::IsIdentifiableVecOfVia<$item_ty> for $struct_name {
fn via_mut(&mut self) -> &mut IdentifiedVecOf<$item_ty> {
&mut self.0
}
Expand All @@ -47,8 +47,10 @@ macro_rules! newtype_identified_vec {

impl IntoIterator for $struct_name {
type Item = $item_ty;
type IntoIter =
identified_vec::IdentifiedVecIntoIterator<<$item_ty as Identifiable>::ID, $item_ty>;
type IntoIter = identified_vec::identified_vec_into_iterator::IdentifiedVecIntoIterator<
<$item_ty as Identifiable>::ID,
$item_ty,
>;

fn into_iter(self) -> Self::IntoIter {
Self::IntoIter::new(self.0)
Expand Down Expand Up @@ -81,6 +83,7 @@ macro_rules! newtype_identified_vec {
deserializer: D,
) -> Result<$struct_name, D::Error> {
let id_vec_of = IdentifiedVecOf::<$item_ty>::deserialize(deserializer)?;
use identified_vec::IsIdentifiableVecOfVia;
return Ok(Self::from_identified_vec_of(id_vec_of));
}
}
Expand Down
File renamed without changes.
6 changes: 2 additions & 4 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ use std::{cell::RefCell, collections::HashSet, fmt::Debug, ops::Deref};

use identified_vec::{
newtype_identified_vec, ConflictResolutionChoice, Error, Identifiable, IdentifiedVec,
IdentifiedVecOf, IdentifiedVecOfSerdeFailure, IsIdentifiableVec, IsIdentifiableVecOf,
IsIdentifiableVecOfVia, ItemsCloned, ViaMarker,
IdentifiedVecOf, IdentifiedVecOfSerdeFailure, IsIdentifiedVec, IsIdentifiedVecOf, ItemsCloned,
};

#[cfg(any(test, feature = "serde"))]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize};

#[derive(Eq, PartialEq, Clone)]
#[cfg_attr(any(test, feature = "serde"), derive(Serialize, Deserialize))]
Expand Down

0 comments on commit e7c782f

Please sign in to comment.