1#![allow(unused_imports)]
2use super::super::prelude::*;
3
4#[cfg(feature = "alloc")]
5use alloc::borrow::Cow;
6#[cfg(feature = "alloc")]
7use alloc::borrow::ToOwned;
8#[cfg(feature = "alloc")]
9use alloc::boxed::Box;
10#[cfg(feature = "alloc")]
11use alloc::rc::Rc;
12#[cfg(all(feature = "alloc", not(verus_verify_core)))]
13use alloc::string::String;
14#[cfg(feature = "alloc")]
15use alloc::sync::Arc;
16#[cfg(feature = "alloc")]
17use core::alloc::Allocator;
18use core::borrow::Borrow;
19
20verus! {
21
22#[verifier::external_trait_specification]
23#[verifier::external_trait_extension(BorrowSpec via BorrowSpecImpl)]
24pub trait ExBorrow<Borrowed: ?Sized> {
25 type ExternalTraitSpecificationFor: Borrow<Borrowed>;
26
27 spec fn obeys_borrow_spec() -> bool;
28
29 spec fn borrow_spec(&self) -> &Borrowed;
30
31 fn borrow(&self) -> (result: &Borrowed)
32 ensures
33 Self::obeys_borrow_spec() ==> result == self.borrow_spec(),
34 ;
35}
36
37#[cfg(feature = "alloc")]
38#[verifier::external_trait_specification]
39pub(crate) trait ExToOwned {
40 type ExternalTraitSpecificationFor: ToOwned;
41
42 type Owned: Borrow<Self>;
43}
44
45#[cfg(feature = "alloc")]
46#[verifier::external_type_specification]
47#[verifier::reject_recursive_types(B)]
48pub struct ExCow<'a, B: 'a + ?Sized + ToOwned>(Cow<'a, B>);
49
50#[cfg(feature = "alloc")]
53impl<'a, T: View + Clone> View for Cow<'a, T> {
54 type V = T::V;
55
56 open spec fn view(&self) -> Self::V {
57 match self {
58 Cow::Borrowed(borrowed) => borrowed@,
59 Cow::Owned(owned) => owned@,
60 }
61 }
62}
63
64#[cfg(feature = "alloc")]
67impl<'a, T: DeepView + Clone> DeepView for Cow<'a, T> {
68 type V = T::V;
69
70 open spec fn deep_view(&self) -> Self::V {
71 match self {
72 Cow::Borrowed(borrowed) => borrowed.deep_view(),
73 Cow::Owned(owned) => owned.deep_view(),
74 }
75 }
76}
77
78#[cfg(feature = "alloc")]
80impl<'a> View for Cow<'a, str> {
81 type V = Seq<char>;
82
83 open spec fn view(&self) -> Self::V {
84 match self {
85 Cow::Borrowed(borrowed) => borrowed@,
86 Cow::Owned(owned) => owned@,
87 }
88 }
89}
90
91#[cfg(feature = "alloc")]
92impl<'a> DeepView for Cow<'a, str> {
93 type V = Seq<char>;
94
95 open spec fn deep_view(&self) -> Self::V {
96 match self {
97 Cow::Borrowed(borrowed) => borrowed.deep_view(),
98 Cow::Owned(owned) => owned.deep_view(),
99 }
100 }
101}
102
103#[cfg(feature = "alloc")]
104impl<'a, T: View + Clone> View for Cow<'a, [T]> {
105 type V = Seq<T>;
106
107 open spec fn view(&self) -> Self::V {
108 match self {
109 Cow::Borrowed(borrowed) => borrowed@,
110 Cow::Owned(owned) => owned@,
111 }
112 }
113}
114
115#[cfg(feature = "alloc")]
116impl<'a, T: DeepView + Clone> DeepView for Cow<'a, [T]> {
117 type V = Seq<T::V>;
118
119 open spec fn deep_view(&self) -> Self::V {
120 match self {
121 Cow::Borrowed(borrowed) => borrowed.deep_view(),
122 Cow::Owned(owned) => owned.deep_view(),
123 }
124 }
125}
126
127impl<T: ?Sized> BorrowSpecImpl<T> for T {
128 open spec fn obeys_borrow_spec() -> bool {
129 true
130 }
131
132 open spec fn borrow_spec(&self) -> &T {
133 self
134 }
135}
136
137impl<T: ?Sized> BorrowSpecImpl<T> for &T {
138 open spec fn obeys_borrow_spec() -> bool {
139 true
140 }
141
142 open spec fn borrow_spec(&self) -> &T {
143 *self
144 }
145}
146
147#[cfg(feature = "alloc")]
148impl<T: ?Sized, A: Allocator> BorrowSpecImpl<T> for Box<T, A> {
149 open spec fn obeys_borrow_spec() -> bool {
150 true
151 }
152
153 open spec fn borrow_spec(&self) -> &T {
154 &**self
155 }
156}
157
158#[cfg(feature = "alloc")]
159impl<T: ?Sized, A: Allocator> BorrowSpecImpl<T> for Rc<T, A> {
160 open spec fn obeys_borrow_spec() -> bool {
161 true
162 }
163
164 open spec fn borrow_spec(&self) -> &T {
165 &**self
166 }
167}
168
169#[cfg(feature = "alloc")]
170impl<T: ?Sized, A: Allocator> BorrowSpecImpl<T> for Arc<T, A> {
171 open spec fn obeys_borrow_spec() -> bool {
172 true
173 }
174
175 open spec fn borrow_spec(&self) -> &T {
176 &**self
177 }
178}
179
180#[cfg(all(feature = "alloc", not(verus_verify_core)))]
181impl BorrowSpecImpl<str> for String {
182 open spec fn obeys_borrow_spec() -> bool {
183 true
184 }
185
186 uninterp spec fn borrow_spec(&self) -> &str;
187}
188
189#[cfg(all(feature = "alloc", not(verus_verify_core)))]
190pub broadcast axiom fn axiom_string_borrow(s: &String)
191 ensures
192 #[trigger] <String as BorrowSpec<str>>::borrow_spec(s)@ == s@,
193;
194
195pub broadcast group group_borrow_axioms {
196 #[cfg(all(feature = "alloc", not(verus_verify_core)))]
197 axiom_string_borrow,
198}
199
200}