Replies: 2 comments
-
For reference, this is the code I'm currently using to simplify my Lua binding code: pub trait MatrixExt: Sized {
fn from_vec(values: Vec<f32>) -> Result<Self, BadSize>;
fn from_iter<I: IntoIterator<Item = f32>>(iter: I) -> Result<Self, BadSize>;
fn as_slice(&self) -> &[f32];
fn as_slice_mut(&mut self) -> &mut [f32];
fn to_vec(&self) -> Vec<f32> {
self.as_slice().to_vec()
}
}
impl MatrixExt for Matrix {
fn from_vec(values: Vec<f32>) -> Result<Self, BadSize> {
if values.len() != 9 {
return Err(BadSize {
expected: 9,
found: values.len(),
});
}
let mut result = Matrix::new_identity();
result.as_slice_mut().copy_from_slice(&values);
Ok(result)
}
fn from_iter<I: IntoIterator<Item = f32>>(iter: I) -> Result<Self, BadSize> {
let values: Vec<f32> = iter.into_iter().take(9).collect();
Self::from_vec(values)
}
fn as_slice(&self) -> &[f32] {
unsafe {
(addr_of!(*self) as *mut [f32; 9])
.as_ref()
.unwrap_unchecked()
}
}
fn as_slice_mut(&mut self) -> &mut [f32] {
unsafe {
(addr_of_mut!(*self) as *mut [f32; 9])
.as_mut()
.unwrap_unchecked()
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hello @Caellian, I don't think this should be added to skia-safe, my reasons are stated here: #930 (comment) Moving over to discussions. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I thing the following methods on
Matrix
andM44
would greatly help with interoperability between Skia matrix types and the rest of the Rust ecosystem (ndarray, bytemuck, glam, nalgebra, ...):Matrix::from_vec(values: Vec<f32>) -> Result<Self, BadSize>;
Matrix::from_iter<I: IntoIterator<Item=f32>>(I) -> Result<Self, BadSize>;
Matrix::to_vec(&self) -> Vec<scalar>
Matrix::as_slice(&self) -> &[scalar]
Matrix::as_slice_mut(&mut self) -> &mut [scalar]
The issue is that the get_9 and set_9 work nicely only for C++, in Rust these are missing out on a lot of stdlib integration such as
std::slice::copy_from_slice
. Which means that assigning values directly is very verbose in most cases.Often times slice size isn't statically known even though it's checked at runtime which means that each of the 9/16 values has to be manually assigned requiring either unsafe blocks or 9/16 implicit checks/jumps for indexing.
Matrix
storestype_mask
, butset_9
already ignores this so I don't think it's that important to infer it if the mx wasn't constructed through a constructor providing that information (i.e.from_iter
).Beta Was this translation helpful? Give feedback.
All reactions