Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust): Fix UnitVec inline clone and with_capacity #18586

Merged
merged 6 commits into from
Sep 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 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 {
nameexhaustion marked this conversation as resolved.
Show resolved Hide resolved
data: self.data,
len: self.len,
capacity: self.capacity,
}
} else {
let mut me = std::mem::ManuallyDrop::new(Vec::with_capacity(self.len));
orlp marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -295,11 +303,20 @@ macro_rules! unitvec {
);
($elem:expr) => (
{let mut new = $crate::idx_vec::UnitVec::new();
let v = $elem;
// SAFETY: first element always fits.
unsafe { new.push_unchecked($elem) };
unsafe { new.push_unchecked(v) };
new}
);
($($x:expr),+ $(,)?) => (
vec![$($x),+].into()
);
}

mod tests {
#[test]
fn test_unitvec() {
let v = unitvec![1usize];
orlp marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(v, v.clone());
}
}
Loading