diff --git a/.github/workflows/hybrid-array.yml b/.github/workflows/hybrid-array.yml deleted file mode 100644 index 0eb6b591..00000000 --- a/.github/workflows/hybrid-array.yml +++ /dev/null @@ -1,63 +0,0 @@ -name: hybrid-array - -on: - pull_request: - paths: - - ".github/workflows/hybrid-array.yml" - - "hybrid-array/**" - - "Cargo.*" - push: - branches: master - -permissions: - contents: read - -defaults: - run: - working-directory: hybrid-array - -env: - CARGO_INCREMENTAL: 0 - RUSTFLAGS: "-Dwarnings" - -jobs: - build: - runs-on: ubuntu-latest - strategy: - matrix: - rust: - - 1.65.0 # MSRV - - stable - target: - - armv7a-none-eabi - - thumbv7em-none-eabi - - wasm32-unknown-unknown - steps: - - uses: actions/checkout@v4 - - uses: RustCrypto/actions/cargo-cache@master - - uses: dtolnay/rust-toolchain@master - with: - toolchain: ${{ matrix.rust }} - targets: ${{ matrix.target }} - - run: cargo build --no-default-features --target ${{ matrix.target }} - - minimal-versions: - uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master - with: - working-directory: ${{ github.workflow }} - - test: - strategy: - matrix: - toolchain: - - 1.65.0 # MSRV - - stable - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: RustCrypto/actions/cargo-cache@master - - uses: dtolnay/rust-toolchain@master - with: - toolchain: ${{ matrix.toolchain }} - - run: cargo test - - run: cargo test --all-features diff --git a/Cargo.lock b/Cargo.lock index 5d5bb5ab..f510dfce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,7 +22,7 @@ dependencies = [ name = "block-padding" version = "0.4.0-pre.4" dependencies = [ - "hybrid-array 0.2.0-rc.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hybrid-array", ] [[package]] @@ -46,14 +46,14 @@ version = "0.2.0-pre.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "806e4e3731d44f1340b069551225b44c2056c105cad9e67f0c46266db8a3a6b9" dependencies = [ - "hybrid-array 0.2.0-rc.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hybrid-array", ] [[package]] name = "dbl" version = "0.4.0-pre.4" dependencies = [ - "hybrid-array 0.2.0-rc.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hybrid-array", ] [[package]] @@ -82,14 +82,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" -[[package]] -name = "hybrid-array" -version = "0.2.0-rc.0" -dependencies = [ - "typenum", - "zeroize 1.7.0", -] - [[package]] name = "hybrid-array" version = "0.2.0-rc.0" @@ -104,7 +96,7 @@ name = "inout" version = "0.2.0-pre.4" dependencies = [ "block-padding", - "hybrid-array 0.2.0-rc.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hybrid-array", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index b376e6ac..05ba29e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,6 @@ members = [ "dbl", "fiat-constify", "hex-literal", - "hybrid-array", "inout", "opaque-debug", "wycheproof2blb", diff --git a/hybrid-array/CHANGELOG.md b/hybrid-array/CHANGELOG.md deleted file mode 100644 index 1bb9a9c4..00000000 --- a/hybrid-array/CHANGELOG.md +++ /dev/null @@ -1,13 +0,0 @@ -# Changelog - -All notable changes to this project will be documented in this file. - -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - -## Pending - -* Implement `Zeroize` for `Array`, and `ZeroizeOnDrop` for `Array` - -## 0.1.0 (2022-05-07) -- Initial release diff --git a/hybrid-array/Cargo.toml b/hybrid-array/Cargo.toml deleted file mode 100644 index a5248e8b..00000000 --- a/hybrid-array/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "hybrid-array" -version = "0.2.0-rc.0" -description = """ -Hybrid typenum-based and const generic array types designed to provide the -flexibility of typenum-based expressions while also allowing interoperability -and a transition path to const generics -""" -authors = ["RustCrypto Developers"] -license = "MIT OR Apache-2.0" -documentation = "https://docs.rs/hybrid-array" -repository = "https://github.com/RustCrypto/utils/tree/master/hybrid-array" -categories = ["no-std", "data-structures"] -keywords = ["generic-array"] -readme = "README.md" -edition = "2021" -rust-version = "1.65" - -[dependencies] -typenum = "1.17" -zeroize = { version = "1.7", path = "../zeroize", optional = true } diff --git a/hybrid-array/LICENSE-APACHE b/hybrid-array/LICENSE-APACHE deleted file mode 100644 index 78173fa2..00000000 --- a/hybrid-array/LICENSE-APACHE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - -TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - -1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - -2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - -3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - -4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - -5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - -6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - -7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - -8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - -9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - -END OF TERMS AND CONDITIONS - -APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - -Copyright [yyyy] [name of copyright owner] - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/hybrid-array/LICENSE-MIT b/hybrid-array/LICENSE-MIT deleted file mode 100644 index e4cbf938..00000000 --- a/hybrid-array/LICENSE-MIT +++ /dev/null @@ -1,25 +0,0 @@ -Copyright (c) 2022 The RustCrypto Project Developers - -Permission is hereby granted, free of charge, to any -person obtaining a copy of this software and associated -documentation files (the "Software"), to deal in the -Software without restriction, including without -limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software -is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice -shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. diff --git a/hybrid-array/README.md b/hybrid-array/README.md index 76105718..67e2df57 100644 --- a/hybrid-array/README.md +++ b/hybrid-array/README.md @@ -1,68 +1,6 @@ -# [RustCrypto]: Hybrid Const Generic / Typenum Arrays +# MOVED! -[![crate][crate-image]][crate-link] -[![Docs][docs-image]][docs-link] -[![Build Status][build-image]][build-link] -![Apache2/MIT licensed][license-image] -![Rust Version][rustc-image] -[![Project Chat][chat-image]][chat-link] +The new repository location is: -Hybrid array type combining const generics with the expressiveness of -[`typenum`]-based constraints, providing an alternative to [`generic-array`] -and a incremental transition path to const generics. +https://github.com/RustCrypto/hybrid-array/ -[Documentation][docs-link] - -## About - -This crate uses `typenum` to enable the following features which aren't yet -possible with the stable implementation of const generics: - -- [#60551: Associated constants in traits can not be used in const generics][rust-issue-60551] -- [#76560: Complex generic constants: `feature(generic_const_exprs)`][rust-issue-76560] - -Internally the crate is built on const generics and provides traits which make -it possible to convert between const generic types and `typenum` types. - -## Features - -This crate exposes the following feature flags. The default is NO features. - -* `zeroize` - Implements [`Zeroize`](https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html) for `Array` - -## License - -Licensed under either of: - - * [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) - * [MIT license](http://opensource.org/licenses/MIT) - -at your option. - -### Contribution - -Unless you explicitly state otherwise, any contribution intentionally submitted -for inclusion in the work by you, as defined in the Apache-2.0 license, shall be -dual licensed as above, without any additional terms or conditions. - -[//]: # (badges) - -[crate-image]: https://buildstats.info/crate/hybrid-array -[crate-link]: https://crates.io/crates/hybrid-array -[docs-image]: https://docs.rs/hybrid-array/badge.svg -[docs-link]: https://docs.rs/hybrid-array/ -[build-image]: https://github.com/RustCrypto/utils/workflows/hybrid-array/badge.svg -[build-link]: https://github.com/RustCrypto/utils/actions/workflows/hybrid-array.yml -[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-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 - -[//]: # (links) - -[RustCrypto]: https://github.com/rustcrypto -[RustCrypto/utils#378]: https://github.com/RustCrypto/utils/issues/378 -[`typenum`]: https://github.com/paholg/typenum -[`generic-array`]: https://github.com/fizyk20/generic-array -[rust-issue-60551]: https://github.com/rust-lang/rust/issues/60551 -[rust-issue-76560]: https://github.com/rust-lang/rust/issues/76560 diff --git a/hybrid-array/src/lib.rs b/hybrid-array/src/lib.rs deleted file mode 100644 index 4eeb183c..00000000 --- a/hybrid-array/src/lib.rs +++ /dev/null @@ -1,903 +0,0 @@ -#![no_std] -#![cfg_attr(docsrs, feature(doc_cfg))] -#![doc = include_str!("../README.md")] -#![doc( - html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg", - html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg" -)] -#![warn( - clippy::cast_lossless, - clippy::cast_possible_truncation, - clippy::cast_possible_wrap, - clippy::cast_precision_loss, - clippy::cast_sign_loss, - clippy::checked_conversions, - clippy::implicit_saturating_sub, - clippy::arithmetic_side_effects, - clippy::panic, - clippy::panic_in_result_fn, - clippy::unwrap_used, - missing_docs, - rust_2018_idioms, - unused_lifetimes, - unused_qualifications -)] - -mod sizes; - -pub use typenum; -pub use typenum::consts; - -use core::{ - array::TryFromSliceError, - borrow::{Borrow, BorrowMut}, - cmp::Ordering, - fmt::{self, Debug}, - hash::{Hash, Hasher}, - mem::{ManuallyDrop, MaybeUninit}, - ops::{Add, Deref, DerefMut, Index, IndexMut, Range, Sub}, - ptr, - slice::{self, Iter, IterMut}, -}; -use typenum::{Diff, Sum, Unsigned}; - -#[cfg(feature = "zeroize")] -use zeroize::{Zeroize, ZeroizeOnDrop}; - -/// Type alias for [`Array`] which is const generic around a size `N`, ala `[T; N]`. -pub type ArrayN = Array::Size>; - -/// Hybrid typenum-based and const generic array type. -/// -/// Provides the flexibility of typenum-based expressions while also -/// allowing interoperability and a transition path to const generics. -#[repr(transparent)] -pub struct Array(pub U::ArrayType); - -type SplitResult = (Array, Array>); -type SplitRefResult<'a, T, U, N> = (&'a Array, &'a Array>); -type SplitRefMutResult<'a, T, U, N> = (&'a mut Array, &'a mut Array>); - -impl Array -where - U: ArraySize, -{ - /// Create array where each array element `T` is returned by the `cb` call. - pub fn from_fn(cb: F) -> Self - where - F: FnMut(usize) -> T, - { - Self(FromFn::from_fn(cb)) - } - - /// Returns an iterator over the array. - #[inline] - pub fn iter(&self) -> Iter<'_, T> { - self.as_ref().iter() - } - - /// Returns an iterator that allows modifying each value. - #[inline] - pub fn iter_mut(&mut self) -> IterMut<'_, T> { - self.as_mut().iter_mut() - } - - /// Returns a slice containing the entire array. Equivalent to `&s[..]`. - #[inline] - pub fn as_slice(&self) -> &[T] { - self.0.as_ref() - } - - /// Returns a mutable slice containing the entire array. Equivalent to `&mut s[..]`. - #[inline] - pub fn as_mut_slice(&mut self) -> &mut [T] { - self.0.as_mut() - } - - /// Convert the given slice into a reference to a hybrid array. - /// - /// # Panics - /// - /// Panics if the slice's length doesn't match the array type. - // TODO(tarcieri): deprecate this before the v0.2 release - // #[deprecated(since = "0.2.0", note = "use TryFrom instead")] - #[inline] - pub fn from_slice(slice: &[T]) -> &Self { - slice.try_into().expect("slice length mismatch") - } - - /// Convert the given mutable slice to a mutable reference to a hybrid array. - /// - /// # Panics - /// - /// Panics if the slice's length doesn't match the array type. - // TODO(tarcieri): deprecate this before the v0.2 release - // #[deprecated(since = "0.2.0", note = "use TryFrom instead")] - #[inline] - pub fn from_mut_slice(slice: &mut [T]) -> &mut Self { - slice.try_into().expect("slice length mismatch") - } - - /// Clone the contents of the slice as a new hybrid array. - /// - /// # Panics - /// - /// Panics if the slice's length doesn't match the array type. - // TODO(tarcieri): deprecate this before the v0.2 release - // #[deprecated(since = "0.2.0", note = "use TryFrom instead")] - #[inline] - pub fn clone_from_slice(slice: &[T]) -> Self - where - Self: Clone, - { - slice.try_into().expect("slice length mismatch") - } - - /// Concatenates `self` with `other`. - #[inline] - pub fn concat(self, other: Array) -> Array> - where - N: ArraySize, - U: Add, - Sum: ArraySize, - { - let mut result = MaybeUninit::uninit(); - let result_ptr = result.as_mut_ptr() as *mut Self; - - unsafe { - ptr::write(result_ptr, self); - ptr::write(result_ptr.add(1) as *mut _, other); - result.assume_init() - } - } - - /// Splits `self` at index `N` in two arrays. - /// - /// New arrays hold the original memory from `self`. - #[inline] - pub fn split(self) -> SplitResult - where - U: Sub, - N: ArraySize, - Diff: ArraySize, - { - unsafe { - let array = ManuallyDrop::new(self); - let head = ptr::read(array.as_ptr() as *const _); - let tail = ptr::read(array.as_ptr().add(N::USIZE) as *const _); - (head, tail) - } - } - - /// Splits `&self` at index `N` in two array references. - #[inline] - pub fn split_ref(&self) -> SplitRefResult<'_, T, U, N> - where - U: Sub, - N: ArraySize, - Diff: ArraySize, - { - unsafe { - let array_ptr = self.as_ptr(); - let head = &*(array_ptr as *const _); - let tail = &*(array_ptr.add(N::USIZE) as *const _); - (head, tail) - } - } - - /// Splits `&mut self` at index `N` in two mutable array references. - #[inline] - pub fn split_ref_mut(&mut self) -> SplitRefMutResult<'_, T, U, N> - where - U: Sub, - N: ArraySize, - Diff: ArraySize, - { - unsafe { - let array_ptr = self.as_mut_ptr(); - let head = &mut *(array_ptr as *mut _); - let tail = &mut *(array_ptr.add(N::USIZE) as *mut _); - (head, tail) - } - } -} - -impl AsRef<[T]> for Array -where - U: ArraySize, -{ - #[inline] - fn as_ref(&self) -> &[T] { - self.0.as_ref() - } -} - -impl AsRef<[T; N]> for Array -where - Self: ArrayOps, - U: ArraySize, -{ - #[inline] - fn as_ref(&self) -> &[T; N] { - self.as_core_array() - } -} - -impl AsMut<[T]> for Array -where - U: ArraySize, -{ - #[inline] - fn as_mut(&mut self) -> &mut [T] { - self.0.as_mut() - } -} - -impl AsMut<[T; N]> for Array -where - Self: ArrayOps, - U: ArraySize, -{ - #[inline] - fn as_mut(&mut self) -> &mut [T; N] { - self.as_mut_core_array() - } -} - -impl Borrow<[T]> for Array -where - U: ArraySize, -{ - #[inline] - fn borrow(&self) -> &[T] { - self.0.as_ref() - } -} - -impl Borrow<[T; N]> for Array -where - Self: ArrayOps, - U: ArraySize, -{ - #[inline] - fn borrow(&self) -> &[T; N] { - self.as_core_array() - } -} - -impl BorrowMut<[T]> for Array -where - U: ArraySize, -{ - #[inline] - fn borrow_mut(&mut self) -> &mut [T] { - self.0.as_mut() - } -} - -impl BorrowMut<[T; N]> for Array -where - Self: ArrayOps, - U: ArraySize, -{ - #[inline] - fn borrow_mut(&mut self) -> &mut [T; N] { - self.as_mut_core_array() - } -} - -impl Clone for Array -where - T: Clone, - U: ArraySize, -{ - #[inline] - fn clone(&self) -> Self { - Self::from_fn(|n| self.0.as_ref()[n].clone()) - } -} - -impl Copy for Array -where - T: Copy, - U: ArraySize, - U::ArrayType: Copy, -{ -} - -impl Debug for Array -where - T: Debug, - U: ArraySize, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple("Array").field(&self.0.as_ref()).finish() - } -} - -impl Default for Array -where - T: Default, - U: ArraySize, -{ - #[inline] - fn default() -> Self { - Self::from_fn(|_| Default::default()) - } -} - -impl Deref for Array -where - U: ArraySize, -{ - type Target = [T]; - - #[inline] - fn deref(&self) -> &[T] { - self.0.as_ref() - } -} - -impl DerefMut for Array -where - U: ArraySize, -{ - #[inline] - fn deref_mut(&mut self) -> &mut [T] { - self.0.as_mut() - } -} - -impl Eq for Array -where - T: Eq, - U: ArraySize, -{ -} - -impl From<[T; N]> for Array -where - Self: ArrayOps, - U: ArraySize, -{ - #[inline] - fn from(arr: [T; N]) -> Array { - Self::from_core_array(arr) - } -} - -impl From> for [T; N] -where - Array: ArrayOps, - U: ArraySize = [T; N]>, -{ - #[inline] - fn from(arr: Array) -> [T; N] { - arr.0 - } -} - -impl<'a, T, U, const N: usize> From<&'a [T; N]> for &'a Array -where - Array: ArrayOps, - U: ArraySize, -{ - #[inline] - fn from(array_ref: &'a [T; N]) -> &'a Array { - >::ref_from_core_array(array_ref) - } -} - -impl<'a, T, U, const N: usize> From<&'a Array> for &'a [T; N] -where - Array: ArrayOps, - U: ArraySize, -{ - #[inline] - fn from(array_ref: &'a Array) -> &'a [T; N] { - array_ref.as_core_array() - } -} - -impl<'a, T, U, const N: usize> From<&'a mut [T; N]> for &'a mut Array -where - Array: ArrayOps, - U: ArraySize, -{ - #[inline] - fn from(array_ref: &'a mut [T; N]) -> &'a mut Array { - >::ref_from_mut_core_array(array_ref) - } -} - -impl<'a, T, U, const N: usize> From<&'a mut Array> for &'a mut [T; N] -where - Array: ArrayOps, - U: ArraySize, -{ - #[inline] - fn from(array_ref: &'a mut Array) -> &'a mut [T; N] { - array_ref.as_mut_core_array() - } -} - -impl FromIterator for Array -where - U: ArraySize, -{ - fn from_iter>(iter: I) -> Self { - let mut iter = iter.into_iter(); - let ret = Self::from_fn(|_| { - iter.next() - .expect("iterator should have enough items to fill array") - }); - - assert!( - iter.next().is_none(), - "too many items in iterator to fit in array" - ); - ret - } -} - -impl Hash for Array -where - T: Hash, - U: ArraySize, -{ - #[inline] - fn hash(&self, state: &mut H) { - self.0.as_ref().hash(state); - } -} - -impl Index for Array -where - [T]: Index, - U: ArraySize, -{ - type Output = <[T] as Index>::Output; - - #[inline] - fn index(&self, index: I) -> &Self::Output { - Index::index(self.as_slice(), index) - } -} - -impl IndexMut for Array -where - [T]: IndexMut, - U: ArraySize, -{ - #[inline] - fn index_mut(&mut self, index: I) -> &mut Self::Output { - IndexMut::index_mut(self.as_mut_slice(), index) - } -} - -impl IntoIterator for Array -where - U: ArraySize, -{ - type Item = T; - type IntoIter = as IntoIterator>::IntoIter; - - /// Creates a consuming iterator, that is, one that moves each value out of - /// the array (from start to end). The array cannot be used after calling - /// this unless `T` implements `Copy`, so the whole array is copied. - #[inline] - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() - } -} - -impl<'a, T, U> IntoIterator for &'a Array -where - U: ArraySize, -{ - type Item = &'a T; - type IntoIter = Iter<'a, T>; - - #[inline] - fn into_iter(self) -> Iter<'a, T> { - self.iter() - } -} - -impl<'a, T, U> IntoIterator for &'a mut Array -where - U: ArraySize, -{ - type Item = &'a mut T; - type IntoIter = IterMut<'a, T>; - - #[inline] - fn into_iter(self) -> IterMut<'a, T> { - self.iter_mut() - } -} - -impl PartialEq for Array -where - T: PartialEq, - U: ArraySize, -{ - #[inline] - fn eq(&self, other: &Self) -> bool { - self.0.as_ref().eq(other.0.as_ref()) - } -} - -impl PartialEq<[T; N]> for Array -where - T: PartialEq, - U: ArraySize = [T; N]>, -{ - #[inline] - fn eq(&self, other: &[T; N]) -> bool { - self.0.eq(other) - } -} - -impl PartialEq> for [T; N] -where - T: PartialEq, - U: ArraySize = [T; N]>, -{ - #[inline] - fn eq(&self, other: &Array) -> bool { - self.eq(&other.0) - } -} - -impl PartialOrd for Array -where - T: PartialOrd, - U: ArraySize, -{ - #[inline] - fn partial_cmp(&self, other: &Self) -> Option { - self.0.as_ref().partial_cmp(other.0.as_ref()) - } -} - -impl Ord for Array -where - T: Ord, - U: ArraySize, -{ - #[inline] - fn cmp(&self, other: &Self) -> Ordering { - self.0.as_ref().cmp(other.0.as_ref()) - } -} - -impl<'a, T, U> TryFrom<&'a [T]> for Array -where - Self: Clone, - U: ArraySize, -{ - type Error = TryFromSliceError; - - #[inline] - fn try_from(slice: &'a [T]) -> Result, TryFromSliceError> { - <&'a Self>::try_from(slice).map(Clone::clone) - } -} - -impl<'a, T, U> TryFrom<&'a [T]> for &'a Array -where - U: ArraySize, -{ - type Error = TryFromSliceError; - - #[inline] - fn try_from(slice: &'a [T]) -> Result { - check_slice_length::(slice)?; - - // SAFETY: `Array` is a `repr(transparent)` newtype for a core - // array with length checked above. - Ok(unsafe { &*(slice.as_ptr() as *const Array) }) - } -} - -impl<'a, T, U> TryFrom<&'a mut [T]> for &'a mut Array -where - U: ArraySize, -{ - type Error = TryFromSliceError; - - #[inline] - fn try_from(slice: &'a mut [T]) -> Result { - check_slice_length::(slice)?; - - // SAFETY: `Array` is a `repr(transparent)` newtype for a core - // array with length checked above. - Ok(unsafe { &mut *(slice.as_ptr() as *mut Array) }) - } -} - -#[cfg(feature = "zeroize")] -impl Zeroize for Array -where - T: Zeroize, - U: ArraySize, -{ - #[inline] - fn zeroize(&mut self) { - self.0.as_mut().iter_mut().zeroize() - } -} - -#[cfg(feature = "zeroize")] -impl ZeroizeOnDrop for Array -where - T: ZeroizeOnDrop, - U: ArraySize, -{ -} - -/// Generate a [`TryFromSliceError`] if the slice doesn't match the given length. -#[cfg_attr(debug_assertions, allow(clippy::panic_in_result_fn))] -fn check_slice_length(slice: &[T]) -> Result<(), TryFromSliceError> { - debug_assert_eq!(Array::<(), U>::default().len(), U::USIZE); - - if slice.len() != U::USIZE { - // Hack: `TryFromSliceError` lacks a public constructor - <&[T; 1]>::try_from([].as_slice())?; - - #[cfg(debug_assertions)] - unreachable!(); - } - - Ok(()) -} - -/// Array operations which are const generic over a given array size. -pub trait ArrayOps: - Borrow<[T; N]> - + BorrowMut<[T; N]> - + From<[T; N]> - + FromFn - + Into<[T; N]> - + IntoIterator - + Sized - + SliceOps -{ - /// Size of an array as a `usize`. - /// - /// Not to be confused with [`AssociatedArraySize::Size`], which is [`typenum`]-based. - const SIZE: usize; - - /// Returns a reference to the inner array. - fn as_core_array(&self) -> &[T; N]; - - /// Returns a mutable reference to the inner array. - fn as_mut_core_array(&mut self) -> &mut [T; N]; - - /// Create array from Rust's core array type. - fn from_core_array(arr: [T; N]) -> Self; - - /// Create array reference from reference to Rust's core array type. - fn ref_from_core_array(arr: &[T; N]) -> &Self; - - /// Create mutable array reference from reference to Rust's core array type. - fn ref_from_mut_core_array(arr: &mut [T; N]) -> &mut Self; - - /// Returns an array of the same size as `self`, with function `f` applied to each element - /// in order. - fn map_to_core_array(self, f: F) -> [U; N] - where - F: FnMut(T) -> U; - - /// Transform slice to slice of core array type - fn cast_slice_to_core(slice: &[Self]) -> &[[T; N]]; - - /// Transform mutable slice to mutable slice of core array type - fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; N]]; - - /// Transform slice to slice of core array type - fn cast_slice_from_core(slice: &[[T; N]]) -> &[Self]; - - /// Transform mutable slice to mutable slice of core array type - fn cast_slice_from_core_mut(slice: &mut [[T; N]]) -> &mut [Self]; -} - -impl ArrayOps for [T; N] { - const SIZE: usize = N; - - #[inline] - fn as_core_array(&self) -> &[T; N] { - self - } - - #[inline] - fn as_mut_core_array(&mut self) -> &mut [T; N] { - self - } - - #[inline] - fn from_core_array(arr: [T; N]) -> Self { - arr - } - - #[inline] - fn ref_from_core_array(array_ref: &[T; N]) -> &Self { - array_ref - } - - #[inline] - fn ref_from_mut_core_array(array_ref: &mut [T; N]) -> &mut Self { - array_ref - } - - #[inline] - fn map_to_core_array(self, f: F) -> [U; N] - where - F: FnMut(T) -> U, - { - self.map(f) - } - - #[inline] - fn cast_slice_to_core(slice: &[Self]) -> &[[T; N]] { - slice - } - - #[inline] - fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; N]] { - slice - } - - #[inline] - fn cast_slice_from_core(slice: &[[T; N]]) -> &[Self] { - slice - } - - #[inline] - fn cast_slice_from_core_mut(slice: &mut [[T; N]]) -> &mut [Self] { - slice - } -} - -/// Slice operations which don't have access to a const generic array size. -pub trait SliceOps: - AsRef<[T]> - + AsMut<[T]> - + Borrow<[T]> - + BorrowMut<[T]> - + Index - + Index> - + IndexMut - + IndexMut> -{ - /// Splits the shared slice into a slice of `N`-element arrays. - /// - /// See [`slice_as_chunks`] for more information. - #[inline] - fn as_array_chunks(&self) -> (&[Array], &[T]) { - slice_as_chunks(self.as_ref()) - } - - /// Splits the exclusive slice into a slice of `N`-element arrays. - /// - /// See [`slice_as_chunks_mut`] for more information. - #[inline] - fn as_array_chunks_mut(&mut self) -> (&mut [Array], &mut [T]) { - slice_as_chunks_mut(self.as_mut()) - } -} - -impl SliceOps for [T] {} -impl SliceOps for [T; N] {} -impl SliceOps for Array {} - -/// Trait which associates a [`usize`] size and `ArrayType` with a -/// `typenum`-provided [`Unsigned`] integer. -/// -/// # Safety -/// -/// `ArrayType` MUST be an array with a number of elements exactly equal to -/// [`Unsigned::USIZE`]. Breaking this requirement will cause undefined behavior. -/// -/// NOTE: This trait is effectively sealed and can not be implemented by third-party crates. -/// It is implemented only for a number of types defined in [`typenum::consts`]. -pub unsafe trait ArraySize: Unsigned { - /// Array type which corresponds to this size. - type ArrayType: AssociatedArraySize - + From> - + FromFn - + Into> - + IntoIterator - + SliceOps; -} - -/// Associates an [`ArraySize`] with a given type. -pub trait AssociatedArraySize: Sized { - /// Size of an array type, expressed as a [`typenum`]-based [`ArraySize`]. - type Size: ArraySize; -} - -impl AssociatedArraySize for Array -where - U: ArraySize, -{ - type Size = U; -} - -/// Construct an array type from the given function. -pub trait FromFn: Sized { - /// Create array using the given callback function for each element. - fn from_fn(cb: F) -> Self - where - F: FnMut(usize) -> T; -} - -impl FromFn for Array -where - U: ArraySize, -{ - #[inline] - fn from_fn(cb: F) -> Self - where - F: FnMut(usize) -> T, - { - Array::from_fn(cb) - } -} - -impl FromFn for [T; N] { - #[inline] - fn from_fn(cb: F) -> Self - where - F: FnMut(usize) -> T, - { - core::array::from_fn(cb) - } -} - -/// Splits the shared slice into a slice of `N`-element arrays, starting at the beginning -/// of the slice, and a remainder slice with length strictly less than `N`. -/// -/// # Panics -/// Panics if `N` is 0. -#[allow(clippy::arithmetic_side_effects)] -#[inline] -pub fn slice_as_chunks(buf: &[T]) -> (&[Array], &[T]) { - assert_ne!(N::USIZE, 0, "chunk size must be non-zero"); - // Arithmetic safety: we have checked that `N::USIZE` is not zero, thus - // division always returns correct result. `tail_pos` can not be bigger than `buf.len()`, - // thus overflow on multiplication and underflow on substraction are impossible. - let chunks_len = buf.len() / N::USIZE; - let tail_pos = N::USIZE * chunks_len; - let tail_len = buf.len() - tail_pos; - unsafe { - let ptr = buf.as_ptr(); - let chunks = slice::from_raw_parts(ptr as *const Array, chunks_len); - let tail = slice::from_raw_parts(ptr.add(tail_pos), tail_len); - (chunks, tail) - } -} - -/// Splits the exclusive slice into a slice of `N`-element arrays, starting at the beginning -/// of the slice, and a remainder slice with length strictly less than `N`. -/// -/// # Panics -/// Panics if `N` is 0. -#[allow(clippy::arithmetic_side_effects)] -#[inline] -pub fn slice_as_chunks_mut(buf: &mut [T]) -> (&mut [Array], &mut [T]) { - assert_ne!(N::USIZE, 0, "chunk size must be non-zero"); - // Arithmetic safety: we have checked that `N::USIZE` is not zero, thus - // division always returns correct result. `tail_pos` can not be bigger than `buf.len()`, - // thus overflow on multiplication and underflow on substraction are impossible. - let chunks_len = buf.len() / N::USIZE; - let tail_pos = N::USIZE * chunks_len; - let tail_len = buf.len() - tail_pos; - unsafe { - let ptr = buf.as_mut_ptr(); - let chunks = slice::from_raw_parts_mut(ptr as *mut Array, chunks_len); - let tail = slice::from_raw_parts_mut(ptr.add(tail_pos), tail_len); - (chunks, tail) - } -} diff --git a/hybrid-array/src/sizes.rs b/hybrid-array/src/sizes.rs deleted file mode 100644 index 183ca547..00000000 --- a/hybrid-array/src/sizes.rs +++ /dev/null @@ -1,355 +0,0 @@ -//! Macros for defining various array sizes, and their associated invocations. - -use super::{Array, ArrayOps, ArraySize, AssociatedArraySize}; - -macro_rules! impl_array_size { - ($($len:expr => $ty:ident),+) => { - $( - unsafe impl ArraySize for typenum::$ty { - type ArrayType = [T; $len]; - } - - impl AssociatedArraySize for [T; $len] { - type Size = typenum::$ty; - } - - impl ArrayOps for Array { - const SIZE: usize = $len; - - #[inline] - fn as_core_array(&self) -> &[T; $len] { - &self.0 - } - - #[inline] - fn as_mut_core_array(&mut self) -> &mut [T; $len] { - &mut self.0 - } - - #[inline] - fn from_core_array(arr: [T; $len]) -> Self { - Self(arr) - } - - #[inline] - fn ref_from_core_array(array_ref: &[T; $len]) -> &Self { - // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` - unsafe { &*(array_ref.as_ptr() as *const Self) } - } - - #[inline] - fn ref_from_mut_core_array(array_ref: &mut [T; $len]) -> &mut Self { - // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` - unsafe { &mut *(array_ref.as_mut_ptr() as *mut Self) } - } - - #[inline] - fn map_to_core_array(self, f: F) -> [U; $len] - where - F: FnMut(T) -> U - { - self.0.map(f) - } - - #[inline] - fn cast_slice_to_core(slice: &[Self]) -> &[[T; $len]] { - // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` - unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) } - } - - #[inline] - fn cast_slice_to_core_mut(slice: &mut [Self]) -> &mut [[T; $len]] { - // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` - unsafe { core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) } - } - - #[inline] - fn cast_slice_from_core(slice: &[[T; $len]]) -> &[Self] { - // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` - unsafe { core::slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) } - } - - #[inline] - fn cast_slice_from_core_mut(slice: &mut [[T; $len]]) -> &mut [Self] { - // SAFETY: `Self` is a `repr(transparent)` newtype for `[T; $len]` - unsafe { core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), slice.len()) } - } - } - )+ - }; -} - -impl_array_size! { - 0 => U0, - 1 => U1, - 2 => U2, - 3 => U3, - 4 => U4, - 5 => U5, - 6 => U6, - 7 => U7, - 8 => U8, - 9 => U9, - 10 => U10, - 11 => U11, - 12 => U12, - 13 => U13, - 14 => U14, - 15 => U15, - 16 => U16, - 17 => U17, - 18 => U18, - 19 => U19, - 20 => U20, - 21 => U21, - 22 => U22, - 23 => U23, - 24 => U24, - 25 => U25, - 26 => U26, - 27 => U27, - 28 => U28, - 29 => U29, - 30 => U30, - 31 => U31, - 32 => U32, - 33 => U33, - 34 => U34, - 35 => U35, - 36 => U36, - 37 => U37, - 38 => U38, - 39 => U39, - 40 => U40, - 41 => U41, - 42 => U42, - 43 => U43, - 44 => U44, - 45 => U45, - 46 => U46, - 47 => U47, - 48 => U48, - 49 => U49, - 50 => U50, - 51 => U51, - 52 => U52, - 53 => U53, - 54 => U54, - 55 => U55, - 56 => U56, - 57 => U57, - 58 => U58, - 59 => U59, - 60 => U60, - 61 => U61, - 62 => U62, - 63 => U63, - 64 => U64, - 65 => U65, - 66 => U66, - 67 => U67, - 68 => U68, - 69 => U69, - 70 => U70, - 71 => U71, - 72 => U72, - 73 => U73, - 74 => U74, - 75 => U75, - 76 => U76, - 77 => U77, - 78 => U78, - 79 => U79, - 80 => U80, - 81 => U81, - 82 => U82, - 83 => U83, - 84 => U84, - 85 => U85, - 86 => U86, - 87 => U87, - 88 => U88, - 89 => U89, - 90 => U90, - 91 => U91, - 92 => U92, - 93 => U93, - 94 => U94, - 95 => U95, - 96 => U96, - 97 => U97, - 98 => U98, - 99 => U99, - 100 => U100, - 101 => U101, - 102 => U102, - 103 => U103, - 104 => U104, - 105 => U105, - 106 => U106, - 107 => U107, - 108 => U108, - 109 => U109, - 110 => U110, - 111 => U111, - 112 => U112, - 113 => U113, - 114 => U114, - 115 => U115, - 116 => U116, - 117 => U117, - 118 => U118, - 119 => U119, - 120 => U120, - 121 => U121, - 122 => U122, - 123 => U123, - 124 => U124, - 125 => U125, - 126 => U126, - 127 => U127, - 128 => U128, - 129 => U129, - 130 => U130, - 131 => U131, - 132 => U132, - 133 => U133, - 134 => U134, - 135 => U135, - 136 => U136, - 137 => U137, - 138 => U138, - 139 => U139, - 140 => U140, - 141 => U141, - 142 => U142, - 143 => U143, - 144 => U144, - 145 => U145, - 146 => U146, - 147 => U147, - 148 => U148, - 149 => U149, - 150 => U150, - 151 => U151, - 152 => U152, - 153 => U153, - 154 => U154, - 155 => U155, - 156 => U156, - 157 => U157, - 158 => U158, - 159 => U159, - 160 => U160, - 161 => U161, - 162 => U162, - 163 => U163, - 164 => U164, - 165 => U165, - 166 => U166, - 167 => U167, - 168 => U168, - 169 => U169, - 170 => U170, - 171 => U171, - 172 => U172, - 173 => U173, - 174 => U174, - 175 => U175, - 176 => U176, - 177 => U177, - 178 => U178, - 179 => U179, - 180 => U180, - 181 => U181, - 182 => U182, - 183 => U183, - 184 => U184, - 185 => U185, - 186 => U186, - 187 => U187, - 188 => U188, - 189 => U189, - 190 => U190, - 191 => U191, - 192 => U192, - 193 => U193, - 194 => U194, - 195 => U195, - 196 => U196, - 197 => U197, - 198 => U198, - 199 => U199, - 200 => U200, - 201 => U201, - 202 => U202, - 203 => U203, - 204 => U204, - 205 => U205, - 206 => U206, - 207 => U207, - 208 => U208, - 209 => U209, - 210 => U210, - 211 => U211, - 212 => U212, - 213 => U213, - 214 => U214, - 215 => U215, - 216 => U216, - 217 => U217, - 218 => U218, - 219 => U219, - 220 => U220, - 221 => U221, - 222 => U222, - 223 => U223, - 224 => U224, - 225 => U225, - 226 => U226, - 227 => U227, - 228 => U228, - 229 => U229, - 230 => U230, - 231 => U231, - 232 => U232, - 233 => U233, - 234 => U234, - 235 => U235, - 236 => U236, - 237 => U237, - 238 => U238, - 239 => U239, - 240 => U240, - 241 => U241, - 242 => U242, - 243 => U243, - 244 => U244, - 245 => U245, - 246 => U246, - 247 => U247, - 248 => U248, - 249 => U249, - 250 => U250, - 251 => U251, - 252 => U252, - 253 => U253, - 254 => U254, - 255 => U255, - 256 => U256, - 272 => U272, - 288 => U288, - 304 => U304, - 320 => U320, - 336 => U336, - 352 => U352, - 384 => U384, - 448 => U448, - 512 => U512, - 768 => U768, - 896 => U896, - 1024 => U1024, - 2048 => U2048, - 4096 => U4096, - 8192 => U8192 -} diff --git a/hybrid-array/tests/mod.rs b/hybrid-array/tests/mod.rs deleted file mode 100644 index 90949ee4..00000000 --- a/hybrid-array/tests/mod.rs +++ /dev/null @@ -1,92 +0,0 @@ -use hybrid_array::{Array, ArrayN}; -use typenum::{U0, U2, U3, U4, U5, U6, U7}; - -const EXAMPLE_SLICE: &[u8] = &[1, 2, 3, 4, 5, 6]; - -/// Ensure `ArrayN` works as expected. -const _FOO: ArrayN = Array([1, 2, 3, 4]); - -#[test] -fn clone_from_slice() { - let array = Array::::clone_from_slice(EXAMPLE_SLICE); - assert_eq!(array.as_slice(), EXAMPLE_SLICE); -} - -#[test] -fn tryfrom_slice_for_array() { - assert!(Array::::try_from(EXAMPLE_SLICE).is_err()); - assert!(Array::::try_from(EXAMPLE_SLICE).is_err()); - - let array_ref = Array::::try_from(EXAMPLE_SLICE).expect("slice contains 6 bytes"); - assert_eq!(&*array_ref, EXAMPLE_SLICE); - - assert!(Array::::try_from(EXAMPLE_SLICE).is_err()); -} - -#[test] -fn tryfrom_slice_for_array_ref() { - assert!(<&Array>::try_from(EXAMPLE_SLICE).is_err()); - assert!(<&Array::>::try_from(EXAMPLE_SLICE).is_err()); - - let array_ref = <&Array>::try_from(EXAMPLE_SLICE).expect("slice contains 6 bytes"); - assert_eq!(array_ref.as_slice(), EXAMPLE_SLICE); - - assert!(<&Array::>::try_from(EXAMPLE_SLICE).is_err()); -} - -#[test] -fn concat() { - let prefix = Array::::clone_from_slice(&EXAMPLE_SLICE[..2]); - let suffix = Array::::clone_from_slice(&EXAMPLE_SLICE[2..]); - - let array = prefix.concat(suffix); - assert_eq!(array.as_slice(), EXAMPLE_SLICE); -} - -#[test] -fn split() { - let array = Array::::clone_from_slice(EXAMPLE_SLICE); - - let (prefix, suffix) = array.split::(); - - assert_eq!(prefix.as_slice(), &EXAMPLE_SLICE[..2]); - assert_eq!(suffix.as_slice(), &EXAMPLE_SLICE[2..]); -} - -#[test] -fn split_ref() { - let array = Array::::clone_from_slice(EXAMPLE_SLICE); - - let (prefix, suffix) = array.split_ref::(); - - assert_eq!(prefix.as_slice(), &EXAMPLE_SLICE[..3]); - assert_eq!(suffix.as_slice(), &EXAMPLE_SLICE[3..]); -} - -#[test] -fn split_ref_mut() { - let array = &mut Array::::clone_from_slice(EXAMPLE_SLICE); - - let (prefix, suffix) = array.split_ref_mut::(); - - assert_eq!(prefix.as_slice(), &EXAMPLE_SLICE[..4]); - assert_eq!(suffix.as_slice(), &EXAMPLE_SLICE[4..]); -} - -#[test] -fn from_iterator_correct_size() { - let array: Array = EXAMPLE_SLICE.iter().copied().collect(); - assert_eq!(array.as_slice(), EXAMPLE_SLICE); -} - -#[test] -#[should_panic] -fn from_iterator_too_short() { - let _array: Array = EXAMPLE_SLICE.iter().copied().collect(); -} - -#[test] -#[should_panic] -fn from_iterator_too_long() { - let _array: Array = EXAMPLE_SLICE.iter().copied().collect(); -}