Skip to main content

polars_utils/
scratch_vec.rs

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