Skip to main content

vstd/std_specs/
core.rs

1use super::super::prelude::*;
2use core::marker::PointeeSized;
3
4use verus as verus_skip_verusfmt;
5verus_skip_verusfmt! {
6
7#[verifier::external_trait_specification]
8pub trait ExTuple {
9    type ExternalTraitSpecificationFor: core::marker::Tuple;
10}
11
12#[verifier::external_trait_specification]
13pub trait ExFnOnce<Args: core::marker::Tuple> {
14    type ExternalTraitSpecificationFor: core::ops::FnOnce<Args>;
15
16    type Output;
17}
18
19#[verifier::external_trait_specification]
20pub trait ExFnMut<Args: core::marker::Tuple>: FnOnce<Args> {
21    type ExternalTraitSpecificationFor: core::ops::FnMut<Args>;
22}
23
24#[verifier::external_trait_specification]
25pub trait ExFn<Args: core::marker::Tuple>: FnMut<Args> {
26    type ExternalTraitSpecificationFor: core::ops::Fn<Args>;
27}
28
29#[verifier::external_trait_specification]
30pub trait ExDeref: PointeeSized {
31    type ExternalTraitSpecificationFor: core::ops::Deref;
32
33    type Target: ?Sized;
34
35    fn deref(&self) -> &Self::Target;
36}
37
38#[verifier::external_trait_specification]
39pub trait ExDerefMut: core::ops::Deref + PointeeSized {
40    type ExternalTraitSpecificationFor: core::ops::DerefMut;
41
42    fn deref_mut(&mut self) -> &mut Self::Target;
43}
44
45#[verifier::external_trait_specification]
46#[verifier::external_trait_extension(IndexSpec via IndexSpecImpl)]
47pub trait ExIndex<Idx> where Idx: ?Sized {
48    type ExternalTraitSpecificationFor: core::ops::Index<Idx>;
49
50    type Output: ?Sized;
51
52    // NOTE: this used as a precondition for both `Index` and `IndexMut`,
53    // since both share the same `s[i]` syntax.
54    spec fn index_req(&self, index: &Idx) -> bool;
55
56    fn index(&self, index: Idx) -> (output: &Self::Output) where Idx: Sized
57        requires
58            self.index_req(&index),
59    ;
60}
61
62#[verifier::external_trait_specification]
63pub trait ExIndexMut<Idx>: core::ops::Index<Idx> where Idx: ?Sized {
64    type ExternalTraitSpecificationFor: core::ops::IndexMut<Idx>;
65
66    fn index_mut(&mut self, index: Idx) -> (output: &mut Self::Output) where Idx: Sized
67        requires
68            self.index_req(&index),
69    ;
70}
71
72#[verifier::external_trait_specification]
73pub trait ExInteger: Copy {
74    type ExternalTraitSpecificationFor: Integer;
75}
76
77#[verifier::external_trait_specification]
78pub trait ExSpecOrd<Rhs> {
79    type ExternalTraitSpecificationFor: SpecOrd<Rhs>;
80}
81
82#[cfg(not(verus_verify_core))]
83#[verifier::external_trait_specification]
84pub trait ExAllocator {
85    type ExternalTraitSpecificationFor: core::alloc::Allocator;
86}
87
88#[cfg(not(verus_verify_core))]
89#[verifier::external_trait_specification]
90pub trait ExAllocatorClone: core::alloc::Allocator + Clone {
91    type ExternalTraitSpecificationFor: core::alloc::AllocatorClone;
92}
93
94#[verifier::external_trait_specification]
95pub trait ExFreeze: PointeeSized {
96    type ExternalTraitSpecificationFor: core::marker::Freeze;
97}
98
99// Declaring the methods means an impl of `Hasher` cannot add a `requires` clause, which
100// std's callers (such as the `Hash` impls) would not satisfy. The unstable `write_str` and
101// `write_length_prefix` are not declared.
102#[verifier::external_trait_specification]
103pub trait ExHasher {
104    type ExternalTraitSpecificationFor: core::hash::Hasher;
105
106    fn finish(&self) -> u64;
107
108    fn write(&mut self, bytes: &[u8]);
109
110    fn write_u8(&mut self, i: u8);
111
112    fn write_u16(&mut self, i: u16);
113
114    fn write_u32(&mut self, i: u32);
115
116    fn write_u64(&mut self, i: u64);
117
118    fn write_u128(&mut self, i: u128);
119
120    fn write_usize(&mut self, i: usize);
121
122    fn write_i8(&mut self, i: i8);
123
124    fn write_i16(&mut self, i: i16);
125
126    fn write_i32(&mut self, i: i32);
127
128    fn write_i64(&mut self, i: i64);
129
130    fn write_i128(&mut self, i: i128);
131
132    fn write_isize(&mut self, i: isize);
133}
134
135// Declaring the methods means an impl of `Hash` cannot add a `requires` clause, which
136// std's callers (such as `HashMap::insert`) would not satisfy.
137#[verifier::external_trait_specification]
138pub trait ExHash: PointeeSized {
139    type ExternalTraitSpecificationFor: core::hash::Hash;
140
141    fn hash<H: core::hash::Hasher>(&self, state: &mut H);
142
143    fn hash_slice<H: core::hash::Hasher>(data: &[Self], state: &mut H) where Self: Sized;
144}
145
146#[verifier::external_trait_specification]
147pub trait ExPtrPointee: PointeeSized {
148    type ExternalTraitSpecificationFor: core::ptr::Pointee;
149
150    type Metadata:
151        Copy + Send + Sync + Ord + core::hash::Hash + Unpin + core::fmt::Debug + Sized + core::marker::Freeze;
152}
153
154#[verifier::external_trait_specification]
155pub trait ExBorrow<Borrowed> where Borrowed: ?Sized {
156    type ExternalTraitSpecificationFor: core::borrow::Borrow<Borrowed>;
157}
158
159#[verifier::external_trait_specification]
160pub trait ExStructural {
161    type ExternalTraitSpecificationFor: Structural;
162}
163
164// Since this trait involves the unstable library feature `const_destruct`,
165// we only enable it when verifying core
166#[cfg(verus_verify_core)]
167#[verifier::external_trait_specification]
168trait ExDestruct: PointeeSized {
169    type ExternalTraitSpecificationFor: core::marker::Destruct;
170}
171
172#[verifier::external_trait_specification]
173pub trait ExMetaSized {
174    type ExternalTraitSpecificationFor: core::marker::MetaSized;
175}
176
177pub assume_specification<T>[ core::mem::swap::<T> ](a: &mut T, b: &mut T)
178    ensures
179        *final(a) == *old(b),
180        *final(b) == *old(a),
181    opens_invariants none
182    no_unwind
183;
184
185#[verifier::external_type_specification]
186pub struct ExOrdering(core::cmp::Ordering);
187
188#[verifier::external_type_specification]
189#[verifier::accept_recursive_types(V)]
190#[verifier::ext_equal]
191pub struct ExOption<V>(core::option::Option<V>);
192
193#[verifier::external_type_specification]
194#[verifier::accept_recursive_types(T)]
195#[verifier::reject_recursive_types_in_ground_variants(E)]
196pub struct ExResult<T, E>(core::result::Result<T, E>);
197
198// I don't really expect this to be particularly useful;
199// this is mostly here because I wanted an easy way to test
200// the combination of external_type_specification & external_body
201// in a cross-crate context.
202#[verifier::external_type_specification]
203#[verifier::external_body]
204pub struct ExDuration(core::time::Duration);
205
206#[verifier::external_type_specification]
207#[verifier::accept_recursive_types(V)]
208pub struct ExPhantomData<V: PointeeSized>(core::marker::PhantomData<V>);
209
210pub assume_specification[ core::intrinsics::likely ](b: bool) -> (c: bool)
211    ensures
212        c == b,
213;
214
215pub assume_specification[ core::intrinsics::unlikely ](b: bool) -> (c: bool)
216    ensures
217        c == b,
218;
219
220pub assume_specification<T, F: FnOnce() -> T>[ bool::then ](b: bool, f: F) -> (ret: Option<T>)
221    requires
222        b ==> f.requires(()),
223    ensures
224        if b {
225            ret.is_some() && f.ensures((), ret.unwrap())
226        } else {
227            ret.is_none()
228        },
229;
230
231pub assume_specification<T> [core::hint::must_use] (value: T) -> (ret: T)
232    ensures
233        ret == value,
234;
235
236pub assume_specification [core::panicking::panic] (s: &'static str) -> !
237    requires
238        false,
239;
240
241pub assume_specification [core::panicking::panic_fmt] (s: core::fmt::Arguments<'_>) -> !
242    requires
243        false,
244;
245
246// Called by `assert_eq!` and `assert_ne!`; not external_body, since the macros construct an `AssertKind`
247#[verifier::external_type_specification]
248pub struct ExAssertKind(core::panicking::AssertKind);
249
250pub assume_specification<T, U>[ core::panicking::assert_failed::<T, U> ](
251    kind: core::panicking::AssertKind,
252    left: &T,
253    right: &U,
254    args: Option<core::fmt::Arguments<'_>>,
255) -> !
256    where
257        T: core::fmt::Debug + ?Sized,
258        U: core::fmt::Debug + ?Sized,
259    requires
260        false,
261;
262
263} // verus!
264
265#[verifier::external_type_specification]
266#[verifier::external_body]
267#[verifier::accept_recursive_types(T)]
268pub struct ExAssertParamIsClone<T: Clone + PointeeSized>(core::clone::AssertParamIsClone<T>);