Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Dec 18, 2023
1 parent 99b7d22 commit e00c0d4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
mod iterators;
mod vec;
mod vec_of;
mod via;

pub mod identified_vec {
//! A collection of unique identifiable elements which retains **insertion** order.
Expand All @@ -136,5 +137,10 @@ pub mod identified_vec_of {
pub use crate::vec_of::*;
}

pub mod identified_vec_via {
pub use crate::via::*;
}

pub use crate::identified_vec::*;
pub use crate::identified_vec_of::*;
pub use crate::identified_vec_via::*;
4 changes: 0 additions & 4 deletions src/vec_of/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ 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::*;
68 changes: 68 additions & 0 deletions src/via/identified_vec_via.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::{
identified_vec_into_iterator::IdentifiedVecIntoIterator, Identifiable, IdentifiedVecOf,
IsIdentifiableVecOfVia, ViaMarker,
};

pub struct IdentifiedVecVia<Element: Identifiable>(IdentifiedVecOf<Element>);

impl<Element: Identifiable> ViaMarker for IdentifiedVecVia<Element> {}
impl<Element: Identifiable> IsIdentifiableVecOfVia<Element> for IdentifiedVecVia<Element> {
fn via_mut(&mut self) -> &mut IdentifiedVecOf<Element> {
&mut self.0
}

fn via(&self) -> &IdentifiedVecOf<Element> {
&self.0
}

fn from_identified_vec_of(identified_vec_of: IdentifiedVecOf<Element>) -> Self {
Self(identified_vec_of)
}
}

impl<Element: Identifiable + std::fmt::Display + std::fmt::Debug> std::fmt::Display
for IdentifiedVecVia<Element>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}

impl<Element: Identifiable> IntoIterator for IdentifiedVecVia<Element> {
type Item = Element;
type IntoIter = IdentifiedVecIntoIterator<<Element as Identifiable>::ID, Element>;

fn into_iter(self) -> Self::IntoIter {
Self::IntoIter::new(self.0)
}
}

#[cfg(any(test, feature = "serde"))]
impl<Element: Identifiable> serde::Serialize for IdentifiedVecVia<Element>
where
Element: serde::Serialize + Identifiable + std::fmt::Debug + Clone,
{
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as serde::Serializer>::Ok, <S as serde::Serializer>::Error>
where
S: serde::Serializer,
{
IdentifiedVecOf::serialize(&self.0, serializer)
}
}

#[cfg(any(test, feature = "serde"))]
impl<'de, Element> serde::Deserialize<'de> for IdentifiedVecVia<Element>
where
Element: serde::Deserialize<'de> + Identifiable + std::fmt::Debug + Clone,
{
#[cfg(not(tarpaulin_include))] // false negative
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<IdentifiedVecVia<Element>, D::Error> {
let id_vec_of = IdentifiedVecOf::<Element>::deserialize(deserializer)?;
return Ok(Self::from_identified_vec_of(id_vec_of));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::{
identified_vec_iterator::IdentifiedVecIterator, ConflictResolutionChoice, Error,
IsIdentifiedVec, IsIdentifiedVecOf, ItemsCloned,
identified_vec_iterator::IdentifiedVecIterator, ConflictResolutionChoice, Error, Identifiable,
IdentifiedVecOf, IsIdentifiedVec, IsIdentifiedVecOf, ItemsCloned,
};

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

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

Expand Down
7 changes: 7 additions & 0 deletions src/via/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod identified_vec_via;
mod is_identified_vec_of_via;
mod newtype_identified_vec_of;

pub use identified_vec_via::*;
pub use is_identified_vec_of_via::*;
pub use newtype_identified_vec_of::*;
File renamed without changes.

0 comments on commit e00c0d4

Please sign in to comment.