From e00c0d4b4bf5654cf1b7198bda8d313f0660ccbd Mon Sep 17 00:00:00 2001 From: Alexander Cyon Date: Mon, 18 Dec 2023 14:26:33 +0100 Subject: [PATCH] WIP --- src/lib.rs | 6 ++ src/vec_of/mod.rs | 4 -- src/via/identified_vec_via.rs | 68 +++++++++++++++++++ .../is_identified_vec_of_via.rs | 6 +- src/via/mod.rs | 7 ++ .../newtype_identified_vec_of.rs | 0 6 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 src/via/identified_vec_via.rs rename src/{vec_of => via}/is_identified_vec_of_via.rs (97%) create mode 100644 src/via/mod.rs rename src/{vec_of => via}/newtype_identified_vec_of.rs (100%) diff --git a/src/lib.rs b/src/lib.rs index 05097e1..7831d96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. @@ -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::*; diff --git a/src/vec_of/mod.rs b/src/vec_of/mod.rs index f0f6998..873ad39 100644 --- a/src/vec_of/mod.rs +++ b/src/vec_of/mod.rs @@ -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::*; diff --git a/src/via/identified_vec_via.rs b/src/via/identified_vec_via.rs new file mode 100644 index 0000000..e226219 --- /dev/null +++ b/src/via/identified_vec_via.rs @@ -0,0 +1,68 @@ +use crate::{ + identified_vec_into_iterator::IdentifiedVecIntoIterator, Identifiable, IdentifiedVecOf, + IsIdentifiableVecOfVia, ViaMarker, +}; + +pub struct IdentifiedVecVia(IdentifiedVecOf); + +impl ViaMarker for IdentifiedVecVia {} +impl IsIdentifiableVecOfVia for IdentifiedVecVia { + fn via_mut(&mut self) -> &mut IdentifiedVecOf { + &mut self.0 + } + + fn via(&self) -> &IdentifiedVecOf { + &self.0 + } + + fn from_identified_vec_of(identified_vec_of: IdentifiedVecOf) -> Self { + Self(identified_vec_of) + } +} + +impl std::fmt::Display + for IdentifiedVecVia +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.0, f) + } +} + +impl IntoIterator for IdentifiedVecVia { + type Item = Element; + type IntoIter = IdentifiedVecIntoIterator<::ID, Element>; + + fn into_iter(self) -> Self::IntoIter { + Self::IntoIter::new(self.0) + } +} + +#[cfg(any(test, feature = "serde"))] +impl serde::Serialize for IdentifiedVecVia +where + Element: serde::Serialize + Identifiable + std::fmt::Debug + Clone, +{ + fn serialize( + &self, + serializer: S, + ) -> Result<::Ok, ::Error> + where + S: serde::Serializer, + { + IdentifiedVecOf::serialize(&self.0, serializer) + } +} + +#[cfg(any(test, feature = "serde"))] +impl<'de, Element> serde::Deserialize<'de> for IdentifiedVecVia +where + Element: serde::Deserialize<'de> + Identifiable + std::fmt::Debug + Clone, +{ + #[cfg(not(tarpaulin_include))] // false negative + fn deserialize>( + deserializer: D, + ) -> Result, D::Error> { + let id_vec_of = IdentifiedVecOf::::deserialize(deserializer)?; + return Ok(Self::from_identified_vec_of(id_vec_of)); + } +} diff --git a/src/vec_of/is_identified_vec_of_via.rs b/src/via/is_identified_vec_of_via.rs similarity index 97% rename from src/vec_of/is_identified_vec_of_via.rs rename to src/via/is_identified_vec_of_via.rs index 34e06f5..d995ee5 100644 --- a/src/vec_of/is_identified_vec_of_via.rs +++ b/src/via/is_identified_vec_of_via.rs @@ -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 {} diff --git a/src/via/mod.rs b/src/via/mod.rs new file mode 100644 index 0000000..12ca04b --- /dev/null +++ b/src/via/mod.rs @@ -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::*; diff --git a/src/vec_of/newtype_identified_vec_of.rs b/src/via/newtype_identified_vec_of.rs similarity index 100% rename from src/vec_of/newtype_identified_vec_of.rs rename to src/via/newtype_identified_vec_of.rs