use core::marker::PhantomData;
pub(super) struct Origin<'a> {
address: usize,
borrow: PhantomData<&'a ()>,
}
impl<'a> Origin<'a> {
pub(super) fn of<T>(value: &'a T) -> Self {
const {
assert!(core::mem::size_of::<T>() > 0);
}
Self {
address: core::ptr::from_ref(value).addr(),
borrow: PhantomData,
}
}
pub(super) fn matches<T>(&self, value: &T) -> bool {
const {
assert!(core::mem::size_of::<T>() > 0);
}
self.address == core::ptr::from_ref(value).addr()
}
}
#[cfg(test)]
mod tests {
use super::Origin;
#[test]
fn origin_compares_live_instances_without_copying_their_payload() {
let first = [7u8; 8];
let second = first;
let origin = Origin::of(&first);
assert!(origin.matches(&first));
assert!(!origin.matches(&second));
}
}