#[cfg(creusot)]
use crate::util::SizedW;
use crate::*;
pub type RawPtr<T> = *const T;
#[trusted]
pub struct PtrOwn<T: ?Sized>(std::marker::PhantomData<T>);
impl<T: ?Sized> PtrOwn<T> {
#[trusted]
#[logic]
pub fn ptr(&self) -> RawPtr<T> {
dead
}
#[trusted]
#[logic]
pub fn val(&self) -> SizedW<T> {
dead
}
}
impl<T: ?Sized> Invariant for PtrOwn<T> {
#[predicate(prophetic)]
#[open]
#[creusot::trusted_ignore_structural_inv]
#[creusot::trusted_is_tyinv_trivial_if_param_trivial]
fn invariant(self) -> bool {
pearlite! { !self.ptr().is_null_logic() && inv(self.val()) }
}
}
impl<T> PtrOwn<T> {
#[ensures(result.1.ptr() == result.0 && *result.1.val() == v)]
pub fn new(v: T) -> (RawPtr<T>, GhostBox<PtrOwn<T>>) {
Self::from_box(Box::new(v))
}
}
impl<T: ?Sized> PtrOwn<T> {
#[trusted]
#[ensures(result.1.ptr() == result.0 && *result.1.val() == *val)]
pub fn from_box(val: Box<T>) -> (RawPtr<T>, GhostBox<PtrOwn<T>>) {
assert!(core::mem::size_of_val::<T>(&*val) > 0, "PtrOwn doesn't support ZSTs");
(Box::into_raw(val), GhostBox::conjure())
}
#[trusted]
#[requires(ptr == own.ptr())]
#[ensures(*result == *own.val())]
#[allow(unused_variables)]
pub fn as_ref(ptr: RawPtr<T>, own: GhostBox<&PtrOwn<T>>) -> &T {
unsafe { &*ptr }
}
#[trusted]
#[allow(unused_variables)]
#[requires(ptr == own.ptr())]
#[ensures(*result == *own.val())]
#[ensures((^own.inner_logic()).ptr() == own.ptr())]
#[ensures(*(^own.inner_logic()).val() == ^result)]
pub fn as_mut(ptr: RawPtr<T>, own: GhostBox<&mut PtrOwn<T>>) -> &mut T {
unsafe { &mut *(ptr as *mut _) }
}
#[trusted]
#[requires(ptr == own.ptr())]
#[ensures(*result == *own.val())]
#[allow(unused_variables)]
pub fn to_box(ptr: RawPtr<T>, own: GhostBox<PtrOwn<T>>) -> Box<T> {
unsafe { Box::from_raw(ptr as *mut _) }
}
#[requires(ptr == own.ptr())]
pub fn drop(ptr: RawPtr<T>, own: GhostBox<PtrOwn<T>>) {
let _ = Self::to_box(ptr, own);
}
#[trusted]
#[pure]
#[ensures(own1.ptr().addr_logic() != own2.ptr().addr_logic())]
#[ensures(*own1 == ^own1)]
#[allow(unused_variables)]
pub fn disjoint_lemma(own1: &mut PtrOwn<T>, own2: &PtrOwn<T>) {
panic!()
}
}