use crate::Linearity;
pub trait PointerExt<T> {
unsafe fn select(self, target_other: Self, target_predicate: bool) -> Self;
unsafe fn select_deref(self, target_other: Self, target_predicate: bool) -> T;
}
pub trait PointerMutExt<T>: PointerExt<T> {
}
impl<T> PointerExt<T> for *const T
where
T: Copy,
usize: Linearity,
{
#[inline]
unsafe fn select_deref(self, target_other: Self, target_predicate: bool) -> T {
core::ptr::read(self.select(target_other, target_predicate))
}
#[inline]
unsafe fn select(self, target_other: Self, target_dependence: bool) -> Self {
let target_left = self as usize;
let target_right = target_other as usize;
let target_outcome = usize::select(target_left, target_right, target_dependence);
target_outcome as Self
}
}
impl<T> PointerExt<T> for *mut T {
#[inline]
unsafe fn select_deref(self, target_other: Self, target_predicate: bool) -> T {
core::ptr::read(self.select(target_other, target_predicate))
}
#[inline]
unsafe fn select(self, target_other: Self, target_dependence: bool) -> Self {
let target_left = self as usize;
let target_right = target_other as usize;
let target_outcome =
<usize as Linearity>::select(target_left, target_right, target_dependence);
target_outcome as Self
}
}
impl<T> PointerMutExt<T> for *mut T where *mut T: PointerExt<T> {}