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
use memory::*;
use std::fmt;
use std::mem;
use std::slice;
pub struct FlatBox {
len: usize,
raw_box: *mut [u8]
}
impl FlatBox {
pub fn from_box(b: Box<[u8]>) -> FlatBox {
FlatBox {
len: b.len(),
raw_box: Box::into_raw(b)
}
}
pub fn as_slice<T>(&self) -> &[T] {
unsafe {
slice::from_raw_parts_mut(
self.raw_box as *mut T,
self.len / mem::size_of::<T>()
)
}
}
pub fn as_mut_slice<T>(&mut self) -> &mut [T] {
unsafe {
slice::from_raw_parts_mut(
self.raw_box as *mut T,
self.len / mem::size_of::<T>()
)
}
}
pub fn byte_size(&self) -> usize {
self.len
}
}
impl Drop for FlatBox {
fn drop(&mut self) {
unsafe {
Box::from_raw(self.raw_box);
}
}
}
impl fmt::Debug for FlatBox {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "FlatBox of length {}", &self.len)
}
}
impl IMemory for FlatBox {}