Skip to content

Commit a9e8509

Browse files
committed
add VolatilePtr::iter & VolatilePtr::iter_mut
1 parent 4accd51 commit a9e8509

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,24 @@ impl<'a, T, R, W> VolatilePtr<'a, [T], Access<R, W>> {
476476
}
477477
}
478478

479+
/// Returns an iterator over the slice.
480+
pub fn iter<'b>(
481+
&'b self,
482+
) -> impl Iterator<Item = VolatilePtr<'b, T, Access<R, access::NoAccess>>> + 'b {
483+
let len = self.len();
484+
(0..len).map(move |i| self.index(i))
485+
}
486+
487+
/// Returns an iterator that allows modifying each value.
488+
pub fn iter_mut<'b>(
489+
&'b mut self,
490+
) -> impl Iterator<Item = VolatilePtr<'b, T, Access<R, W>>> + 'b {
491+
let ptr = self.as_ptr().as_ptr() as *mut T;
492+
let len = self.len();
493+
(0..len)
494+
.map(move |i| unsafe { VolatilePtr::new_generic(NonNull::new_unchecked(ptr.add(i))) })
495+
}
496+
479497
/// Copies all elements from `self` into `dst`, using a volatile memcpy.
480498
///
481499
/// The length of `dst` must be the same as `self`.

0 commit comments

Comments
 (0)