use core::ptr::NonNull;
use crate::VolatilePtr;
impl<'a, T, A> VolatilePtr<'a, T, A>
where
T: ?Sized,
{
pub const unsafe fn map_const<F, U>(self, f: F) -> VolatilePtr<'a, U, A>
where
F: FnOnce(NonNull<T>) -> NonNull<U>,
U: ?Sized,
{
unsafe { VolatilePtr::new_generic(f(self.pointer)) }
}
}
#[cfg(feature = "unstable")]
impl<'a, T, A> VolatilePtr<'a, [T], A> {
pub const fn index_const(self, index: usize) -> VolatilePtr<'a, T, A> {
assert!(index < self.pointer.len(), "index out of bounds");
struct Mapper {
index: usize,
}
impl<T> FnOnce<(NonNull<[T]>,)> for Mapper {
type Output = NonNull<T>;
extern "rust-call" fn call_once(self, (slice,): (NonNull<[T]>,)) -> Self::Output {
unsafe { NonNull::new_unchecked(slice.as_non_null_ptr().as_ptr().add(self.index)) }
}
}
unsafe { self.map_const(Mapper { index }) }
}
}