Skip to main content

vstd/std_specs/
cmp.rs

1use super::super::prelude::*;
2use super::borrow::BorrowSpec;
3
4#[cfg(feature = "alloc")]
5use alloc::{boxed::Box, rc::Rc, sync::Arc};
6#[cfg(feature = "alloc")]
7use core::alloc::Allocator;
8use core::borrow::Borrow;
9use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
10use core::marker::PointeeSized;
11
12use verus as verus_skip_verusfmt;
13verus_skip_verusfmt! {
14
15#[verifier::external_trait_specification]
16#[verifier::external_trait_extension(PartialEqSpec via PartialEqSpecImpl)]
17pub trait ExPartialEq<Rhs: PointeeSized = Self>: PointeeSized {
18    type ExternalTraitSpecificationFor: PartialEq<Rhs>;
19
20    spec fn obeys_eq_spec() -> bool;
21
22    spec fn eq_spec(&self, other: &Rhs) -> bool;
23
24    fn eq(&self, other: &Rhs) -> (r: bool)
25        ensures
26            Self::obeys_eq_spec() ==> r == self.eq_spec(other);
27
28    fn ne(&self, other: &Rhs) -> (r: bool)
29        ensures
30            Self::obeys_eq_spec() ==> r == !self.eq_spec(other),
31        default_ensures
32            call_ensures(Self::eq, (self, other), !r);
33}
34
35#[verifier::external_trait_specification]
36pub trait ExEq: PartialEq + PointeeSized {
37    type ExternalTraitSpecificationFor: Eq;
38}
39
40#[verifier::external_type_specification]
41#[verifier::external_body]
42#[verifier::accept_recursive_types(T)]
43pub struct ExAssertParamIsEq<T: Eq + PointeeSized>(core::cmp::AssertParamIsEq<T>);
44
45#[verifier::external_trait_specification]
46#[verifier::external_trait_extension(PartialOrdSpec via PartialOrdSpecImpl)]
47pub trait ExPartialOrd<Rhs: PointeeSized = Self>: PartialEq<Rhs> + PointeeSized {
48    type ExternalTraitSpecificationFor: PartialOrd<Rhs>;
49
50    spec fn obeys_partial_cmp_spec() -> bool;
51
52    spec fn partial_cmp_spec(&self, other: &Rhs) -> Option<Ordering>;
53
54    fn partial_cmp(&self, other: &Rhs) -> (r: Option<Ordering>)
55        ensures
56            Self::obeys_partial_cmp_spec() ==> r == self.partial_cmp_spec(other);
57
58    fn lt(&self, other: &Rhs) -> (r: bool)
59        ensures
60            Self::obeys_partial_cmp_spec() ==>
61                (r <==> self.partial_cmp_spec(other) == Some(Ordering::Less)),
62        default_ensures
63            exists|o: Option<Ordering>|
64                {
65                    &&& #[trigger] call_ensures(Self::partial_cmp, (self, other), o)
66                    &&& r <==> o == Some(Ordering::Less)
67                }
68    ;
69
70    fn le(&self, other: &Rhs) -> (r: bool)
71        ensures
72            Self::obeys_partial_cmp_spec() ==>
73                (r <==> self.partial_cmp_spec(other) matches Some(
74                    Ordering::Less
75                    | Ordering::Equal,
76                )),
77        default_ensures
78            exists|o: Option<Ordering>|
79                {
80                    &&& #[trigger] call_ensures(Self::partial_cmp, (self, other), o)
81                    &&& r <==> o matches Some(
82                        Ordering::Less
83                        | Ordering::Equal,
84                    )
85                }
86    ;
87
88    fn gt(&self, other: &Rhs) -> (r: bool)
89        ensures
90            Self::obeys_partial_cmp_spec() ==>
91                (r <==> self.partial_cmp_spec(other) == Some(Ordering::Greater)),
92        default_ensures
93            exists|o: Option<Ordering>|
94                {
95                    &&& #[trigger] call_ensures(Self::partial_cmp, (self, other), o)
96                    &&& r <==> o == Some(Ordering::Greater)
97                }
98    ;
99
100    fn ge(&self, other: &Rhs) -> (r: bool)
101        ensures
102            Self::obeys_partial_cmp_spec() ==>
103                (r <==> self.partial_cmp_spec(other) matches Some(
104                    Ordering::Greater
105                    | Ordering::Equal,
106                )),
107        default_ensures
108            exists|o: Option<Ordering>|
109                {
110                    &&& #[trigger] call_ensures(Self::partial_cmp, (self, other), o)
111                    &&& r <==> o matches Some(
112                        Ordering::Greater
113                        | Ordering::Equal,
114                    )
115                }
116    ;
117}
118
119#[verifier::external_trait_specification]
120#[verifier::external_trait_extension(OrdSpec via OrdSpecImpl)]
121pub trait ExOrd: Eq + PartialOrd + PointeeSized {
122    type ExternalTraitSpecificationFor: Ord;
123
124    spec fn obeys_cmp_spec() -> bool;
125
126    spec fn cmp_spec(&self, other: &Self) -> Ordering;
127
128    fn cmp(&self, other: &Self) -> (r: Ordering)
129        ensures
130            Self::obeys_cmp_spec() ==> r == self.cmp_spec(other);
131
132    fn max(self, other: Self) -> (r: Self)
133        ensures
134            ({
135                &&& Self::obeys_cmp_spec()
136                &&& Self::obeys_partial_cmp_spec()
137                &&& other.partial_cmp_spec(&self) == Some(other.cmp_spec(&self))
138            }) ==> match other.cmp_spec(&self) {
139                Ordering::Less => r == self,
140                Ordering::Equal => r == other,
141                Ordering::Greater => r == other,
142            }
143        default_ensures
144            exists|b: bool|
145                {
146                    &&& #[trigger] call_ensures(Self::lt, (&other, &self), b)
147                    &&& b ==> r == self
148                    &&& !b ==> r == other
149                }
150    ;
151
152    fn min(self, other: Self) -> (r: Self)
153        ensures
154            ({
155                &&& Self::obeys_cmp_spec()
156                &&& Self::obeys_partial_cmp_spec()
157                &&& other.partial_cmp_spec(&self) == Some(other.cmp_spec(&self))
158            }) ==> match other.cmp_spec(&self) {
159                Ordering::Less => r == other,
160                Ordering::Equal => r == self,
161                Ordering::Greater => r == self,
162            }
163        default_ensures
164            exists|b: bool|
165                {
166                    &&& #[trigger] call_ensures(Self::lt, (&other, &self), b)
167                    &&& b ==> r == other
168                    &&& !b ==> r == self
169                }
170    ;
171
172    fn clamp(self, min: Self, max: Self) -> (r: Self)
173        requires
174            // There's an "assert!(min <= max)" in the provided clamp that must succeed, so:
175            Self::obeys_partial_cmp_spec(),
176            min.partial_cmp_spec(&max) matches Some(
177                Ordering::Less
178                | Ordering::Equal,
179            ),
180        ensures
181            ({
182                &&& Self::obeys_cmp_spec()
183                &&& Self::obeys_partial_cmp_spec()
184                &&& self.partial_cmp_spec(&min) == Some(self.cmp_spec(&min))
185                &&& self.partial_cmp_spec(&max) == Some(self.cmp_spec(&max))
186            }) ==> match (self.cmp_spec(&min), self.cmp_spec(&max)) {
187                (Ordering::Less, _) => r == min,
188                (_, Ordering::Greater) => r == max,
189                _ => r == self,
190            }
191        default_ensures
192            exists|b1: bool|
193                {
194                    &&& #[trigger] call_ensures(Self::lt, (&self, &min), b1)
195                    &&& b1 ==> r == min
196                    &&& !b1 ==> exists|b2: bool|
197                        {
198                            &&& #[trigger] call_ensures(Self::gt, (&self, &max), b2)
199                            &&& b2 ==> r == max
200                            &&& !b2 ==> r == self
201                        }
202                }
203    ;
204}
205
206/// Whether borrowing preserves the ordering of values.
207///
208/// This compares the `cmp_spec` results; faithfulness of the comparison and
209/// borrowing specifications is expressed separately by their `obeys_*_spec` predicates.
210pub open spec fn borrowed_key_ordering_matches<T: Borrow<U> + Ord, U: Ord + ?Sized>() -> bool {
211    forall|x: T, y: T| #[trigger]
212        x.cmp_spec(&y) == <T as BorrowSpec<U>>::borrow_spec(&x).cmp_spec(
213            <T as BorrowSpec<U>>::borrow_spec(&y),
214        )
215}
216
217pub trait PartialEqIs<Rhs: PointeeSized = Self>: PartialEq<Rhs> + PointeeSized {
218    spec fn is_eq(&self, other: &Rhs) -> bool;
219
220    spec fn is_ne(&self, other: &Rhs) -> bool;
221}
222
223pub trait PartialOrdIs<Rhs: PointeeSized = Self>: PartialOrd<Rhs> + PointeeSized {
224    spec fn is_lt(&self, other: &Rhs) -> bool;
225
226    spec fn is_le(&self, other: &Rhs) -> bool;
227
228    spec fn is_gt(&self, other: &Rhs) -> bool;
229
230    spec fn is_ge(&self, other: &Rhs) -> bool;
231}
232
233impl<A: PointeeSized + PartialEq<Rhs>, Rhs: PointeeSized> PartialEqIs<Rhs> for A {
234    #[verifier::inline]
235    open spec fn is_eq(&self, other: &Rhs) -> bool {
236        self.eq_spec(other)
237    }
238
239    #[verifier::inline]
240    open spec fn is_ne(&self, other: &Rhs) -> bool {
241        !self.eq_spec(other)
242    }
243}
244
245impl<A: PointeeSized + PartialOrd<Rhs>, Rhs: PointeeSized> PartialOrdIs<Rhs> for A {
246    #[verifier::inline]
247    open spec fn is_lt(&self, other: &Rhs) -> bool {
248        self.partial_cmp_spec(other) == Some(Ordering::Less)
249    }
250
251    #[verifier::inline]
252    open spec fn is_le(&self, other: &Rhs) -> bool {
253        matches!(self.partial_cmp_spec(other), Some(Ordering::Less | Ordering::Equal))
254    }
255
256    #[verifier::inline]
257    open spec fn is_gt(&self, other: &Rhs) -> bool {
258        self.partial_cmp_spec(other) == Some(Ordering::Greater)
259    }
260
261    #[verifier::inline]
262    open spec fn is_ge(&self, other: &Rhs) -> bool {
263        matches!(self.partial_cmp_spec(other), Some(Ordering::Greater | Ordering::Equal))
264    }
265}
266
267/* min and max */
268
269// `min` and `max` are aliases of `Ord::min` and `Ord::max`.
270pub assume_specification<T: Ord>[ core::cmp::min ](v1: T, v2: T) -> (r: T)
271    ensures
272        call_ensures(T::min, (v1, v2), r),
273;
274
275pub assume_specification<T: Ord>[ core::cmp::max ](v1: T, v2: T) -> (r: T)
276    ensures
277        call_ensures(T::max, (v1, v2), r),
278;
279
280// `min_by` and `max_by` call `compare(&v1, &v2)` once. When it returns `Equal`,
281// `min_by` returns `v1` and `max_by` returns `v2`.
282pub assume_specification<T, F: FnOnce(&T, &T) -> Ordering>[ core::cmp::min_by ](
283    v1: T,
284    v2: T,
285    compare: F,
286) -> (r: T)
287    requires
288        compare.requires((&v1, &v2)),
289    ensures
290        exists|o: Ordering| {
291            &&& #[trigger] compare.ensures((&v1, &v2), o)
292            &&& r == (if o == Ordering::Greater { v2 } else { v1 })
293        },
294;
295
296pub assume_specification<T, F: FnOnce(&T, &T) -> Ordering>[ core::cmp::max_by ](
297    v1: T,
298    v2: T,
299    compare: F,
300) -> (r: T)
301    requires
302        compare.requires((&v1, &v2)),
303    ensures
304        exists|o: Ordering| {
305            &&& #[trigger] compare.ensures((&v1, &v2), o)
306            &&& r == (if o == Ordering::Greater { v1 } else { v2 })
307        },
308;
309
310// `min_by_key` and `max_by_key` compute the keys `f(&v1)` and `f(&v2)` and
311// compare them with `<`. When neither key is less than the other, `min_by_key`
312// returns `v1` and `max_by_key` returns `v2`.
313pub assume_specification<T, F: FnMut(&T) -> K, K: Ord>[ core::cmp::min_by_key ](
314    v1: T,
315    v2: T,
316    f: F,
317) -> (r: T)
318    requires
319        f.requires((&v1,)),
320        f.requires((&v2,)),
321    ensures
322        exists|k1: K, k2: K, lt: bool| {
323            &&& f.ensures((&v1,), k1)
324            &&& f.ensures((&v2,), k2)
325            &&& #[trigger] call_ensures(K::lt, (&k2, &k1), lt)
326            &&& r == (if lt { v2 } else { v1 })
327        },
328;
329
330pub assume_specification<T, F: FnMut(&T) -> K, K: Ord>[ core::cmp::max_by_key ](
331    v1: T,
332    v2: T,
333    f: F,
334) -> (r: T)
335    requires
336        f.requires((&v1,)),
337        f.requires((&v2,)),
338    ensures
339        exists|k1: K, k2: K, lt: bool| {
340            &&& f.ensures((&v1,), k1)
341            &&& f.ensures((&v2,), k2)
342            &&& #[trigger] call_ensures(K::lt, (&k2, &k1), lt)
343            &&& r == (if lt { v1 } else { v2 })
344        },
345;
346
347/* bool */
348
349impl PartialEqSpecImpl for bool {
350    open spec fn obeys_eq_spec() -> bool {
351        true
352    }
353
354    open spec fn eq_spec(&self, other: &bool) -> bool {
355        *self == *other
356    }
357}
358
359pub assume_specification[ <bool as PartialEq<bool>>::eq ](x: &bool, y: &bool) -> bool;
360
361pub assume_specification[ <bool as PartialEq<bool>>::ne ](x: &bool, y: &bool) -> bool;
362
363/* char */
364
365impl PartialEqSpecImpl for char {
366    open spec fn obeys_eq_spec() -> bool {
367        true
368    }
369
370    open spec fn eq_spec(&self, other: &char) -> bool {
371        *self == *other
372    }
373}
374
375impl PartialOrdSpecImpl for char {
376    open spec fn obeys_partial_cmp_spec() -> bool {
377        true
378    }
379
380    open spec fn partial_cmp_spec(&self, other: &char) -> Option<Ordering> {
381        if *self < *other {
382            Some(Ordering::Less)
383        } else if *self > *other {
384            Some(Ordering::Greater)
385        } else {
386            Some(Ordering::Equal)
387        }
388    }
389}
390
391impl OrdSpecImpl for char {
392    open spec fn obeys_cmp_spec() -> bool {
393        true
394    }
395
396    open spec fn cmp_spec(&self, other: &char) -> Ordering {
397        if *self < *other {
398            Ordering::Less
399        } else if *self > *other {
400            Ordering::Greater
401        } else {
402            Ordering::Equal
403        }
404    }
405}
406
407pub assume_specification[ <char as Ord>::cmp ](x: &char, y: &char) -> Ordering;
408
409pub assume_specification[ <char as PartialOrd<char>>::partial_cmp ](x: &char, y: &char) -> Option<
410    Ordering,
411>;
412
413pub assume_specification[ <char as PartialOrd<char>>::lt ](x: &char, y: &char) -> bool;
414
415pub assume_specification[ <char as PartialOrd<char>>::le ](x: &char, y: &char) -> bool;
416
417pub assume_specification[ <char as PartialOrd<char>>::gt ](x: &char, y: &char) -> bool;
418
419pub assume_specification[ <char as PartialOrd<char>>::ge ](x: &char, y: &char) -> bool;
420
421/* floating point */
422
423/*
424// Note: we do not assume that floating point types have obeys_*_spec() == true
425// because Rust floating point operations are not guaranteed to be deterministic.
426// (See https://github.com/rust-lang/rfcs/blob/master/text/3514-float-semantics.md )
427// Instead, we ensure an uninterpreted function about the result,
428// which can be used to trigger user-supplied axioms.
429*/
430
431pub uninterp spec fn eq_ensures<A>(x: A, y: A, o: bool) -> bool;
432
433pub uninterp spec fn ne_ensures<A>(x: A, y: A, o: bool) -> bool;
434
435pub uninterp spec fn partial_cmp_ensures<A>(x: A, y: A, o: Option<Ordering>) -> bool;
436
437pub uninterp spec fn lt_ensures<A>(x: A, y: A, o: bool) -> bool;
438
439pub uninterp spec fn le_ensures<A>(x: A, y: A, o: bool) -> bool;
440
441pub uninterp spec fn gt_ensures<A>(x: A, y: A, o: bool) -> bool;
442
443pub uninterp spec fn ge_ensures<A>(x: A, y: A, o: bool) -> bool;
444
445// Warning: floating-point eq is not the same as spec ==
446pub assume_specification[ <f32 as PartialEq<f32>>::eq ](x: &f32, y: &f32) -> (o: bool)
447    ensures
448        eq_ensures::<f32>(*x, *y, o),
449;
450
451// Warning: floating-point ne is not the same as spec !=
452pub assume_specification[ <f32 as PartialEq<f32>>::ne ](x: &f32, y: &f32) -> (o: bool)
453    ensures
454        ne_ensures::<f32>(*x, *y, o),
455;
456
457pub assume_specification[ <f32 as PartialOrd<f32>>::partial_cmp ](x: &f32, y: &f32) -> (o: Option<Ordering>)
458    ensures
459        partial_cmp_ensures::<f32>(*x, *y, o),
460;
461
462pub assume_specification[ <f32 as PartialOrd<f32>>::lt ](x: &f32, y: &f32) -> (o: bool)
463    ensures
464        lt_ensures::<f32>(*x, *y, o),
465;
466
467pub assume_specification[ <f32 as PartialOrd<f32>>::le ](x: &f32, y: &f32) -> (o: bool)
468    ensures
469        le_ensures::<f32>(*x, *y, o),
470;
471
472pub assume_specification[ <f32 as PartialOrd<f32>>::gt ](x: &f32, y: &f32) -> (o: bool)
473    ensures
474        gt_ensures::<f32>(*x, *y, o),
475;
476
477pub assume_specification[ <f32 as PartialOrd<f32>>::ge ](x: &f32, y: &f32) -> (o: bool)
478    ensures
479        ge_ensures::<f32>(*x, *y, o),
480;
481
482// Warning: floating-point eq is not the same as spec ==
483pub assume_specification[ <f64 as PartialEq<f64>>::eq ](x: &f64, y: &f64) -> (o: bool)
484    ensures
485        eq_ensures::<f64>(*x, *y, o),
486;
487
488// Warning: floating-point ne is not the same as spec !=
489pub assume_specification[ <f64 as PartialEq<f64>>::ne ](x: &f64, y: &f64) -> (o: bool)
490    ensures
491        ne_ensures::<f64>(*x, *y, o),
492;
493
494pub assume_specification[ <f64 as PartialOrd<f64>>::partial_cmp ](x: &f64, y: &f64) -> (o: Option<Ordering>)
495    ensures
496        partial_cmp_ensures::<f64>(*x, *y, o),
497;
498
499pub assume_specification[ <f64 as PartialOrd<f64>>::lt ](x: &f64, y: &f64) -> (o: bool)
500    ensures
501        lt_ensures::<f64>(*x, *y, o),
502;
503
504pub assume_specification[ <f64 as PartialOrd<f64>>::le ](x: &f64, y: &f64) -> (o: bool)
505    ensures
506        le_ensures::<f64>(*x, *y, o),
507;
508
509pub assume_specification[ <f64 as PartialOrd<f64>>::gt ](x: &f64, y: &f64) -> (o: bool)
510    ensures
511        gt_ensures::<f64>(*x, *y, o),
512;
513
514pub assume_specification[ <f64 as PartialOrd<f64>>::ge ](x: &f64, y: &f64) -> (o: bool)
515    ensures
516        ge_ensures::<f64>(*x, *y, o),
517;
518
519/* reference types & */
520
521impl<A: PointeeSized, B: PointeeSized> PartialEqSpecImpl<&B> for &A
522where
523    A: PartialEq<B>,
524{
525    open spec fn obeys_eq_spec() -> bool {
526        <A as PartialEqSpec<B>>::obeys_eq_spec()
527    }
528
529    open spec fn eq_spec(&self, other: &&B) -> bool {
530        <A as PartialEqSpec<B>>::eq_spec(*self, *other)
531    }
532}
533
534pub assume_specification<'_0, 'a, A: PointeeSized, B: PointeeSized>[ <&'a A as PartialEq<&B>>::eq ](
535    a: &&'a A,
536    b: &&B,
537) -> bool
538where
539    A: PartialEq<B>,
540;
541
542pub assume_specification<'_0, 'a, A: PointeeSized, B: PointeeSized>[ <&'a A as PartialEq<&B>>::ne ](
543    a: &&'a A,
544    b: &&B,
545) -> bool
546where
547    A: PartialEq<B>,
548;
549
550impl<A: PointeeSized, B: PointeeSized> PartialOrdSpecImpl<&B> for &A
551where
552    A: PartialOrd<B>,
553{
554    open spec fn obeys_partial_cmp_spec() -> bool {
555        <A as PartialOrdSpec<B>>::obeys_partial_cmp_spec()
556    }
557
558    open spec fn partial_cmp_spec(&self, other: &&B) -> Option<core::cmp::Ordering> {
559        <A as PartialOrdSpec<B>>::partial_cmp_spec(*self, *other)
560    }
561}
562
563pub assume_specification<'_0, 'a, A: PointeeSized, B: PointeeSized>[ <&'a A as PartialOrd<&B>>::partial_cmp ](
564    a: &&'a A,
565    b: &&B,
566) -> Option<core::cmp::Ordering>
567where
568    A: PartialOrd<B>,
569;
570
571pub assume_specification<'_0, 'a, A: PointeeSized, B: PointeeSized>[ <&'a A as PartialOrd<&B>>::lt ](
572    a: &&'a A,
573    b: &&B,
574) -> bool
575where
576    A: PartialOrd<B>,
577;
578
579pub assume_specification<'_0, 'a, A: PointeeSized, B: PointeeSized>[ <&'a A as PartialOrd<&B>>::le ](
580    a: &&'a A,
581    b: &&B,
582) -> bool
583where
584    A: PartialOrd<B>,
585;
586
587pub assume_specification<'_0, 'a, A: PointeeSized, B: PointeeSized>[ <&'a A as PartialOrd<&B>>::gt ](
588    a: &&'a A,
589    b: &&B,
590) -> bool
591where
592    A: PartialOrd<B>,
593;
594
595pub assume_specification<'_0, 'a, A: PointeeSized, B: PointeeSized>[ <&'a A as PartialOrd<&B>>::ge ](
596    a: &&'a A,
597    b: &&B,
598) -> bool
599where
600    A: PartialOrd<B>,
601;
602
603impl<A: PointeeSized + Ord> OrdSpecImpl for &A {
604    open spec fn obeys_cmp_spec() -> bool {
605        A::obeys_cmp_spec()
606    }
607
608    open spec fn cmp_spec(&self, other: &Self) -> core::cmp::Ordering {
609        A::cmp_spec(*self, *other)
610    }
611}
612
613pub assume_specification<'a, A: PointeeSized + Ord>[ <&'a A as Ord>::cmp ](
614    a: &&'a A,
615    b: &&'a A,
616) -> core::cmp::Ordering
617;
618
619} // verus!
620
621/* smart pointers: Box, Rc, Arc */
622
623// Each pointer compares its pointee. Every comparison method it overrides forwards to
624// the same method on `T`, except Rc and Arc's `eq` and `ne` (see below).
625#[cfg(feature = "alloc")]
626macro_rules! smart_ptr_cmp_impl {
627    ($ptr:ident, obeys_eq_spec: { $($obeys_eq:tt)+ }) => {
628        verus! {
629        impl<T: PartialEq + ?Sized, A: Allocator> PartialEqSpecImpl for $ptr<T, A> {
630            open spec fn obeys_eq_spec() -> bool {
631                $($obeys_eq)+
632            }
633
634            open spec fn eq_spec(&self, other: &Self) -> bool {
635                <T as PartialEqSpec>::eq_spec(&**self, &**other)
636            }
637        }
638
639        pub assume_specification<T: PartialEq + ?Sized, A: Allocator>[ <$ptr<T, A> as PartialEq>::eq ](
640            x: &$ptr<T, A>,
641            y: &$ptr<T, A>,
642        ) -> bool;
643
644        pub assume_specification<T: PartialEq + ?Sized, A: Allocator>[ <$ptr<T, A> as PartialEq>::ne ](
645            x: &$ptr<T, A>,
646            y: &$ptr<T, A>,
647        ) -> bool;
648
649        impl<T: PartialOrd + ?Sized, A: Allocator> PartialOrdSpecImpl for $ptr<T, A> {
650            open spec fn obeys_partial_cmp_spec() -> bool {
651                T::obeys_partial_cmp_spec()
652            }
653
654            open spec fn partial_cmp_spec(&self, other: &Self) -> Option<Ordering> {
655                <T as PartialOrdSpec>::partial_cmp_spec(&**self, &**other)
656            }
657        }
658
659        pub assume_specification<T: PartialOrd + ?Sized, A: Allocator>[ <$ptr<T, A> as PartialOrd>::partial_cmp ](
660            x: &$ptr<T, A>,
661            y: &$ptr<T, A>,
662        ) -> Option<Ordering>;
663
664        pub assume_specification<T: PartialOrd + ?Sized, A: Allocator>[ <$ptr<T, A> as PartialOrd>::lt ](
665            x: &$ptr<T, A>,
666            y: &$ptr<T, A>,
667        ) -> bool;
668
669        pub assume_specification<T: PartialOrd + ?Sized, A: Allocator>[ <$ptr<T, A> as PartialOrd>::le ](
670            x: &$ptr<T, A>,
671            y: &$ptr<T, A>,
672        ) -> bool;
673
674        pub assume_specification<T: PartialOrd + ?Sized, A: Allocator>[ <$ptr<T, A> as PartialOrd>::gt ](
675            x: &$ptr<T, A>,
676            y: &$ptr<T, A>,
677        ) -> bool;
678
679        pub assume_specification<T: PartialOrd + ?Sized, A: Allocator>[ <$ptr<T, A> as PartialOrd>::ge ](
680            x: &$ptr<T, A>,
681            y: &$ptr<T, A>,
682        ) -> bool;
683
684        impl<T: Ord + ?Sized, A: Allocator> OrdSpecImpl for $ptr<T, A> {
685            open spec fn obeys_cmp_spec() -> bool {
686                T::obeys_cmp_spec()
687            }
688
689            open spec fn cmp_spec(&self, other: &Self) -> Ordering {
690                <T as OrdSpec>::cmp_spec(&**self, &**other)
691            }
692        }
693
694        pub assume_specification<T: Ord + ?Sized, A: Allocator>[ <$ptr<T, A> as Ord>::cmp ](
695            x: &$ptr<T, A>,
696            y: &$ptr<T, A>,
697        ) -> Ordering;
698        } // verus!
699    };
700}
701
702#[cfg(feature = "alloc")]
703smart_ptr_cmp_impl!(Box, obeys_eq_spec: { T::obeys_eq_spec() });
704
705// With `T: Eq`, std specializes `Rc::eq` and `Arc::eq` to return true for two
706// pointers that share an allocation without calling `T::eq` (and `ne` to return
707// false), so delegating to `T::eq_spec` requires it to be reflexive.
708#[cfg(feature = "alloc")]
709smart_ptr_cmp_impl!(Rc, obeys_eq_spec: {
710    &&& T::obeys_eq_spec()
711    &&& forall|x: &T| #[trigger] x.eq_spec(x)
712});
713
714#[cfg(feature = "alloc")]
715smart_ptr_cmp_impl!(Arc, obeys_eq_spec: {
716    &&& T::obeys_eq_spec()
717    &&& forall|x: &T| #[trigger] x.eq_spec(x)
718});
719
720macro_rules! tuple_cmp_impl {
721    ($($idx:tt $T:ident, )+) => {
722        verus! {
723        impl<$($T: PartialEq + PartialEqSpec),+> PartialEqSpecImpl for ($($T,)+)
724        {
725            open spec fn obeys_eq_spec() -> bool {
726                $(&&& $T::obeys_eq_spec())+
727            }
728
729            open spec fn eq_spec(&self, other: &($($T,)+)) -> bool {
730                $(&&& $T::eq_spec(&self.$idx, &other.$idx ))+
731            }
732        }
733
734        impl<$($T: PartialOrd + PartialOrdSpec),+> PartialOrdSpecImpl for ($($T,)+)
735        {
736            open spec fn obeys_partial_cmp_spec() -> bool {
737                $(&&& $T::obeys_partial_cmp_spec())+
738            }
739
740            open spec fn partial_cmp_spec(&self, other: &($($T,)+)) -> Option<core::cmp::Ordering> {
741                lexical_partial_cmp_spec!($( self.$idx, other.$idx ),+)
742            }
743        }
744
745        impl<$($T: Ord + OrdSpec),+> OrdSpecImpl for ($($T,)+)
746        {
747            open spec fn obeys_cmp_spec() -> bool {
748                $(&&& $T::obeys_cmp_spec())+
749            }
750
751            open spec fn cmp_spec(&self, other: &($($T,)+)) -> core::cmp::Ordering {
752                lexical_cmp_spec!($( self.$idx, other.$idx ),+)
753            }
754        }
755
756        }
757    };
758}
759
760#[allow(unused)]
761macro_rules! lexical_partial_cmp_spec {
762    ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
763        match ($a).partial_cmp_spec(&$b) {
764            Some(core::cmp::Ordering::Equal) => lexical_partial_cmp_spec!($($rest_a, $rest_b),+),
765            ordering => ordering
766        }
767    };
768    ($a:expr, $b:expr) => { ($a).partial_cmp_spec(&$b) };
769}
770
771#[allow(unused)]
772macro_rules! lexical_cmp_spec {
773    ($a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
774        match ($a).cmp_spec(&$b) {
775            core::cmp::Ordering::Equal => lexical_cmp_spec!($($rest_a, $rest_b),+),
776            ordering => ordering
777        }
778    };
779    ($a:expr, $b:expr) => { ($a).cmp_spec(&$b) };
780}
781
782tuple_cmp_impl!(0 T, );
783tuple_cmp_impl!(0 U, 1 T, );
784tuple_cmp_impl!(0 V, 1 U, 2 T, );
785tuple_cmp_impl!(0 W, 1 V, 2 U, 3 T, );
786tuple_cmp_impl!(0 X, 1 W, 2 V, 3 U, 4 T, );
787tuple_cmp_impl!(0 Y, 1 X, 2 W, 3 V, 4 U, 5 T, );
788tuple_cmp_impl!(0 Z, 1 Y, 2 X, 3 W, 4 V, 5 U, 6 T, );
789tuple_cmp_impl!(0 A, 1 Z, 2 Y, 3 X, 4 W, 5 V, 6 U, 7 T, );
790tuple_cmp_impl!(0 B, 1 A, 2 Z, 3 Y, 4 X, 5 W, 6 V, 7 U, 8 T, );
791tuple_cmp_impl!(0 C, 1 B, 2 A, 3 Z, 4 Y, 5 X, 6 W, 7 V, 8 U, 9 T, );
792tuple_cmp_impl!(0 D, 1 C, 2 B, 3 A, 4 Z, 5 Y, 6 X, 7 W, 8 V, 9 U, 10 T, );
793tuple_cmp_impl!(0 E, 1 D, 2 C, 3 B, 4 A, 5 Z, 6 Y, 7 X, 8 W, 9 V, 10 U, 11 T, );