Skip to content

Instantly share code, notes, and snippets.

@dakom
Created June 16, 2019 09:51
Show Gist options
  • Save dakom/db346941eba19434e388815a583afd7f to your computer and use it in GitHub Desktop.
Save dakom/db346941eba19434e388815a583afd7f to your computer and use it in GitHub Desktop.
Rust slice helpers
//since memory is aligned contiguously we can use the values() function as-is
#[repr(C)]
#[derive(Copy, Clone, PartialEq)]
pub struct Point {
pub x:f64,
pub y:f64,
pub z:f64
}
impl SliceValues for Point {}
pub trait SliceValues {
fn values(self:&Self) -> &[f64] {
let pointer = self as *const Self as *const f64;
let slice: &[f64] = unsafe { std::slice::from_raw_parts(pointer, 4) };
slice
}
}
//not really related to the above, but handy
pub fn copy_slices_to_slice<T>(srcs:&[Vec<T>], dest:&mut [T])
where T: Copy
{
let mut offset = 0;
for src in srcs.iter() {
let len = src.len();
let max = offset + len;
dest[offset..max].copy_from_slice(&src);
offset = max;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment