Skip to main content

creusot_std/std/
rc.rs

1use crate::prelude::*;
2#[cfg(creusot)]
3use crate::std::ptr::PointerExt as _;
4use alloc::rc::Rc;
5#[cfg(feature = "nightly")]
6use alloc::{alloc::Allocator, boxed::Box};
7#[cfg(creusot)]
8use core::ops::Deref;
9
10/// Extension trait for [`Rc`].
11pub trait RcExt {
12    /// The `T` in `Rc<T>`
13    type Pointee: ?Sized;
14
15    /// Get the underlying raw pointer, in logic.
16    ///
17    /// Used to specify [`Rc::as_ptr`].
18    #[logic]
19    fn as_ptr_logic(self) -> *const Self::Pointee;
20}
21
22#[cfg(feature = "nightly")]
23impl<T: ?Sized, A: Allocator> RcExt for Rc<T, A> {
24    type Pointee = T;
25    #[logic(opaque)]
26    fn as_ptr_logic(self) -> *const T {
27        dead
28    }
29}
30
31#[cfg(feature = "nightly")]
32impl<T: DeepModel + ?Sized, A: Allocator> DeepModel for Rc<T, A> {
33    type DeepModelTy = T::DeepModelTy;
34    #[logic(open, inline)]
35    fn deep_model(self) -> Self::DeepModelTy {
36        pearlite! { *self.view().deep_model() }
37    }
38}
39
40#[cfg(feature = "nightly")]
41impl<T: ?Sized, A: Allocator> View for Rc<T, A> {
42    type ViewTy = Box<T>;
43    #[logic(opaque)]
44    fn view(self) -> Self::ViewTy {
45        dead
46    }
47}
48
49extern_spec! {
50    impl<T> Rc<T> {
51        #[check(ghost)]
52        #[ensures(*result@ == value)]
53        fn new(value: T) -> Self;
54    }
55
56    impl<T, A: Allocator> Rc<T, A> {
57        #[check(ghost)]
58        #[ensures(result == this.as_ptr_logic())]
59        #[ensures(!result.is_null_logic())]
60        fn as_ptr(this: &Rc<T, A>) -> *const T;
61
62        #[check(terminates)] // Not ghost, as this would allow deducing that there is a finite number of possible `Rc`s.
63        #[ensures(result == (this.as_ptr_logic().deep_model() == other.as_ptr_logic().deep_model()))]
64        #[ensures(result ==> this@ == other@)]
65        fn ptr_eq(this: &Rc<T, A>, other: &Rc<T, A>) -> bool;
66    }
67
68
69    impl<T, A: Allocator> AsRef<T> for Rc<T, A> {
70        #[check(ghost)]
71        #[ensures(*result == *(*self)@)]
72        fn as_ref(&self) -> &T;
73    }
74
75    impl<T: ?Sized, A: Allocator + Clone> Clone for Rc<T, A> {
76        #[check(ghost)]
77        #[ensures(result == *self)]
78        fn clone(&self) -> Rc<T, A>;
79    }
80
81    impl<T: ?Sized, A: Allocator> Deref for Rc<T, A> {
82        #[check(ghost)]
83        #[ensures(*result == *(*self)@)]
84        fn deref(&self) -> &T { self.as_ref() }
85    }
86}
87
88/// Dummy impls that don't use the unstable trait Allocator
89#[cfg(not(feature = "nightly"))]
90impl<T: DeepModel> DeepModel for Rc<T> {
91    type DeepModelTy = T::DeepModelTy;
92}
93
94#[cfg(not(feature = "nightly"))]
95impl<T> View for Rc<T> {
96    type ViewTy = T;
97}