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
use crate::{Datum, Row};
#[derive(Debug)]
pub struct DatumVec {
outer: Vec<Datum<'static>>,
}
impl DatumVec {
pub fn new() -> Self {
Self { outer: Vec::new() }
}
pub fn borrow<'a>(&'a mut self) -> DatumVecBorrow<'a> {
let inner = std::mem::take(&mut self.outer);
DatumVecBorrow {
outer: &mut self.outer,
inner,
}
}
pub fn borrow_with<'a>(&'a mut self, row: &'a Row) -> DatumVecBorrow<'a> {
let mut borrow = self.borrow();
borrow.extend(row.iter());
borrow
}
pub fn borrow_with_many<'a, 'b, D: ::std::borrow::Borrow<Row> + 'a>(
&'a mut self,
rows: &'b [&'a D],
) -> DatumVecBorrow<'a> {
let mut borrow = self.borrow();
for row in rows {
borrow.extend(row.borrow().iter());
}
borrow
}
}
#[derive(Debug)]
pub struct DatumVecBorrow<'outer> {
outer: &'outer mut Vec<Datum<'static>>,
inner: Vec<Datum<'outer>>,
}
impl<'outer> Drop for DatumVecBorrow<'outer> {
fn drop(&mut self) {
*self.outer = ore::vec::repurpose_allocation(std::mem::take(&mut self.inner));
}
}
impl<'outer> std::ops::Deref for DatumVecBorrow<'outer> {
type Target = Vec<Datum<'outer>>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<'outer> std::ops::DerefMut for DatumVecBorrow<'outer> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn miri_test_datum_vec() {
let mut d = DatumVec::new();
assert_eq!(d.borrow().len(), 0);
let mut r = Row::with_capacity(10);
r.push(Datum::String("first"));
r.push(Datum::Dummy);
{
let borrow = d.borrow_with(&r);
assert_eq!(borrow.len(), 2);
assert_eq!(borrow[0], Datum::String("first"));
}
{
let mut r2 = Row::with_capacity(1);
r2.push(Datum::String("second"));
let borrow = d.borrow_with_many(&[&r, &r2]);
assert_eq!(borrow.len(), 3);
assert_eq!(borrow[2], Datum::String("second"));
}
}
}