forked from cesarb/clear_on_drop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
owned.rs
256 lines (221 loc) · 6.31 KB
/
owned.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use std::fmt;
use std::hash::{Hash,Hasher};
use std::cmp::*;
use std::ops::{Deref, DerefMut};
use std::borrow::{Borrow, BorrowMut};
use clearable::Clearable;
/// Abreviation for `ClearOnDrop` composed with `Owned`
pub type ClearOwnedOnDrop<T> = ::clear_on_drop::ClearOnDrop<T, Owned<T>>;
// where T: Clearable;
/// Abreviation for `ClearOnDrop::new(Owned::new(_))`
#[inline(always)]
pub fn owned_clear_on_drop<T>(t: T) -> ClearOwnedOnDrop<T>
where T: Clearable
{
::clear_on_drop::ClearOnDrop::new(Owned::new(t))
}
/// Wraps an owned value so it masquerades as a reference.
///
/// In Rust, we abstract over types of borrowing using the `Borrow<T>` and
/// `BorrowMut<T>` traits, which cover both `T` as well as references like
/// `&T`. These permit one type to be borrowed in many ways however, so
/// that they can be used more easily as keys for `HashMap`.
/// As a consequence, they cannot provide a cannonical target for our
/// `ClearOnDrop` type. `ToOwned` does not make this cannonical either.
///
/// We use `Deref` and `DerefMut` bounds for `ClearOnDrop` because they do
/// provide a single target for dereferencing, but we cannot ask for
/// `T: Deref<Target = T>`. As a result, `ClearOnDrop` cannot hold an
/// owned type directly. Instead, `Owned<T>` provides a reference type
/// compatable with `ClearOnDrop` that secretly owns its referent without
/// wasting space on real reference.
///
/// In essence, `Owned<T>` provides the caller form of the functionality
/// callees provide using `Borrow<T>` and `BorrowMut<T>`.
///
/// Example
///
/// ```
/// # use clear_on_drop::ClearOnDrop;
/// # use clear_on_drop::Owned;
/// let place: *const u16;
/// {
/// let mut key = ClearOnDrop::new(Owned::new([1,2,3,4,5,6,7]));
/// key[5] = 3;
/// place = &key[0];
/// // This causes the test to fail!
/// // ::std::mem::drop(key);
/// }
/// // Warning removing the above
/// // ::std::mem::drop(key);
/// for i in 0..7 {
/// unsafe { assert_eq!(*place.offset(i), 0); }
/// }
/// ```
///
/// Failed Example
///
/// ```
/// # use clear_on_drop::owned_clear_on_drop;
/// # use std::collections::HashMap;
/// # // use std::ops::Deref;
/// let ptr: *const u64;
/// let mut hm = HashMap::new();
/// hm.insert(13u16,owned_clear_on_drop(69u64));
/// ptr = hm.get_mut(&13u16).unwrap().as_ref();
/// unsafe { assert_eq!(*ptr, 69u64); }
/// ::std::mem::drop(hm);
/// // This causes the test to fail!
/// // unsafe { assert_eq!(*ptr, 0u64); }
/// ```
pub struct Owned<T>(T) where T: ?Sized;
impl<T> Owned<T> where T: Sized {
/// Wrap an owned value so it masquerades as a reference.
pub fn new(t: T) -> Owned<T> { Owned(t) }
}
/*
/// We should not actually be using `Owned<T>: Clearable` anywhere.
/// At present, we do not allow `Owned<T>: Copy` so neither should
/// this be harmful either, except for possibly making some test
/// pass incorrectly.
unsafe impl<T> Clearable for T where T: Clearable {
unsafe fn clear(&mut self) { self.0.clear(); }
}
*/
// --- Implement pointer traits --- //
impl<T> Deref for Owned<T>
where T: ?Sized
{
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for Owned<T>
where T: ?Sized
{
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T> AsRef<T> for Owned<T>
where T: ?Sized
{
#[inline]
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T> AsMut<T> for Owned<T>
where T: ?Sized
{
#[inline]
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> Borrow<T> for Owned<T>
where T: ?Sized
{
#[inline]
fn borrow(&self) -> &T {
&self.0
}
}
impl<T> BorrowMut<T> for Owned<T>
where T: ?Sized
{
#[inline]
fn borrow_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> Default for Owned<T>
where T: ?Sized + Default
{
#[inline]
fn default() -> Self {
Owned(Default::default())
}
}
// --- Delegate derivable traits --- //
impl<T> fmt::Debug for Owned<T>
where T: ?Sized + fmt::Debug
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
{ fmt::Debug::fmt(&self.0, f) }
}
/*
impl<T> Clone for Owned<T>
where T: ?Sized
{
fn clone(&self) -> Self { Owned(self.0.clone()) }
fn clone_from(&mut self, source: &Self) { self.0.clone_from(&source.0); }
}
*/
// impl<T> Copy for Owned<T> where T: Clone + Copy + ?Sized
impl<T> Hash for Owned<T>
where T: ?Sized + Hash
{
fn hash<H: Hasher>(&self, state: &mut H) { self.0.hash(state); }
}
impl<T,R> PartialEq<Owned<R>> for Owned<T>
where T: ?Sized + PartialEq<R>,
R: ?Sized
{
fn eq(&self, other: &Owned<R>) -> bool { self.0.eq(&other.0) }
fn ne(&self, other: &Owned<R>) -> bool { self.0.ne(&other.0) }
}
impl<T> Eq for Owned<T> where T: ?Sized + PartialEq<T> + Eq { }
impl<T,R> PartialOrd<Owned<R>> for Owned<T>
where T: ?Sized + PartialOrd<R>,
R: ?Sized
{
fn partial_cmp(&self, other: &Owned<R>) -> Option<Ordering> { self.0.partial_cmp(&other.0) }
fn lt(&self, other: &Owned<R>) -> bool { self.0.lt(&other.0) }
fn le(&self, other: &Owned<R>) -> bool { self.0.le(&other.0) }
fn gt(&self, other: &Owned<R>) -> bool { self.0.gt(&other.0) }
fn ge(&self, other: &Owned<R>) -> bool { self.0.ge(&other.0) }
}
impl<T> Ord for Owned<T>
where T: ?Sized + Ord
{
fn cmp(&self, other: &Self) -> Ordering { self.0.cmp(&other.0) }
}
#[cfg(test)]
mod tests {
use super::*;
use clear_on_drop::ClearOnDrop;
#[test]
fn owned() {
let place: *const u16;
{
let mut key = ClearOnDrop::new(Owned::new([1,2,3,4,5,6,7]));
key[5] = 3;
place = &key[0];
// This causes the test to fail!
// ::std::mem::drop(key);
}
for i in 0..7 {
unsafe { assert_eq!(*place.offset(i), 0); }
}
}
#[cfg(not(debug_assertions))]
#[test]
fn release_owned() {
let place: *const u16;
{
let mut key = ClearOnDrop::new(Owned::new([1,2,3,4,5,6,7]));
key[5] = 3;
place = &key[0];
// This should at least work in release buids, but does not.
// ::std::mem::drop(key);
}
for i in 0..7 {
unsafe { assert_eq!(*place.offset(i), 0); }
}
}
}