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
use std::mem::{align_of, size_of};
#[cfg(feature = "smallvec")]
use smallvec::SmallVec;
pub fn repurpose_allocation<T1, T2>(mut v: Vec<T1>) -> Vec<T2> {
assert_eq!(size_of::<T1>(), size_of::<T2>(), "same size");
assert_eq!(align_of::<T1>(), align_of::<T2>(), "same alignment");
v.clear();
let cap = v.capacity();
let p = v.as_mut_ptr().cast();
std::mem::forget(v);
unsafe { Vec::from_raw_parts(p, 0, cap) }
}
pub trait Vector<T> {
fn push(&mut self, value: T);
fn extend_from_slice(&mut self, other: &[T])
where
T: Copy;
}
impl<T> Vector<T> for Vec<T> {
fn push(&mut self, value: T) {
Vec::push(self, value)
}
fn extend_from_slice(&mut self, other: &[T])
where
T: Copy,
{
Vec::extend_from_slice(self, other)
}
}
#[cfg(feature = "smallvec")]
impl<A> Vector<A::Item> for SmallVec<A>
where
A: smallvec::Array,
{
fn push(&mut self, value: A::Item) {
SmallVec::push(self, value)
}
fn extend_from_slice(&mut self, other: &[A::Item])
where
A::Item: Copy,
{
SmallVec::extend_from_slice(self, other)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn miri_test_repurpose() {
let v: Vec<usize> = vec![0, 1, 2];
let mut other: Vec<isize> = repurpose_allocation(v);
assert!(other.is_empty());
other.push(-1);
assert_eq!(other[0], -1);
struct Gus1 {
s: String,
}
impl Drop for Gus1 {
fn drop(&mut self) {
println!("happy {}", self.s);
}
}
struct Gus2 {
s: String,
}
impl Drop for Gus2 {
fn drop(&mut self) {
println!("happy {}", self.s);
}
}
let v: Vec<Gus1> = vec![Gus1 {
s: "hmm".to_string(),
}];
let mut other: Vec<Gus2> = repurpose_allocation(v);
assert!(other.is_empty());
other.push(Gus2 {
s: "hmm2".to_string(),
});
assert_eq!(other[0].s, "hmm2");
}
#[test]
#[should_panic(expected = "same size")]
fn miri_test_wrong_size() {
let v: Vec<usize> = vec![0, 1, 2];
let _: Vec<()> = repurpose_allocation(v);
}
#[test]
#[should_panic(expected = "same alignment")]
fn miri_test_wrong_align() {
#[repr(align(8))]
#[derive(Default)]
struct Gus1 {
_i: [u8; 16],
}
#[repr(align(16))]
#[derive(Default)]
struct Gus2 {
_i: [u8; 8],
}
use std::mem::size_of;
assert_eq!(size_of::<Gus1>(), size_of::<Gus2>(), "same size in test");
let v: Vec<Gus1> = vec![Default::default()];
let _: Vec<Gus2> = repurpose_allocation(v);
}
}