Skip to content

Remove unsafe from array_ref! macro #24

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ description = "Macros to take array references of slices"
license = "BSD-2-Clause"
repository = "https://github.com/droundy/arrayref"
documentation = "https://docs.rs/arrayref"
edition = "2021"

[dev-dependencies]
quickcheck = "0.6"
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,16 @@ macro_rules! array_ref {
($arr:expr, $offset:expr, $len:expr) => {{
{
#[inline]
unsafe fn as_array<T>(slice: &[T]) -> &[T; $len] {
&*(slice.as_ptr() as *const [_; $len])
fn as_array<T>(slice: &[T]) -> &[T; $len] {
slice
.try_into()
.expect("array ref len and offset should be valid for provided array")
}
let offset = $offset;
let slice = & $arr[offset..offset + $len];
#[allow(unused_unsafe)]
unsafe {
as_array(slice)
}
let slice = &$arr[offset..offset + $len];
as_array(slice)
}
}}
}};
}

/// You can use `array_refs` to generate a series of array references
Expand Down