From 451dc53a752d219766d6628199fd62cd1f39706a Mon Sep 17 00:00:00 2001 From: Nabil Wadih Date: Fri, 13 Jan 2023 21:29:38 -0800 Subject: [PATCH 1/2] remove unsafe code from array_ref macro --- src/lib.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4c4b418..0bb7be7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,17 +58,16 @@ macro_rules! array_ref { ($arr:expr, $offset:expr, $len:expr) => {{ { #[inline] - unsafe fn as_array(slice: &[T]) -> &[T; $len] { - &*(slice.as_ptr() as *const [_; $len]) + fn as_array(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 From cbaba57857d7392c36458dd1f2ed3c83235e6fb1 Mon Sep 17 00:00:00 2001 From: Nabil Wadih Date: Fri, 13 Jan 2023 21:53:04 -0800 Subject: [PATCH 2/2] update edition to 2021 --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index 6d92097..aa267a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"