|
| 1 | +use crate::{ |
| 2 | + GrowthWithConstantTimeAccess, concurrent_pinned_vec::iter_ptr_slices::IterPtrOfConSlices, |
| 3 | +}; |
| 4 | +use core::{cell::UnsafeCell, ops::Range}; |
| 5 | + |
| 6 | +pub struct IterPtrOfCon<'a, T, G> |
| 7 | +where |
| 8 | + G: GrowthWithConstantTimeAccess, |
| 9 | +{ |
| 10 | + slices: IterPtrOfConSlices<'a, T, G>, |
| 11 | + len_of_remaining_slices: usize, |
| 12 | + current_ptr: *const T, |
| 13 | + current_last: *const T, |
| 14 | +} |
| 15 | + |
| 16 | +impl<'a, T, G> IterPtrOfCon<'a, T, G> |
| 17 | +where |
| 18 | + G: GrowthWithConstantTimeAccess, |
| 19 | +{ |
| 20 | + pub fn new( |
| 21 | + capacity: usize, |
| 22 | + fragments: &'a [UnsafeCell<*mut T>], |
| 23 | + growth: G, |
| 24 | + range: Range<usize>, |
| 25 | + ) -> Self { |
| 26 | + let len_of_remaining_slices = range.len(); |
| 27 | + let slices = IterPtrOfConSlices::new(capacity, fragments, growth, range); |
| 28 | + Self { |
| 29 | + slices, |
| 30 | + len_of_remaining_slices, |
| 31 | + current_ptr: core::ptr::null(), |
| 32 | + current_last: core::ptr::null(), |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + fn remaining(&self) -> usize { |
| 37 | + let remaining_current = match self.current_ptr.is_null() { |
| 38 | + true => 0, |
| 39 | + // SAFETY: whenever current_ptr is not null, we know that current_last is also not |
| 40 | + // null which is >= current_ptr. |
| 41 | + false => unsafe { self.current_last.offset_from(self.current_ptr) as usize + 1 }, |
| 42 | + }; |
| 43 | + |
| 44 | + self.len_of_remaining_slices + remaining_current |
| 45 | + } |
| 46 | + |
| 47 | + fn next_slice(&mut self) -> Option<*mut T> { |
| 48 | + self.slices.next().and_then(|(ptr, len)| { |
| 49 | + debug_assert!(len > 0); |
| 50 | + self.len_of_remaining_slices -= len; |
| 51 | + // SAFETY: pointers are not null since slice is not empty |
| 52 | + self.current_ptr = ptr; |
| 53 | + self.current_last = unsafe { ptr.add(len - 1) }; |
| 54 | + self.next() |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl<'a, T, G> Iterator for IterPtrOfCon<'a, T, G> |
| 60 | +where |
| 61 | + G: GrowthWithConstantTimeAccess, |
| 62 | +{ |
| 63 | + type Item = *mut T; |
| 64 | + |
| 65 | + fn next(&mut self) -> Option<Self::Item> { |
| 66 | + match self.current_ptr { |
| 67 | + ptr if ptr.is_null() => self.next_slice(), |
| 68 | + ptr if ptr == self.current_last => { |
| 69 | + self.current_ptr = core::ptr::null_mut(); |
| 70 | + Some(ptr as *mut T) |
| 71 | + } |
| 72 | + ptr => { |
| 73 | + // SAFETY: current_ptr is not the last element, hance current_ptr+1 is in bounds |
| 74 | + self.current_ptr = unsafe { self.current_ptr.add(1) }; |
| 75 | + |
| 76 | + // SAFETY: ptr is valid and its value can be taken. |
| 77 | + // Drop will skip this position which is now uninitialized. |
| 78 | + Some(ptr as *mut T) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 84 | + let len = self.remaining(); |
| 85 | + (len, Some(len)) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl<'a, T, G> ExactSizeIterator for IterPtrOfCon<'a, T, G> |
| 90 | +where |
| 91 | + G: GrowthWithConstantTimeAccess, |
| 92 | +{ |
| 93 | + fn len(&self) -> usize { |
| 94 | + self.remaining() |
| 95 | + } |
| 96 | +} |
0 commit comments