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

feat: introduce a utility function for converting Vec<T> to Vec<u8> #2898

Closed
Closed
Changes from all commits
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
26 changes: 25 additions & 1 deletion rust/lance-encoding/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,28 @@
impl From<Buffer> for LanceBuffer {
fn from(buffer: Buffer) -> Self {
Self::Borrowed(buffer)
}

Check warning on line 316 in rust/lance-encoding/src/buffer.rs

View workflow job for this annotation

GitHub Actions / linux-arm

Diff in /runner/_work/lance/lance/rust/lance-encoding/src/buffer.rs
}

pub fn transmute_to_bytes_vec<T>(mut from: Vec<T>) -> Vec<u8>
where
T: Copy,
{
unsafe {
let element_size = size_of::<T>();
let capacity = from.capacity() * element_size;
let len = from.len() * element_size;
let ptr = from.as_mut_ptr();
std::mem::forget(from);
Vec::from_raw_parts(ptr as *mut u8, len, capacity)
}
}

#[cfg(test)]
mod tests {
use arrow_buffer::Buffer;

use super::LanceBuffer;
use super::{transmute_to_bytes_vec, LanceBuffer};

#[test]
fn test_eq() {
Expand Down Expand Up @@ -395,4 +409,14 @@
let buf = LanceBuffer::Owned(vec![1, 2, 15, 20]);
assert_eq!("01020F14", buf.as_hex());
}

#[test]
fn test_transmute_to_bytes_vec_u16() {
let vec: Vec<u16> = vec![1, 2, 3];
let bytes = transmute_to_bytes_vec(vec);
// for this test to pass, the endianness of the machine must be little-endian,
// should I include a panic in `transmute_to_bytes_vec` when the endianness is not little-endian?

Check warning on line 418 in rust/lance-encoding/src/buffer.rs

View workflow job for this annotation

GitHub Actions / linux-arm

Diff in /runner/_work/lance/lance/rust/lance-encoding/src/buffer.rs
assert_eq!(bytes, vec![1, 0, 2, 0, 3, 0]);
}

}
Loading