Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Dec 16, 2023
1 parent 6b2510b commit 338fa15
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 69 deletions.
20 changes: 0 additions & 20 deletions identified_vec/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,26 +827,6 @@ where
}
}

///////////////////////
//// DEBUG ///
///////////////////////
impl<I, E> IdentifiedVec<I, E>
where
E: Debug,
I: Eq + Hash + Clone + Debug,
{
#[cfg(not(tarpaulin_include))]
#[cfg(debug_assertions)]
pub fn debug(&self) {
println!("{}", self.debug_str());
}

#[cfg(debug_assertions)]
pub fn debug_str(&self) -> String {
format!("order: {:?}\nelements: {:?}", self.order, self.elements)
}
}

impl<ID, Element> IdentifiedVec<ID, Element>
where
ID: Eq + Hash + Clone + Debug,
Expand Down
11 changes: 8 additions & 3 deletions identified_vec_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#[macro_export]
macro_rules! newtype_identified_vec {
(of: $item_ty: ty, named: $struct_name: ident) => {
use identified_vec::IdentifiedVecIntoIterator;

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct $struct_name(IdentifiedVecOf<$item_ty>);

Expand All @@ -21,9 +19,16 @@ macro_rules! newtype_identified_vec {
}
}

impl std::fmt::Display for $struct_name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.elements().fmt(f)
}
}

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

fn into_iter(self) -> Self::IntoIter {
Self::IntoIter::new(self.0)
Expand Down
49 changes: 3 additions & 46 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Identifiable for User {
}
}

type SUT = IdentifiedVecOf<u32>;
newtype_identified_vec!(of: u32, named: SUT);
newtype_identified_vec!(of: User, named: Users);

#[test]
Expand All @@ -69,14 +69,6 @@ fn ids() {
assert_eq!(identified_vec.ids(), &[1, 2, 3])
}

#[test]
fn debug_str() {
let identified_vec = SUT::from_iter([1, 2, 3]);
assert!(identified_vec
.debug_str()
.starts_with("order: [1, 2, 3]\nelements: {"),)
}

#[test]
fn elements() {
let vec = vec![User::blob(), User::blob_jr(), User::blob_sr()];
Expand Down Expand Up @@ -306,6 +298,7 @@ fn append() {

#[test]
fn try_append_unique_element() {
type SUT = IdentifiedVecOf<u32>;
let mut identified_vec = SUT::from_iter([1, 2, 3]);
let result = identified_vec.try_append_unique_element(4);
assert!(result.is_ok());
Expand Down Expand Up @@ -644,50 +637,14 @@ fn display() {

#[test]
fn hash() {
type SUT = IdentifiedVecOf<u32>;
let identified_vec = SUT::from_iter([1, 2, 3]);
assert_eq!(
HashSet::<IdentifiedVec<u32, u32>>::from_iter([identified_vec.clone()]),
HashSet::from_iter([identified_vec.clone(), identified_vec])
)
}

#[test]
fn isid() {
struct CollectionOfUsersVia(IdentifiedVecOf<User>);
impl ViaMarker for CollectionOfUsersVia {}
impl IsIdentifiableVecOfVia<User> for CollectionOfUsersVia {
fn via_mut(&mut self) -> &mut IdentifiedVecOf<User> {
&mut self.0
}
fn via(&self) -> &IdentifiedVecOf<User> {
&self.0
}
fn from_identified_vec_of(identified_vec_of: IdentifiedVecOf<User>) -> Self {
Self(identified_vec_of)
}
}
impl IntoIterator for CollectionOfUsersVia {
type Item = User;

type IntoIter = IdentifiedVecIntoIterator<<User as Identifiable>::ID, User>;

fn into_iter(self) -> Self::IntoIter {
todo!()
}
}

let mut sut = CollectionOfUsersVia::new();
sut.append(User::blob_jr());
assert_eq!(sut.items(), [User::blob_jr()]);
sut.remove_at(0);
assert_eq!(sut.len(), 0);
sut.update_or_append(User::blob_sr());
sut.update_or_append(User::blob_sr());
assert_eq!(sut.items(), [User::blob_sr()]);
sut.update_or_append(User::blob_jr());
assert_eq!(sut.items(), [User::blob_sr(), User::blob_jr()]);
}

#[test]
fn test_macro() {
newtype_identified_vec!(of: User, named: CollectionOfUsers);
Expand Down

0 comments on commit 338fa15

Please sign in to comment.