use crate::{logic::ops::DerefLogic, prelude::*};
use core::{marker::PhantomData, ops::Deref};
#[builtin("identity")]
pub struct GhostShared<T>(PhantomData<*mut T>);
#[trusted]
unsafe impl<T: Sync> Send for GhostShared<T> {}
#[trusted]
unsafe impl<T: Sync> Sync for GhostShared<T> {}
impl<T> Invariant for GhostShared<T> {
#[logic(open, prophetic)]
fn invariant(self) -> bool {
resolve(*self) && inv(*self)
}
}
impl<T> GhostShared<T> {
#[trusted]
#[check(ghost)]
#[ensures(**result == *val)]
#[allow(unused_variables)]
pub fn new(val: Ghost<T>) -> Ghost<Self> {
Ghost::conjure()
}
}
impl<T> GhostShared<T> {
#[trusted]
#[check(ghost)]
#[ensures(*result == *self)]
pub fn to_ref<'a>(self) -> &'a T {
unreachable!("ghost only")
}
}
impl<T> AsRef<T> for GhostShared<T> {
#[check(ghost)]
#[ensures(*result == **self)]
fn as_ref(&self) -> &T {
self.to_ref()
}
}
impl<T> Clone for GhostShared<T> {
#[check(ghost)]
#[ensures(result == *self)]
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for GhostShared<T> {}
impl<T> Deref for GhostShared<T> {
type Target = T;
#[check(ghost)]
#[logic_alias(self.deref_logic())]
fn deref(&self) -> &T {
self.to_ref()
}
}
impl<T> DerefLogic for GhostShared<T> {
#[logic(opaque)]
#[builtin("identity")]
fn deref_logic(&self) -> &T {
dead
}
}