Skip to content

Commit

Permalink
Migrate to hybrid-array; MSRV 1.65
Browse files Browse the repository at this point in the history
Migrates the following crates away from using `generic-array` to using
`hybrid-array` instead:

- `block-buffer`
- `block-padding`
- `dbl`
- `inout`
  • Loading branch information
tarcieri committed Oct 26, 2023
1 parent a0874f5 commit 4aba66e
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 169 deletions.
17 changes: 7 additions & 10 deletions .github/workflows/block-buffer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -37,29 +37,26 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
- run: rm ../Cargo.toml
- run: cargo build --target ${{ matrix.target }}

minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
with:
working-directory: ${{ github.workflow }}
# TODO(tarcieri): re-enable after next `crypto-common` release
#minimal-versions:
# uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
# with:
# working-directory: ${{ github.workflow }}

test:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
- run: rm ../Cargo.toml
- run: cargo test
- run: cargo test --all-features
4 changes: 2 additions & 2 deletions .github/workflows/block-padding.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -49,7 +49,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/dbl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
rust:
- 1.41.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -37,8 +37,6 @@ jobs:
with:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
- run: rm ../Cargo.toml
- run: cargo build --target ${{ matrix.target }}

minimal-versions:
Expand All @@ -51,14 +49,12 @@ jobs:
strategy:
matrix:
rust:
- 1.41.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
- uses: RustCrypto/actions/cargo-cache@master
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
# Isolate this crate from workspace which is otherwise MSRV 1.56 due to 2021 edition crates
- run: rm ../Cargo.toml
- run: cargo test
4 changes: 2 additions & 2 deletions .github/workflows/inout.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -50,7 +50,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
Expand Down
44 changes: 17 additions & 27 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ members = [

[profile.dev]
opt-level = 2

[patch.crates-io]
crypto-common = { git = "https://github.com/RustCrypto/traits" }
1 change: 0 additions & 1 deletion block-buffer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ readme = "README.md"

[dependencies]
crypto-common = "0.2.0-pre"
generic-array = "0.14"
zeroize = { version = "1.4", optional = true, default-features = false }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion block-buffer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Unless you explicitly state otherwise, any contribution intentionally submitted
[docs-image]: https://docs.rs/block-buffer/badge.svg
[docs-link]: https://docs.rs/block-buffer/
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
[rustc-image]: https://img.shields.io/badge/rustc-1.41+-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
[build-image]: https://github.com/RustCrypto/utils/workflows/block-buffer/badge.svg?branch=master&event=push
Expand Down
24 changes: 11 additions & 13 deletions block-buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
)]
#![warn(missing_docs, rust_2018_idioms)]

pub use generic_array;
pub use crypto_common::{array, Block};

use core::{fmt, ops::Add, slice};
pub use crypto_common::Block;
use crypto_common::{BlockSizeUser, BlockSizes};
use generic_array::{
use array::{
typenum::{Add1, B1},
ArrayLength, GenericArray,
Array, ArraySize,
};
use core::{fmt, ops::Add, slice};
use crypto_common::{BlockSizeUser, BlockSizes};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

Expand All @@ -24,7 +24,7 @@ mod sealed;
pub use read::ReadBuffer;

/// Block with additional one byte
type BlockP1<BlockSize> = GenericArray<u8, Add1<BlockSize>>;
type BlockP1<BlockSize> = Array<u8, Add1<BlockSize>>;

/// Trait for buffer kinds.
pub trait BufferKind: sealed::Sealed {}
Expand Down Expand Up @@ -306,7 +306,7 @@ impl<BS: BlockSizes> BlockBuffer<BS, Lazy> {
pub fn serialize(&self) -> BlockP1<BS>
where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
Add1<BS>: ArraySize,
{
let mut res = BlockP1::<BS>::default();
res[0] = self.pos;
Expand All @@ -320,7 +320,7 @@ impl<BS: BlockSizes> BlockBuffer<BS, Lazy> {
pub fn deserialize(buffer: &BlockP1<BS>) -> Result<Self, Error>
where
BS: Add<B1>,
Add1<BS>: ArrayLength<u8>,
Add1<BS>: ArraySize,
{
let pos = buffer[0];
if !<Lazy as sealed::Sealed>::invariant(pos as usize, BS::USIZE) {
Expand All @@ -329,10 +329,8 @@ impl<BS: BlockSizes> BlockBuffer<BS, Lazy> {
if buffer[1..][pos as usize..].iter().any(|&b| b != 0) {
return Err(Error);
}
Ok(Self {
buffer: GenericArray::clone_from_slice(&buffer[1..]),
pos,
})
let buffer = Array::clone_from_slice(&buffer[1..]);
Ok(Self { buffer, pos })
}
}

Expand Down
12 changes: 6 additions & 6 deletions block-buffer/src/sealed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::array::{Array, ArraySize};
use core::slice;
use generic_array::{ArrayLength, GenericArray};

/// Sealed trait for buffer kinds.
pub trait Sealed {
Expand All @@ -17,7 +17,7 @@ pub trait Sealed {
fn invariant(pos: usize, block_size: usize) -> bool;

/// Split input data into slice of blocks and tail.
fn split_blocks<N: ArrayLength<u8>>(data: &[u8]) -> (&[GenericArray<u8, N>], &[u8]);
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]);
}

impl Sealed for super::Eager {
Expand All @@ -38,14 +38,14 @@ impl Sealed for super::Eager {
}

#[inline(always)]
fn split_blocks<N: ArrayLength<u8>>(data: &[u8]) -> (&[GenericArray<u8, N>], &[u8]) {
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]) {
let nb = data.len() / N::USIZE;
let blocks_len = nb * N::USIZE;
let tail_len = data.len() - blocks_len;
// SAFETY: we guarantee that created slices do not point
// outside of `data`
unsafe {
let blocks_ptr = data.as_ptr() as *const GenericArray<u8, N>;
let blocks_ptr = data.as_ptr() as *const Array<u8, N>;
let tail_ptr = data.as_ptr().add(blocks_len);
(
slice::from_raw_parts(blocks_ptr, nb),
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Sealed for super::Lazy {
}

#[inline(always)]
fn split_blocks<N: ArrayLength<u8>>(data: &[u8]) -> (&[GenericArray<u8, N>], &[u8]) {
fn split_blocks<N: ArraySize>(data: &[u8]) -> (&[Array<u8, N>], &[u8]) {
if data.is_empty() {
return (&[], &[]);
}
Expand All @@ -87,7 +87,7 @@ impl Sealed for super::Lazy {
// SAFETY: we guarantee that created slices do not point
// outside of `data`
unsafe {
let blocks_ptr = data.as_ptr() as *const GenericArray<u8, N>;
let blocks_ptr = data.as_ptr() as *const Array<u8, N>;
let tail_ptr = data.as_ptr().add(blocks_len);
(
slice::from_raw_parts(blocks_ptr, nb),
Expand Down
Loading

0 comments on commit 4aba66e

Please sign in to comment.