Skip to content

Commit 1c51db5

Browse files
authored
hybrid-array: use GATs for ArraySize (#893)
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`.
1 parent f0c595a commit 1c51db5

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

.github/workflows/hybrid-array.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
strategy:
2727
matrix:
2828
rust:
29-
- 1.56.0 # MSRV
29+
- 1.65.0 # MSRV
3030
- stable
3131
target:
3232
- armv7a-none-eabi
@@ -50,7 +50,7 @@ jobs:
5050
strategy:
5151
matrix:
5252
toolchain:
53-
- 1.56.0 # MSRV
53+
- 1.65.0 # MSRV
5454
- stable
5555
runs-on: ubuntu-latest
5656
steps:

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hybrid-array/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hybrid-array"
3-
version = "0.1.0" # Also update html_root_url in lib.rs when bumping this
3+
version = "0.2.0-pre"
44
description = """
55
Hybrid typenum-based and const generic array types designed to provide the
66
flexibility of typenum-based expressions while also allowing interoperability
@@ -14,7 +14,7 @@ categories = ["no-std", "data-structures"]
1414
keywords = ["generic-array"]
1515
readme = "README.md"
1616
edition = "2021"
17-
rust-version = "1.56"
17+
rust-version = "1.65"
1818

1919
[dependencies]
2020
typenum = "1.16"

hybrid-array/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ dual licensed as above, without any additional terms or conditions.
5151
[safety-image]: https://img.shields.io/badge/unsafe-forbidden-success.svg
5252
[safety-link]: https://github.com/rust-secure-code/safety-dance/
5353
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
54-
[rustc-image]: https://img.shields.io/badge/rustc-1.56+-blue.svg
54+
[rustc-image]: https://img.shields.io/badge/rustc-1.65+-blue.svg
5555
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
5656
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260052-utils
5757

hybrid-array/src/lib.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub trait ArrayOps<T, const N: usize>:
5656
/// [`ArraySize`] type: `typenum`-provided [`Unsigned`] integer.
5757
///
5858
/// Not to be confused with [`ArrayOps::SIZE`], which is a `usize`.
59-
type Size: ArraySize<T>;
59+
type Size: ArraySize;
6060

6161
/// Returns a reference to the inner array.
6262
fn as_array_ref(&self) -> &[T; N];
@@ -106,15 +106,15 @@ pub trait ArrayOps<T, const N: usize>:
106106

107107
/// Trait which associates a [`usize`] size and `ArrayType` with a
108108
/// `typenum`-provided [`Unsigned`] integer.
109-
pub trait ArraySize<T>: Unsigned {
109+
pub trait ArraySize: Unsigned {
110110
/// Array type which corresponds to this size.
111-
type ArrayType: AsRef<[T]> + AsMut<[T]> + IntoArray<T> + Sized;
111+
type ArrayType<T>: AsRef<[T]> + AsMut<[T]> + IntoArray<T> + Sized;
112112
}
113113

114114
/// Convert the given type into an [`Array`].
115115
pub trait IntoArray<T> {
116116
/// Size of the [`Array`].
117-
type Size: ArraySize<T>;
117+
type Size: ArraySize;
118118

119119
/// Convert into the `hybrid-array` crate's [`Array`] type.
120120
fn into_hybrid_array(self) -> Array<T, Self::Size>;
@@ -157,8 +157,8 @@ macro_rules! impl_array_size {
157157
}
158158
}
159159

160-
impl<T> ArraySize<T> for typenum::$ty {
161-
type ArrayType = [T; $len];
160+
impl ArraySize for typenum::$ty {
161+
type ArrayType<T> = [T; $len];
162162
}
163163

164164
impl<T> IntoArray<T> for [T; $len] {
@@ -290,11 +290,11 @@ impl_array_size! {
290290
/// allowing interoperability and a transition path to const generics.
291291
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
292292
#[repr(transparent)]
293-
pub struct Array<T, U: ArraySize<T>>(pub U::ArrayType);
293+
pub struct Array<T, U: ArraySize>(pub U::ArrayType<T>);
294294

295295
impl<T, U> Array<T, U>
296296
where
297-
U: ArraySize<T>,
297+
U: ArraySize,
298298
{
299299
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
300300
#[inline]
@@ -312,7 +312,7 @@ where
312312
impl<T, U, const N: usize> AsRef<[T; N]> for Array<T, U>
313313
where
314314
Self: ArrayOps<T, N>,
315-
U: ArraySize<T>,
315+
U: ArraySize,
316316
{
317317
#[inline]
318318
fn as_ref(&self) -> &[T; N] {
@@ -323,7 +323,7 @@ where
323323
impl<T, U, const N: usize> AsMut<[T; N]> for Array<T, U>
324324
where
325325
Self: ArrayOps<T, N>,
326-
U: ArraySize<T>,
326+
U: ArraySize,
327327
{
328328
#[inline]
329329
fn as_mut(&mut self) -> &mut [T; N] {
@@ -334,7 +334,7 @@ where
334334
impl<T, U, const N: usize> Borrow<[T; N]> for Array<T, U>
335335
where
336336
Self: ArrayOps<T, N>,
337-
U: ArraySize<T>,
337+
U: ArraySize,
338338
{
339339
#[inline]
340340
fn borrow(&self) -> &[T; N] {
@@ -345,7 +345,7 @@ where
345345
impl<T, U, const N: usize> BorrowMut<[T; N]> for Array<T, U>
346346
where
347347
Self: ArrayOps<T, N>,
348-
U: ArraySize<T>,
348+
U: ArraySize,
349349
{
350350
#[inline]
351351
fn borrow_mut(&mut self) -> &mut [T; N] {
@@ -356,7 +356,7 @@ where
356356
impl<T, U, const N: usize> From<[T; N]> for Array<T, U>
357357
where
358358
Self: ArrayOps<T, N>,
359-
U: ArraySize<T>,
359+
U: ArraySize,
360360
{
361361
#[inline]
362362
fn from(arr: [T; N]) -> Array<T, U> {
@@ -367,7 +367,7 @@ where
367367
impl<T, I, U> Index<I> for Array<T, U>
368368
where
369369
[T]: Index<I>,
370-
U: ArraySize<T>,
370+
U: ArraySize,
371371
{
372372
type Output = <[T] as Index<I>>::Output;
373373

@@ -380,7 +380,7 @@ where
380380
impl<T, I, U> IndexMut<I> for Array<T, U>
381381
where
382382
[T]: IndexMut<I>,
383-
U: ArraySize<T>,
383+
U: ArraySize,
384384
{
385385
#[inline]
386386
fn index_mut(&mut self, index: I) -> &mut Self::Output {
@@ -391,8 +391,8 @@ where
391391
impl<'a, T, U> TryFrom<&'a [T]> for Array<T, U>
392392
where
393393
T: Copy,
394-
U: ArraySize<T>,
395-
U::ArrayType: TryFrom<&'a [T], Error = TryFromSliceError>,
394+
U: ArraySize,
395+
U::ArrayType<T>: TryFrom<&'a [T], Error = TryFromSliceError>,
396396
{
397397
type Error = TryFromSliceError;
398398

0 commit comments

Comments
 (0)