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

Obtaining &[T; N] for GenericArray<T, N> #52

Closed
tarcieri opened this issue Nov 25, 2017 · 5 comments
Closed

Obtaining &[T; N] for GenericArray<T, N> #52

tarcieri opened this issue Nov 25, 2017 · 5 comments

Comments

@tarcieri
Copy link

Apologies if there's already a way to do this, but I can't figure it out.

I'd like to pass a generic array as a sized array reference. I'm able to do this using e.g. the arrayref crate but I'd like to avoid that if possible (among other things by virtue of using a macro that expands to unsafe code I'm unable to deny(unsafe_code).

@fizyk20
Copy link
Owner

fizyk20 commented Dec 14, 2017

GenericArray already implements Deref<Target = [T]>. I don't think obtaining exactly &[T; N] is possible, at least not without implementing this separately for every N. So the only way here is probably a macro :(

@tarcieri
Copy link
Author

It would need something similar to this:

https://github.com/fizyk20/generic-array/blob/master/src/impls.rs#L138

But perhaps implementing AsRef and AsMut for the sized array types. That said, these traits are already implemented for the slice types, so implementing them for both might be ambiguous and annoying in places.

@tarcieri
Copy link
Author

At some point, I guess it might be worth giving up and waiting for const generics 😜

@fizyk20
Copy link
Owner

fizyk20 commented Feb 18, 2018

I had another look at the problem (sorry for the delay!) - implementing AsRef and AsMut for various array sizes would be possible, but as you say, it might be quite problematic - I can imagine that the compiler could sometimes have problems inferring whether you want a &[T; x] or &[T] as a result, which would require tedious annotations (AsRef::<[T; x]>::as_ref(array) or something). I'm not sure if this is a good idea.

Or maybe it's not as bad as I think? Are there reasonable situations in which this would be ambiguous? I'm not sure...

@tarcieri
Copy link
Author

Yeah, I have not had good luck with having impls of both AsRef<[T]> and AsRef<[T; x]> for the reasons you just described, and as AsRef<[T]> is already provided perhaps it's best if I continue to use arrayref as needed.

Will go ahead and close this out then.

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

2 participants