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

read: use const generics in src/read/util.rs #752

Merged
merged 2 commits into from
Sep 28, 2024
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
37 changes: 18 additions & 19 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
os: macOS-latest
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust
uses: dtolnay/rust-toolchain@master
with:
Expand All @@ -42,7 +42,7 @@ jobs:
msrv-read:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust
uses: dtolnay/[email protected]
- name: Build
Expand All @@ -53,7 +53,7 @@ jobs:
msrv:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust
uses: dtolnay/[email protected]
- name: Build
Expand All @@ -65,20 +65,18 @@ jobs:
name: Build fuzz targets
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust nightly
uses: dtolnay/rust-toolchain@nightly
- name: Install `cargo fuzz`
run: cargo install cargo-fuzz --vers '^0.11.0'
- run: cargo fuzz build -Oa
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: fuzz-targets
path: fuzz/target/x86_64-unknown-linux-gnu/release/debug_*
- uses: actions/upload-artifact@v3
with:
name: fuzz-targets
path: fuzz/target/x86_64-unknown-linux-gnu/release/eh_*
path: |
fuzz/target/x86_64-unknown-linux-gnu/release/debug_*
fuzz/target/x86_64-unknown-linux-gnu/release/eh_*

run_fuzz_targets:
strategy:
Expand All @@ -89,14 +87,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Clone the fuzz corpora
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: gimli-rs/gimli-libfuzzer-corpora
path: corpora
- name: Download fuzz targets
uses: actions/download-artifact@v1
uses: actions/download-artifact@v4
with:
name: fuzz-targets
path: fuzz-targets
# Note: -max_total_time=300 == 300 seconds == 5 minutes.
- name: Run `${{matrix.fuzz_target}}` fuzz target
run: |
Expand All @@ -109,15 +108,15 @@ jobs:
# can debug them.
- name: Upload fuzz artifacts
if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{matrix.fuzz_target}}_artifacts
path: ./${{matrix.fuzz_target}}_artifacts

features:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust stable
uses: dtolnay/rust-toolchain@stable
- uses: taiki-e/install-action@v2
Expand All @@ -130,7 +129,7 @@ jobs:
bench:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust nightly
uses: dtolnay/rust-toolchain@nightly
- run: cargo bench
Expand All @@ -145,7 +144,7 @@ jobs:
- powerpc64-unknown-linux-gnu
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust stable
uses: dtolnay/rust-toolchain@stable
with:
Expand All @@ -158,7 +157,7 @@ jobs:
rustfmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust stable
uses: dtolnay/rust-toolchain@stable
with:
Expand All @@ -171,7 +170,7 @@ jobs:
image: xd009642/tarpaulin
options: --security-opt seccomp=unconfined
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust stable
uses: dtolnay/rust-toolchain@stable
- name: Run cargo-tarpaulin
Expand All @@ -185,7 +184,7 @@ jobs:
doc:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install rust stable
uses: dtolnay/rust-toolchain@stable
- run: cargo doc
80 changes: 31 additions & 49 deletions src/read/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,70 +41,52 @@ pub trait ArrayLike: Sealed {
fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<Self::Item>];
}

// Use macro since const generics can't be used due to MSRV.
macro_rules! impl_array {
() => {};
($n:literal $($rest:tt)*) => {
// SAFETY: does not modify the content in storage.
unsafe impl<T> Sealed for [T; $n] {
type Storage = [MaybeUninit<T>; $n];

fn new_storage() -> Self::Storage {
// SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid.
unsafe { MaybeUninit::uninit().assume_init() }
}
}
// SAFETY: does not modify the content in storage.
unsafe impl<T, const N: usize> Sealed for [T; N] {
type Storage = [MaybeUninit<T>; N];

impl<T> ArrayLike for [T; $n] {
type Item = T;
fn new_storage() -> Self::Storage {
// SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid.
unsafe { MaybeUninit::uninit().assume_init() }
}
}

fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] {
storage
}
impl<T, const N: usize> ArrayLike for [T; N] {
type Item = T;

fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] {
storage
}
}
fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] {
storage
}

impl_array!($($rest)*);
fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] {
storage
}
}

// SAFETY: does not modify the content in storage.
#[cfg(feature = "read")]
macro_rules! impl_box {
() => {};
($n:literal $($rest:tt)*) => {
// SAFETY: does not modify the content in storage.
unsafe impl<T> Sealed for Box<[T; $n]> {
type Storage = Box<[MaybeUninit<T>; $n]>;

fn new_storage() -> Self::Storage {
// SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid.
Box::new(unsafe { MaybeUninit::uninit().assume_init() })
}
}
unsafe impl<T, const N: usize> Sealed for Box<[T; N]> {
type Storage = Box<[MaybeUninit<T>; N]>;

impl<T> ArrayLike for Box<[T; $n]> {
type Item = T;
fn new_storage() -> Self::Storage {
// SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid.
Box::new(unsafe { MaybeUninit::uninit().assume_init() })
}
}

fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] {
&storage[..]
}
#[cfg(feature = "read")]
impl<T, const N: usize> ArrayLike for Box<[T; N]> {
type Item = T;

fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] {
&mut storage[..]
}
}
fn as_slice(storage: &Self::Storage) -> &[MaybeUninit<T>] {
&storage[..]
}

impl_box!($($rest)*);
fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit<T>] {
&mut storage[..]
}
}

impl_array!(0 1 2 3 4 8 16 32 64 128 192);
#[cfg(feature = "read")]
impl_box!(0 1 2 3 4 8 16 32 64 128 192);

#[cfg(feature = "read")]
unsafe impl<T> Sealed for Vec<T> {
type Storage = Box<[MaybeUninit<T>]>;
Expand Down
Loading