Skip to content

Commit

Permalink
c
Browse files Browse the repository at this point in the history
  • Loading branch information
nameexhaustion committed Sep 6, 2024
1 parent e94ada3 commit 13ea45b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions crates/polars-utils/src/idx_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,21 @@ impl<T> Drop for UnitVec<T> {
impl<T> Clone for UnitVec<T> {
fn clone(&self) -> Self {
unsafe {
let mut me = std::mem::ManuallyDrop::new(Vec::with_capacity(self.len));
let buffer = me.as_mut_ptr();
std::ptr::copy(self.data_ptr(), buffer, self.len);
UnitVec {
data: buffer,
len: self.len,
capacity: NonZeroUsize::new(std::cmp::max(self.len, 1)).unwrap(),
if self.capacity.get() == 1 {
Self {
data: self.data,
len: self.len,
capacity: self.capacity,
}
} else {
let mut me = std::mem::ManuallyDrop::new(Vec::with_capacity(self.len));
let buffer = me.as_mut_ptr();
std::ptr::copy(self.data_ptr(), buffer, self.len);
Self {
data: buffer,
len: self.len,
capacity: NonZeroUsize::new(std::cmp::max(self.len, 1)).unwrap(),
}
}
}
}
Expand Down Expand Up @@ -303,3 +311,11 @@ macro_rules! unitvec {
vec![$($x),+].into()
);
}

mod tests {
#[test]
fn test() {
let v = unitvec![1usize];
assert_eq!(v, v.clone());
}
}

0 comments on commit 13ea45b

Please sign in to comment.