trait-obj-list 0.1.2

List of unsized trait objects, stored in a flat array
Documentation
#[path = "../src/test_utils.rs"]
mod utils;

use core::any::Any;
use core::sync::atomic::AtomicUsize;
use core::sync::atomic::Ordering;

use trait_obj_list::DynList;

// need to make a new type/static for each test to support parallel tests
macro_rules! mk_drop_counter {
	($typ:ident) => {
		static DROP_CNT: AtomicUsize = AtomicUsize::new(0);
		struct $typ;
		impl core::ops::Drop for $typ {
			fn drop(&mut self) {
				DROP_CNT.fetch_add(1, Ordering::SeqCst);
			}
		}
		impl $typ {
			pub fn drop_count() -> usize {
				DROP_CNT.load(Ordering::SeqCst)
			}
		}
	};
}

utils::for_each_list_type! {
	#[test]
	fn drop<T>() {
		mk_drop_counter!(CountDrop);

		let mut l = DynList::<dyn Any, T>::new();
		for _ in 0..64 {
			l.push(CountDrop);
			l.push(1234);
		}

		assert_eq!(CountDrop::drop_count(), 0);
		core::mem::drop(l);
		assert_eq!(CountDrop::drop_count(), 64);
	}

	#[test]
	fn clear<T>() {
		mk_drop_counter!(CountDrop);

		let mut l = DynList::<dyn Any, T>::new();
		assert!(l.is_empty());

		for i in 0..64 {
			l.push(CountDrop);
			l.push(i);
		}
		assert!(!l.is_empty());

		assert_eq!(CountDrop::drop_count(), 0);
		l.clear();
		assert!(l.is_empty());
		assert_eq!(CountDrop::drop_count(), 64);
	}
}