From 9726c64bb241b7f853509baebd3c4eb8138c8301 Mon Sep 17 00:00:00 2001 From: Lohann Paterno Coutinho Ferreira Date: Wed, 29 Jan 2025 19:42:13 -0300 Subject: [PATCH] Basic support to generic array (#204) --- Cargo.toml | 2 ++ src/array.rs | 6 ++++++ src/array/generic_array_impl.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 src/array/generic_array_impl.rs diff --git a/Cargo.toml b/Cargo.toml index f46fc01..ba53b31 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,8 @@ tinyvec_macros = { version = "0.1", optional = true } serde = { version = "1.0", optional = true, default-features = false } # Provides derived `Arbitrary` implementations arbitrary = { version = "1", optional = true } +# Implements the trait `Array` for `GenericArray` struct. +generic-array = { version = "1.1.1", optional = true, default-features = false } [features] default = [] diff --git a/src/array.rs b/src/array.rs index 9a20984..aec4580 100644 --- a/src/array.rs +++ b/src/array.rs @@ -41,8 +41,14 @@ pub trait Array { fn default() -> Self; } +#[cfg(all(feature = "generic-array", not(feature = "rustc_1_55")))] +core::compile_error!("generic-array requires `rustc_1_55` feature"); + #[cfg(feature = "rustc_1_55")] mod const_generic_impl; #[cfg(not(feature = "rustc_1_55"))] mod generated_impl; + +#[cfg(feature = "generic-array")] +mod generic_array_impl; diff --git a/src/array/generic_array_impl.rs b/src/array/generic_array_impl.rs new file mode 100644 index 0000000..383fb83 --- /dev/null +++ b/src/array/generic_array_impl.rs @@ -0,0 +1,26 @@ +use core::default; + +use super::Array; +use generic_array::{ArrayLength, GenericArray}; + +impl Array for GenericArray { + type Item = T; + const CAPACITY: usize = N::USIZE; + + #[inline(always)] + #[must_use] + fn as_slice(&self) -> &[T] { + &*self + } + + #[inline(always)] + #[must_use] + fn as_slice_mut(&mut self) -> &mut [T] { + &mut *self + } + + #[inline(always)] + fn default() -> Self { + ::default() + } +}