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
use std::ops::Deref;
use std::ops::DerefMut;
use std::ops::Drop;
use std::{ptr, sync::atomic};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SafeVec {
pub inner: Vec<u8>,
}
impl SafeVec {
pub fn new(inner: Vec<u8>) -> SafeVec {
SafeVec { inner: inner }
}
pub fn inner_mut(&mut self) -> &mut Vec<u8> {
&mut self.inner
}
}
impl Drop for SafeVec {
fn drop(&mut self) {
let default = u8::default();
for c in self.inner.as_mut_slice() {
unsafe { ptr::write_volatile(c, default) };
}
atomic::fence(atomic::Ordering::SeqCst);
atomic::compiler_fence(atomic::Ordering::SeqCst);
}
}
impl Deref for SafeVec {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.inner.deref()
}
}
impl DerefMut for SafeVec {
fn deref_mut(&mut self) -> &mut [u8] {
self.inner.deref_mut()
}
}