trait-obj-list 0.1.2

List of unsized trait objects, stored in a flat array
Documentation
use trait_obj_list::DynList;

#[path = "../src/test_utils.rs"]
mod utils;

#[cfg_attr(feature = "ptr_meta", trait_obj_list::ptr_meta::pointee)]
trait Integer: std::fmt::Debug {
	fn as_i32(&self) -> i32;
}
trait_obj_list::impl_from_impl!(dyn Integer);

impl Integer for i32 {
	fn as_i32(&self) -> i32 {
		*self
	}
}

impl Integer for i16 {
	fn as_i32(&self) -> i32 {
		*self as i32
	}
}

impl Integer for i8 {
	fn as_i32(&self) -> i32 {
		*self as i32
	}
}

utils::for_each_list_type! {
	#[test]
	fn test_custom_trait_debug<T>() {
		let mut l = DynList::<dyn Integer, T>::new();
		l.push(1i8);
		l.push(2i32);
		l.push(3i16);

		assert_eq!(format!("{l:?}"), "[1, 2, 3]");
	}

	#[test]
	fn test_custom_trait<T>() {
		let mut l = DynList::<dyn Integer, T>::new();
		l.push(1i8);
		l.push(2i32);
		l.push(3i16);

		let v = l.iter().map(|v| v.as_i32()).collect::<Vec<_>>();
		assert_eq!(v, [1,2,3]);
	}
}