Skip to content
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

hybrid-array: use GATs for ArraySize #893

Merged
merged 1 commit into from
May 3, 2023
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/hybrid-array.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- armv7a-none-eabi
Expand All @@ -50,7 +50,7 @@ jobs:
strategy:
matrix:
toolchain:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions hybrid-array/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
2 changes: 1 addition & 1 deletion hybrid-array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 17 additions & 17 deletions hybrid-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub trait ArrayOps<T, const N: usize>:
/// [`ArraySize`] type: `typenum`-provided [`Unsigned`] integer.
///
/// Not to be confused with [`ArrayOps::SIZE`], which is a `usize`.
type Size: ArraySize<T>;
type Size: ArraySize;

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

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

/// Convert the given type into an [`Array`].
pub trait IntoArray<T> {
/// Size of the [`Array`].
type Size: ArraySize<T>;
type Size: ArraySize;

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

impl<T> ArraySize<T> for typenum::$ty {
type ArrayType = [T; $len];
impl ArraySize for typenum::$ty {
type ArrayType<T> = [T; $len];
}

impl<T> IntoArray<T> for [T; $len] {
Expand Down Expand Up @@ -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<T, U: ArraySize<T>>(pub U::ArrayType);
pub struct Array<T, U: ArraySize>(pub U::ArrayType<T>);

impl<T, U> Array<T, U>
where
U: ArraySize<T>,
U: ArraySize,
{
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
#[inline]
Expand All @@ -312,7 +312,7 @@ where
impl<T, U, const N: usize> AsRef<[T; N]> for Array<T, U>
where
Self: ArrayOps<T, N>,
U: ArraySize<T>,
U: ArraySize,
{
#[inline]
fn as_ref(&self) -> &[T; N] {
Expand All @@ -323,7 +323,7 @@ where
impl<T, U, const N: usize> AsMut<[T; N]> for Array<T, U>
where
Self: ArrayOps<T, N>,
U: ArraySize<T>,
U: ArraySize,
{
#[inline]
fn as_mut(&mut self) -> &mut [T; N] {
Expand All @@ -334,7 +334,7 @@ where
impl<T, U, const N: usize> Borrow<[T; N]> for Array<T, U>
where
Self: ArrayOps<T, N>,
U: ArraySize<T>,
U: ArraySize,
{
#[inline]
fn borrow(&self) -> &[T; N] {
Expand All @@ -345,7 +345,7 @@ where
impl<T, U, const N: usize> BorrowMut<[T; N]> for Array<T, U>
where
Self: ArrayOps<T, N>,
U: ArraySize<T>,
U: ArraySize,
{
#[inline]
fn borrow_mut(&mut self) -> &mut [T; N] {
Expand All @@ -356,7 +356,7 @@ where
impl<T, U, const N: usize> From<[T; N]> for Array<T, U>
where
Self: ArrayOps<T, N>,
U: ArraySize<T>,
U: ArraySize,
{
#[inline]
fn from(arr: [T; N]) -> Array<T, U> {
Expand All @@ -367,7 +367,7 @@ where
impl<T, I, U> Index<I> for Array<T, U>
where
[T]: Index<I>,
U: ArraySize<T>,
U: ArraySize,
{
type Output = <[T] as Index<I>>::Output;

Expand All @@ -380,7 +380,7 @@ where
impl<T, I, U> IndexMut<I> for Array<T, U>
where
[T]: IndexMut<I>,
U: ArraySize<T>,
U: ArraySize,
{
#[inline]
fn index_mut(&mut self, index: I) -> &mut Self::Output {
Expand All @@ -391,8 +391,8 @@ where
impl<'a, T, U> TryFrom<&'a [T]> for Array<T, U>
where
T: Copy,
U: ArraySize<T>,
U::ArrayType: TryFrom<&'a [T], Error = TryFromSliceError>,
U: ArraySize,
U::ArrayType<T>: TryFrom<&'a [T], Error = TryFromSliceError>,
{
type Error = TryFromSliceError;

Expand Down