Last active
March 20, 2026 15:07
-
-
Save awxkee/e5871e4a068309a3472956aa69422780 to your computer and use it in GitHub Desktop.
autoalign.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pub struct AutoAligner<T: Sized, const N: usize> { | |
| pub width: usize, | |
| pub height: usize, | |
| pub data: Vec<T>, | |
| } | |
| impl<T: Sized + Default + Clone + Copy, const N: usize> AutoAligner<T, N> { | |
| pub fn new(width: usize, height: usize, data: Vec<T>) -> AutoAligner<T, N> { | |
| let row_size = width * N * size_of::<T>(); | |
| if row_size % 4 == 0 { | |
| AutoAligner { | |
| width, | |
| height, | |
| data, | |
| } | |
| } else { | |
| let element_size = N * size_of::<T>(); | |
| let mut extra = 1; | |
| while (row_size + extra * element_size) % 4 != 0 { | |
| extra += 1; | |
| } | |
| let new_width = width + extra; | |
| let mut new_data = vec![T::default(); new_width * N * height]; | |
| for (dst_row, src_row) in new_data | |
| .chunks_exact_mut(new_width * N) | |
| .zip(data.chunks_exact(width * N)) | |
| { | |
| dst_row[..width * N].copy_from_slice(&src_row[..width * N]); | |
| } | |
| AutoAligner { | |
| width, | |
| height, | |
| data: new_data, | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment