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

Implementation without unsafe is possible #18

Open
Shnatsel opened this issue Nov 2, 2019 · 6 comments
Open

Implementation without unsafe is possible #18

Shnatsel opened this issue Nov 2, 2019 · 6 comments

Comments

@Shnatsel
Copy link

Shnatsel commented Nov 2, 2019

It is now possible to implement this macro safely thanks to TryInto implementation from slices to array references:

let my_arr_reference: &[i32; 5] = my_slice.try_into().unwrap();

Bounds checks will also be elided if the size of the slice is known in advance.

Example of this conversion in action: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=9d6e1d61835060f832ce1724becb1214

The only limitation is that this only works for sizes of 32 and lower until const generics are stabilized.

@droundy
Copy link
Owner

droundy commented Nov 2, 2019

Nice. I don't think I'll make a change until it works for large as well as small arrays.

@aticu
Copy link

aticu commented Jul 27, 2020

Since rust-lang/rust#74060 now stabilizes trait implementations for all array lengths, this will be possible once 1.47 is released (which should happen 2020-10-08 if I didn't miscount).

@droundy
Copy link
Owner

droundy commented Jul 27, 2020

Okay, I'll accept a pull request changing the implementation to avoid unsafe.

@burdges
Copy link
Contributor

burdges commented Jul 27, 2020

I suppose slices' size would be known in advance if you do the more subtle tricks?

@coderbot16
Copy link

Looks like Rust 1.47 has been stable for a few weeks now! 🎉 https://blog.rust-lang.org/2020/10/08/Rust-1.47.html

@nabilwadih
Copy link

I raised this PR which will avoid unsafe in the array_ref! macro: #24

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

6 participants