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

Type for an array of dynamic scalar type? #985

Closed
drewm1980 opened this issue Apr 19, 2021 · 3 comments
Closed

Type for an array of dynamic scalar type? #985

drewm1980 opened this issue Apr 19, 2021 · 3 comments
Labels

Comments

@drewm1980
Copy link

I am working on code that deserializes data that originated from numpy (but is in a nonstandard serialized format), and could have any of the supported scalar types from numpy (but in practice I'll only support rust native numeric types).

Every type I've found in the API is (compile-time) generic over the contained scalar type (f32,f64,...), but I need a type where the scalar type is dynamic for the return value of my deserializer.

Is this already supported by the ndarray crate? An option is to roll my own enum around ndarray's concrete types.

For example, the image library has this as the return type of their deserializer: https://docs.rs/image/0.23.14/image/enum.DynamicImage.html

@jturner314
Copy link
Member

jturner314 commented Apr 19, 2021

Yeah, ndarray's ArrayBase type is always generic over the element type.

An option is to roll my own enum around ndarray's concrete types.

That's what I'd suggest if you need dynamic element types. I'm a little surprised that the numpy crate doesn't provide a type like this. It looks like the array type in the API they expose is always generic over the element type.

Another route would be to box ndarray::Arrays into Box<dyn std::any::Any> (and expect the user to downcast the array). Personally, I avoid Any, though.

If possible, I'd suggest making your deserializer generic over the element type instead of using some form of dynamic element types. That's what I've done in the ndarray-npy crate, for example.

@bluss bluss added the question label Apr 20, 2021
@bluss
Copy link
Member

bluss commented Apr 20, 2021

A type-erased array makes sense - or an enum around an array like DynamicImage - but I don't think it fits inside this crate for now. So I really mean that it makes sense to use but we won't provide it right now - in particular, let the types you use/allow be guided by your use case in your own implementation of the idea. I've written a type erased array wrapper before, but it seems quite tricky to get right.

This would be something we possibly would attempt after #879 and #339 are done, which is pretty far future right now.

@drewm1980
Copy link
Author

Thanks for the responses! Indeed, I can make at least ~some simplifying assumptions, and it's good to know I'm on the right track with the enum approach. The data structures I'm deserializing can be arbitrarily nested and contain multiple arrays; making the whole serializer generic of over the inner types doesn't seem like a good fit; there are too many types and I don't control them all.

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

No branches or pull requests

3 participants