Skip to main content

trait_obj_list/iters/
into_boxed_iter.rs

1use crate::iters::IntoPtrsIter;
2use crate::ptr;
3use crate::types::ListType;
4
5/// Iterator that consumes a [`DynList`](crate::DynList) and returns its elements
6/// moved into boxes.
7pub struct IntoBoxedIter<T, Typ>
8where
9	T: ptr::Pointee<Metadata = ptr::DynMetadata<T>> + ?Sized,
10	Typ: ListType<T>,
11{
12	pub(crate) inner: IntoPtrsIter<T, Typ>,
13}
14impl<T, Typ> Iterator for IntoBoxedIter<T, Typ>
15where
16	T: ptr::Pointee<Metadata = ptr::DynMetadata<T>> + ?Sized,
17	Typ: ListType<T>,
18{
19	type Item = alloc::boxed::Box<T>;
20
21	fn next(&mut self) -> Option<Self::Item> {
22		self.inner.next().map(|p| unsafe {
23			let (p, md) = ptr::to_raw_parts_mut::<T>(p);
24			let layout = md.layout();
25			let bp = alloc::alloc::alloc(layout);
26			if bp.is_null() {
27				alloc::alloc::handle_alloc_error(layout);
28			}
29			bp.copy_from_nonoverlapping(p as *const u8, layout.size());
30			alloc::boxed::Box::from_raw(ptr::from_raw_parts_mut(bp as *mut (), md))
31		})
32	}
33
34	fn size_hint(&self) -> (usize, Option<usize>) {
35		self.inner.size_hint()
36	}
37}
38impl<T, Typ> ExactSizeIterator for IntoBoxedIter<T, Typ>
39where
40	T: ptr::Pointee<Metadata = ptr::DynMetadata<T>> + ?Sized,
41	Typ: ListType<T>,
42{
43}