Skip to main content

polars_utils/
scratch_vec.rs

1use crate::UnitVec;
2
3/// Vec container with a getter that clears the vec.
4pub struct ScratchVec<T>(Vec<T>);
5
6impl<T> Default for ScratchVec<T> {
7    fn default() -> Self {
8        Self(vec![])
9    }
10}
11
12impl<T> ScratchVec<T> {
13    pub fn with_capacity(capacity: usize) -> Self {
14        Self(Vec::with_capacity(capacity))
15    }
16
17    /// Clear the vec and return a mutable reference to it.
18    pub fn get(&mut self) -> &mut Vec<T> {
19        self.0.clear();
20        &mut self.0
21    }
22}
23
24/// UnitVec container with a getter that clears the unitvec.
25pub struct ScratchUnitVec<T>(UnitVec<T>);
26
27impl<T> Default for ScratchUnitVec<T> {
28    fn default() -> Self {
29        Self(UnitVec::new())
30    }
31}
32
33impl<T> ScratchUnitVec<T> {
34    pub fn with_capacity(capacity: usize) -> Self {
35        Self(UnitVec::with_capacity(capacity))
36    }
37
38    /// Clear the unitvec and return a mutable reference to it.
39    pub fn get(&mut self) -> &mut UnitVec<T> {
40        self.0.clear();
41        &mut self.0
42    }
43}