From bf60e0041997298cd40777414cd4bb864bda7ef8 Mon Sep 17 00:00:00 2001 From: Teodor Tanasoaia <28601907+teoxoy@users.noreply.github.com> Date: Sun, 12 Feb 2023 20:41:36 +0100 Subject: [PATCH] increase MSRV to 1.63 (#25) use the stabilized `core::array::from_fn` remove unnecessary `ArrayExt::copy_from` fn --- Cargo.toml | 2 +- src/core/rw.rs | 2 +- src/lib.rs | 1 - src/types/array.rs | 2 +- src/types/matrix.rs | 4 +- src/types/vector.rs | 2 +- src/utils.rs | 111 +++----------------------------------------- 7 files changed, 12 insertions(+), 112 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ed3f985..9673ee9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "encase" version = "0.4.1" edition = "2021" -rust-version = "1.61" +rust-version = "1.63" license = "MIT-0" readme = "./README.md" diff --git a/src/core/rw.rs b/src/core/rw.rs index be8bd31..248df4c 100644 --- a/src/core/rw.rs +++ b/src/core/rw.rs @@ -205,7 +205,7 @@ impl BufferMut for [u8] { fn write(&mut self, offset: usize, val: &[u8; N]) { use crate::utils::SliceExt; - self.copy_from_array(offset, val); + *self.array_mut(offset) = *val; } } diff --git a/src/lib.rs b/src/lib.rs index 59cffb0..a0e5a48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -161,7 +161,6 @@ pub mod private { pub use super::types::runtime_sized_array::{ArrayLength, Length, Truncate}; pub use super::types::vector::*; pub use super::utils::consume_zsts; - pub use super::utils::ArrayExt; pub use super::CalculateSizeFor; pub use super::ShaderSize; pub use super::ShaderType; diff --git a/src/types/array.rs b/src/types/array.rs index 0f35e82..742ee2e 100644 --- a/src/types/array.rs +++ b/src/types/array.rs @@ -88,7 +88,7 @@ where Self: ShaderType, { fn create_from(reader: &mut Reader) -> Self { - crate::utils::ArrayExt::from_fn(|_| { + core::array::from_fn(|_| { let res = CreateFrom::create_from(reader); reader.advance(Self::METADATA.el_padding() as usize); res diff --git a/src/types/matrix.rs b/src/types/matrix.rs index 51146c4..8d2c52f 100644 --- a/src/types/matrix.rs +++ b/src/types/matrix.rs @@ -188,8 +188,8 @@ macro_rules! impl_matrix_inner { $el_ty: $crate::private::MatrixScalar + $crate::private::CreateFrom, { fn create_from(reader: &mut $crate::private::Reader) -> Self { - let columns = $crate::private::ArrayExt::from_fn(|_| { - let col = $crate::private::ArrayExt::from_fn(|_| { + let columns = ::core::array::from_fn(|_| { + let col = ::core::array::from_fn(|_| { $crate::private::CreateFrom::create_from(reader) }); reader.advance(::METADATA.col_padding() as ::core::primitive::usize); diff --git a/src/types/vector.rs b/src/types/vector.rs index f3150da..85daa85 100644 --- a/src/types/vector.rs +++ b/src/types/vector.rs @@ -162,7 +162,7 @@ macro_rules! impl_vector_inner { $el_ty: $crate::private::VectorScalar + $crate::private::CreateFrom, { fn create_from(reader: &mut $crate::private::Reader) -> Self { - let elements = $crate::private::ArrayExt::from_fn(|_| { + let elements = ::core::array::from_fn(|_| { $crate::private::CreateFrom::create_from(reader) }); diff --git a/src/utils.rs b/src/utils.rs index 07e283c..3e9da12 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -93,59 +93,6 @@ impl ByteVecExt for Vec { } } -pub trait ArrayExt { - /// Copies all elements from `src` into `self`, using memcpy. - fn copy_from(&mut self, src: &Self) - where - T: Copy; - - /// Creates an array `[T; N]` where each array element `T` is returned by the `cb` call. - /// - /// # Arguments - /// - /// * `cb`: Callback where the passed argument is the current array index. - fn from_fn(cb: F) -> Self - where - Self: Sized, - F: FnMut(usize) -> T; -} - -impl ArrayExt for [T; N] { - fn copy_from(&mut self, src: &Self) - where - T: Copy, - { - // SAFETY - // 1. src is valid for reads of count * size_of::() bytes - // since it's a shared pointer to an array with count elements - // 2. dst is valid for writes of count * size_of::() bytes - // since it's a mutable pointer to an array with count elements - // 3. Both src and dst are properly aligned - // since they are both pointers to arrays with the same element type T - // 4. The region of memory beginning at src with a size of count * size_of::() bytes - // does not overlap with the region of memory beginning at dst with the same size - // since dst is a mutable reference (therefore exclusive) - unsafe { - std::ptr::copy_nonoverlapping(src.as_ptr(), self.as_mut_ptr(), N); - } - } - - // from rust core lib https://github.com/rust-lang/rust/blob/d5a9bc947617babe3833458f3e09f3d6d5e3d736/library/core/src/array/mod.rs#L41 - // track #![feature(array_from_fn)] (https://github.com/rust-lang/rust/issues/89379) - - fn from_fn(mut cb: F) -> Self - where - F: FnMut(usize) -> T, - { - let mut idx = 0; - [(); N].map(|_| { - let res = cb(idx); - idx += 1; - res - }) - } -} - pub(crate) trait SliceExt { /// Returns a "window" (shared reference to an array of length `N`) into this slice. /// @@ -160,18 +107,12 @@ pub(crate) trait SliceExt { /// /// Panics if the range `offset..offset + N` is out of bounds. fn array_mut(&mut self, offset: usize) -> &mut [T; N]; - - /// Copies all elements from `src` into `self`, using memcpy. - /// - /// # Panics - /// - /// Panics if the range `offset..offset + N` is out of bounds. - fn copy_from_array(&mut self, offset: usize, src: &[T; N]) - where - T: Copy; } impl SliceExt for [T] { + // from rust core lib https://github.com/rust-lang/rust/blob/11d96b59307b1702fffe871bfc2d0145d070881e/library/core/src/slice/mod.rs#L1794 + // track #![feature(split_array)] (https://github.com/rust-lang/rust/issues/90091) + fn array(&self, offset: usize) -> &[T; N] { let src = &self[offset..offset + N]; @@ -180,6 +121,9 @@ impl SliceExt for [T] { unsafe { &*(src.as_ptr() as *const [T; N]) } } + // from rust core lib https://github.com/rust-lang/rust/blob/11d96b59307b1702fffe871bfc2d0145d070881e/library/core/src/slice/mod.rs#L1827 + // track #![feature(split_array)] (https://github.com/rust-lang/rust/issues/90091) + fn array_mut(&mut self, offset: usize) -> &mut [T; N] { let src = &mut self[offset..offset + N]; @@ -187,14 +131,6 @@ impl SliceExt for [T] { // casting to &mut [T; N] is safe since src is a &mut [T] of length N unsafe { &mut *(src.as_mut_ptr() as *mut [T; N]) } } - - fn copy_from_array(&mut self, offset: usize, src: &[T; N]) - where - T: Copy, - { - let dst = self.array_mut(offset); - dst.copy_from(src); - } } #[cfg(test)] @@ -228,30 +164,6 @@ mod byte_vec_ext { } } -#[cfg(test)] -mod array_ext { - use crate::utils::ArrayExt; - - #[test] - fn copy_from() { - let src = [1, 3, 7, 6]; - let mut dst = [0; 4]; - - assert_ne!(src, dst); - - dst.copy_from(&src); - - assert_eq!(src, dst); - } - - #[test] - fn from_fn() { - let arr: [usize; 5] = ArrayExt::from_fn(|i| i); - - assert_eq!(arr, [0, 1, 2, 3, 4]); - } -} - #[cfg(test)] mod slice_ext { use crate::utils::SliceExt; @@ -275,15 +187,4 @@ mod slice_ext { assert_eq!(sub_arr, &mut [6, 9]); } - - #[test] - fn copy_from_array() { - let src = [1, 3, 7]; - let mut arr = [0; 6]; - let slice = arr.as_mut(); - - slice.copy_from_array(3, &src); - - assert_eq!(arr, [0, 0, 0, 1, 3, 7]); - } }