use crate::{
ghost::perm::{Container, Perm, SendPerm, SyncPerm},
prelude::*,
};
use core::cell::UnsafeCell;
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[repr(transparent)]
#[opaque]
pub struct PermCell<T: ?Sized>(UnsafeCell<T>);
impl<T: Sized> Container for PermCell<T> {
type Value = T;
}
unsafe impl<T> Send for PermCell<T> {}
unsafe impl<T> Sync for PermCell<T> {}
impl<T> SendPerm for PermCell<T> {}
impl<T> SyncPerm for PermCell<T> {}
impl<T: Sized> Invariant for Perm<PermCell<T>> {
#[logic(open, prophetic, inline)]
#[creusot::trusted_trivial_if_param_trivial]
fn invariant(self) -> bool {
pearlite! { inv(self.val()) }
}
}
impl<T> PermCell<T> {
#[trusted]
#[check(terminates)]
#[ensures(result.0 == *result.1.ward())]
#[ensures((*result.1)@ == value)]
pub fn new(value: T) -> (Self, Ghost<Box<Perm<PermCell<T>>>>) {
let this = Self(UnsafeCell::new(value));
let perm = Ghost::conjure();
(this, perm)
}
#[trusted]
#[check(terminates)]
#[requires(self == perm.ward())]
#[ensures(val == (^perm)@)]
#[ensures(resolve(perm@))]
#[ensures(self == (^perm).ward())]
pub unsafe fn set(&self, perm: Ghost<&mut Perm<PermCell<T>>>, val: T) {
let _ = perm;
unsafe {
*self.0.get() = val;
}
}
#[trusted]
#[check(terminates)]
#[requires(self == perm.ward())]
#[ensures(val == (^perm)@)]
#[ensures(result == perm@)]
#[ensures(self == (^perm).ward())]
pub unsafe fn replace(&self, perm: Ghost<&mut Perm<PermCell<T>>>, val: T) -> T {
let _ = perm;
unsafe { core::ptr::replace(self.0.get(), val) }
}
#[trusted]
#[check(terminates)]
#[requires(self == *perm.ward())]
#[ensures(result == perm@)]
pub fn into_inner(self, perm: Ghost<Box<Perm<PermCell<T>>>>) -> T {
let _ = perm;
self.0.into_inner()
}
#[trusted]
#[check(terminates)]
#[requires(self == perm.ward())]
#[ensures(*result == perm@)]
pub unsafe fn borrow<'a>(&'a self, perm: Ghost<&'a Perm<PermCell<T>>>) -> &'a T {
let _ = perm;
unsafe { &*self.0.get() }
}
#[trusted]
#[check(terminates)]
#[requires(self == perm.ward())]
#[ensures(self == (^perm).ward())]
#[ensures(*result == perm@)]
#[ensures(^result == (^perm)@)]
pub unsafe fn borrow_mut<'a>(&'a self, perm: Ghost<&'a mut Perm<PermCell<T>>>) -> &'a mut T {
let _ = perm;
unsafe { &mut *self.0.get() }
}
}
impl<T: Copy> PermCell<T> {
#[trusted]
#[check(terminates)]
#[requires(self == perm.ward())]
#[ensures(result == (**perm)@)]
pub unsafe fn get(&self, perm: Ghost<&Perm<PermCell<T>>>) -> T {
let _ = perm;
unsafe { *self.0.get() }
}
}
impl<T> PermCell<T> {
#[trusted]
#[ensures(true)]
pub fn as_ptr(&self) -> *mut T {
self.0.get()
}
#[trusted]
#[check(terminates)]
#[ensures(result.0 == result.1.ward())]
#[ensures(^t == (^result.1)@)]
#[ensures(*t == result.1@)]
pub fn from_mut(t: &mut T) -> (&PermCell<T>, Ghost<&mut Perm<PermCell<T>>>) {
let cell: &PermCell<T> = unsafe { &*(t as *mut T as *const Self) };
let perm = Ghost::conjure();
(cell, perm)
}
}
impl<T: Default> PermCell<T> {
#[requires(self == perm.ward())]
#[ensures(self == (^perm).ward())]
#[ensures(result == perm@)]
#[ensures(T::default.postcondition((), (^perm)@))]
pub unsafe fn take(&self, perm: Ghost<&mut Perm<PermCell<T>>>) -> T {
unsafe { self.replace(perm, T::default()) }
}
}