Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajjon committed Dec 15, 2023
1 parent 0957a3b commit 4843b0f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
34 changes: 21 additions & 13 deletions identified_vec_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#[macro_export]
macro_rules! newtype_identified_vec {
(of: $item_ty: ty, named: $struct_name: ident) => {
use std::cell::RefMut;
// pub struct $struct_name<'a>(RefMut<'a, IdentifiedVecOf<$item_ty>>);
pub struct $struct_name(IdentifiedVecOf<$item_ty>);

// impl<'a> ViaMarker for $struct_name<'a> {}
// impl<'a> IsIdentifiableVecOf<$item_ty> for $struct_name<'a> {}
// impl<'a> IsIdentifiableVecOfVia<$item_ty> for $struct_name<'a> {
// fn via_mut(&self) -> RefMut<'a, IdentifiedVecOf<$item_ty>> {
// // &self.0
// todo!()
// }
// fn from_identified_vec_of(identified_vec_of: IdentifiedVecOf<$item_ty>) -> Self {
// Self(RefMut::new(identified_vec_of))
// }
// }
impl ViaMarker for $struct_name {}
impl IsIdentifiableVecOfVia<$item_ty> for $struct_name {
fn via_mut(&mut self) -> &mut IdentifiedVecOf<$item_ty> {
&mut self.0
}

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

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

impl $struct_name {
#[inline]
pub fn items(&self) -> Vec<$item_ty> {
self.0.items()
}
}
};
}
17 changes: 16 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,24 @@ fn isid() {
assert_eq!(sut.items(), [User::blob_sr(), User::blob_jr()]);
}

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

let mut sut = CollectionOfUsers::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);
// let mut users = CollectionOfUsers::new();
// users.0.append(User::blob());
// assert_eq!(users.0.items(), [User::blob()])
Expand Down

0 comments on commit 4843b0f

Please sign in to comment.