Skip to main content

vstd_extra/external/
slice.rs

1use core::ops::Range;
2use vstd::prelude::*;
3
4verus! {
5
6/// Rust 1.100.0, library/core/src/convert/mod.rs: AsMut<[T]> for [T]
7/// returns `self`. The reborrow preserves the entire slice on return and
8/// propagates all subsequent mutations back to the receiver without panicking.
9pub assume_specification<T>[ <[T] as core::convert::AsMut<[T]>>::as_mut ](slice: &mut [T]) -> (ret:
10    &mut [T])
11    ensures
12        ret@ == old(slice)@,
13        final(slice)@ == final(ret)@,
14    opens_invariants none
15    no_unwind
16;
17
18#[verifier::when_used_as_spec(as_ptr_spec)]
19pub assume_specification<T>[ <[T]>::as_ptr ](s: &[T]) -> (r: *const T)
20    ensures
21        r == as_ptr_spec(s),
22    no_unwind
23;
24
25pub uninterp spec fn as_ptr_spec<T>(s: &[T]) -> *const T;
26
27pub uninterp spec fn as_mut_ptr_spec<T>(s: &mut [T]) -> *mut T;
28
29pub assume_specification<T>[ <[T]>::as_mut_ptr ](s: &mut [T]) -> (r: *mut T)
30    ensures
31        r == as_mut_ptr_spec(s),
32    no_unwind
33;
34
35/// AXIOM: A slice's one-past-the-end byte address fits in `usize`.
36///
37/// Rust guarantees that any valid slice's memory range lies within the
38/// addressable address space, so `as_ptr(s) as usize + s.len() * size_of::<T>()`
39/// never overflows. We package this as a `usize`-level fact for `T = u8` (the
40/// common case for byte-slice plumbing); other element types can use it after
41/// multiplying by `size_of::<T>()`.
42pub broadcast axiom fn axiom_slice_addr_no_overflow(s: &[u8])
43    ensures
44        #[trigger] (as_ptr_spec(s) as usize) + s.len() <= usize::MAX,
45;
46
47/// Spec for `core::slice::from_raw_parts`. We promise only that the resulting
48/// slice has the requested length and that the call does not unwind.
49/// The caller must uphold Rust's safety contract.
50pub assume_specification<'a, T>[ core::slice::from_raw_parts::<'a, T> ](
51    data: *const T,
52    len: usize,
53) -> (ret: &'a [T])
54    ensures
55        ret.len() == len,
56    no_unwind
57;
58
59/// Spec for `core::slice::from_raw_parts_mut`. As above.
60pub assume_specification<'a, T>[ core::slice::from_raw_parts_mut::<'a, T> ](
61    data: *mut T,
62    len: usize,
63) -> (ret: &'a mut [T])
64    ensures
65        ret.len() == len,
66    no_unwind
67;
68
69/// Fills every element with a clone of `value`; the length is unchanged.
70///
71/// Fills in place with no allocation, so there is no input-dependent panic; a panicking
72/// `T::clone` is assumed away by `cloned`.
73pub assume_specification<T: Clone>[ <[T]>::fill ](s: &mut [T], value: T)
74    ensures
75        final(s)@.len() == old(s)@.len(),
76        forall|i: int|
77            #![trigger final(s)@[i]]
78            0 <= i < final(s)@.len() ==> cloned::<T>(value, final(s)@[i]),
79;
80
81} // verus!