From 5ada1f6b215a74ae7035b80549398b77528d5571 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 3 May 2023 13:18:53 -0600 Subject: [PATCH] hybrid-array: use GATs for `ArraySize` It's quite annoying to have to add a generic parameter to `ArraySize`, particularly when notating it in bounds. GATs provide a solution to this problem, moving the type parameter to the associated type so bounds are simply `N: ArraySize`. --- .github/workflows/hybrid-array.yml | 4 ++-- Cargo.lock | 4 ++-- hybrid-array/Cargo.toml | 4 ++-- hybrid-array/README.md | 2 +- hybrid-array/src/lib.rs | 34 +++++++++++++++--------------- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/.github/workflows/hybrid-array.yml b/.github/workflows/hybrid-array.yml index 46fe7c30..aba9a19e 100644 --- a/.github/workflows/hybrid-array.yml +++ b/.github/workflows/hybrid-array.yml @@ -26,7 +26,7 @@ jobs: strategy: matrix: rust: - - 1.56.0 # MSRV + - 1.65.0 # MSRV - stable target: - armv7a-none-eabi @@ -50,7 +50,7 @@ jobs: strategy: matrix: toolchain: - - 1.56.0 # MSRV + - 1.65.0 # MSRV - stable runs-on: ubuntu-latest steps: diff --git a/Cargo.lock b/Cargo.lock index 3edcd864..85dfebae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,7 +35,7 @@ version = "0.0.2" [[package]] name = "cpufeatures" -version = "0.2.6" +version = "0.2.7" dependencies = [ "libc", ] @@ -94,7 +94,7 @@ version = "0.4.1" [[package]] name = "hybrid-array" -version = "0.1.0" +version = "0.2.0-pre" dependencies = [ "typenum", ] diff --git a/hybrid-array/Cargo.toml b/hybrid-array/Cargo.toml index 9ca231e9..a5729d7f 100644 --- a/hybrid-array/Cargo.toml +++ b/hybrid-array/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hybrid-array" -version = "0.1.0" # Also update html_root_url in lib.rs when bumping this +version = "0.2.0-pre" description = """ Hybrid typenum-based and const generic array types designed to provide the flexibility of typenum-based expressions while also allowing interoperability @@ -14,7 +14,7 @@ categories = ["no-std", "data-structures"] keywords = ["generic-array"] readme = "README.md" edition = "2021" -rust-version = "1.56" +rust-version = "1.65" [dependencies] typenum = "1.16" diff --git a/hybrid-array/README.md b/hybrid-array/README.md index a9db69ef..86093c1c 100644 --- a/hybrid-array/README.md +++ b/hybrid-array/README.md @@ -51,7 +51,7 @@ dual licensed as above, without any additional terms or conditions. [safety-image]: https://img.shields.io/badge/unsafe-forbidden-success.svg [safety-link]: https://github.com/rust-secure-code/safety-dance/ [license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg -[rustc-image]: https://img.shields.io/badge/rustc-1.56+-blue.svg +[rustc-image]: https://img.shields.io/badge/rustc-1.65+-blue.svg [chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg [chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260052-utils diff --git a/hybrid-array/src/lib.rs b/hybrid-array/src/lib.rs index f673ebc7..41ff4a92 100644 --- a/hybrid-array/src/lib.rs +++ b/hybrid-array/src/lib.rs @@ -56,7 +56,7 @@ pub trait ArrayOps: /// [`ArraySize`] type: `typenum`-provided [`Unsigned`] integer. /// /// Not to be confused with [`ArrayOps::SIZE`], which is a `usize`. - type Size: ArraySize; + type Size: ArraySize; /// Returns a reference to the inner array. fn as_array_ref(&self) -> &[T; N]; @@ -106,15 +106,15 @@ pub trait ArrayOps: /// Trait which associates a [`usize`] size and `ArrayType` with a /// `typenum`-provided [`Unsigned`] integer. -pub trait ArraySize: Unsigned { +pub trait ArraySize: Unsigned { /// Array type which corresponds to this size. - type ArrayType: AsRef<[T]> + AsMut<[T]> + IntoArray + Sized; + type ArrayType: AsRef<[T]> + AsMut<[T]> + IntoArray + Sized; } /// Convert the given type into an [`Array`]. pub trait IntoArray { /// Size of the [`Array`]. - type Size: ArraySize; + type Size: ArraySize; /// Convert into the `hybrid-array` crate's [`Array`] type. fn into_hybrid_array(self) -> Array; @@ -157,8 +157,8 @@ macro_rules! impl_array_size { } } - impl ArraySize for typenum::$ty { - type ArrayType = [T; $len]; + impl ArraySize for typenum::$ty { + type ArrayType = [T; $len]; } impl IntoArray for [T; $len] { @@ -290,11 +290,11 @@ impl_array_size! { /// allowing interoperability and a transition path to const generics. #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)] #[repr(transparent)] -pub struct Array>(pub U::ArrayType); +pub struct Array(pub U::ArrayType); impl Array where - U: ArraySize, + U: ArraySize, { /// Returns a slice containing the entire array. Equivalent to `&s[..]`. #[inline] @@ -312,7 +312,7 @@ where impl AsRef<[T; N]> for Array where Self: ArrayOps, - U: ArraySize, + U: ArraySize, { #[inline] fn as_ref(&self) -> &[T; N] { @@ -323,7 +323,7 @@ where impl AsMut<[T; N]> for Array where Self: ArrayOps, - U: ArraySize, + U: ArraySize, { #[inline] fn as_mut(&mut self) -> &mut [T; N] { @@ -334,7 +334,7 @@ where impl Borrow<[T; N]> for Array where Self: ArrayOps, - U: ArraySize, + U: ArraySize, { #[inline] fn borrow(&self) -> &[T; N] { @@ -345,7 +345,7 @@ where impl BorrowMut<[T; N]> for Array where Self: ArrayOps, - U: ArraySize, + U: ArraySize, { #[inline] fn borrow_mut(&mut self) -> &mut [T; N] { @@ -356,7 +356,7 @@ where impl From<[T; N]> for Array where Self: ArrayOps, - U: ArraySize, + U: ArraySize, { #[inline] fn from(arr: [T; N]) -> Array { @@ -367,7 +367,7 @@ where impl Index for Array where [T]: Index, - U: ArraySize, + U: ArraySize, { type Output = <[T] as Index>::Output; @@ -380,7 +380,7 @@ where impl IndexMut for Array where [T]: IndexMut, - U: ArraySize, + U: ArraySize, { #[inline] fn index_mut(&mut self, index: I) -> &mut Self::Output { @@ -391,8 +391,8 @@ where impl<'a, T, U> TryFrom<&'a [T]> for Array where T: Copy, - U: ArraySize, - U::ArrayType: TryFrom<&'a [T], Error = TryFromSliceError>, + U: ArraySize, + U::ArrayType: TryFrom<&'a [T], Error = TryFromSliceError>, { type Error = TryFromSliceError;