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

cast to lower-level buffer ref #497

Open
jimy-byerley opened this issue Nov 20, 2019 · 10 comments
Open

cast to lower-level buffer ref #497

jimy-byerley opened this issue Nov 20, 2019 · 10 comments

Comments

@jimy-byerley
Copy link

Would it be possible to add a trait to downcast &[Vector<T>] to &[[T; N]] and &[T] ?
I work with buffers of vectors, and I need to pass them to other libraries, that are using &[T] for compatibility. For now I only copy it element by element, but I think that copy could be avoided with a simple reference cast.

For example, something like that

impl Into<&[T]> for &[Vector3<T>] {
    fn into(self) -> &[T] {
         unsafe { std::slice::from_raw_parts(self.as_ptr() as *const [T], self.len() * 3) }
    }
}

and maybe also the inverse

impl Into<&[Vector<T>]> for &[T] {
    fn into(self) -> &[Vector<T>] {
         assert_eq!(self.len() % 3, 0);
         unsafe { std::slice::from_raw_parts(self.as_ptr() as *const [Vector3<T>], self.len() / 3) }
    }
}

I think it's possible because the Vector struct is marked repr(C) and its fields are all of the same type (so there is no alignment concerns). Plus all slices are contiguous data storages
This kind of cast would get it much more simple to get from a buffer type to an other. (slices, vec, ndarray, with grouped components or not).

@kvark
Copy link
Collaborator

kvark commented Dec 12, 2019

@jimy-byerley
Copy link
Author

Yes, to cast cgmath::Vector to slice, but what about std::Vec<cgmath::Vector> to slice ?
for arrays of cgmath vectors (I think to opengl vector buffers for example).

@mickvangelderen
Copy link
Contributor

I think solving this is out of scope for cgmath. The desire to reinterpret elements of collections is common (rust-lang/rfcs#2756) but, as you can see from the pr, there are quite some things to consider.

That being said, I would not be surprised if there are crates out there that provide these conversions and traits which cgmath could implement. If you find any of those we could look at it.

@vjackson725
Copy link

The Pod trait from bytemuck crate does this. It's quite handy for reinterpreting data for buffers, so it would be great if cgmath could support it.

@kvark
Copy link
Collaborator

kvark commented Dec 22, 2020

@vjackson725 so you are suggesting bytemuck as an optional dependency?
I suppose it could be seen similarly to how cgmath optionally supports mint. Both mint and bytemuck are ways to interop data.

@kaphula
Copy link

kaphula commented Jul 24, 2022

Did the optional bytemuck support ever land to cgmath? I have tricky situation where my struct is used in many places and is defined as such :

#[derive(Clone)]
pub struct Triangle {
    pub vertices: [Vector3<Real>; 3],
    pub normal: [Vector3<Real>; 3],
    pub uv: [Vector2<Real>; 3],
}

If I try to add the typical bytemuck derive options here (Pod and Zeroable) that would allow me to cast Vec<Triangle> types to &[u8], I get an error that Pod and Zeroable are not implemented for Vector3<f64>.

@vjackson725
Copy link

vjackson725 commented Aug 13, 2022

Yes, cgmath now uses bytemuck optionally. Enable the feature "bytemuck" to use it. This looks to have been added in #541.

@matt328
Copy link

matt328 commented Dec 15, 2023

I'm looking to use this feature, but the referenced PR doesn't actually add bytemuck as an available feature in cargo.toml?

@freddycansic
Copy link

@matt328 Me too.

@redmcg
Copy link

redmcg commented Jan 15, 2024

It's not part of an official release yet. You'll need to pull from the master branch. For example:

cargo add cgmath --git https://github.com/rustgd/cgmath.git --features bytemuck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants