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

Add getrandom::value function based on zerocopy::FromBytes #381

Closed
wants to merge 10 commits into from
Closed
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
22 changes: 11 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ jobs:
- name: Generate Docs
env:
RUSTDOCFLAGS: --cfg docsrs
run: cargo deadlinks -- --features=custom,std
run: cargo deadlinks -- --features=custom,std,zerocopy
- run: |
cargo generate-lockfile -Z minimal-versions
cargo test --features=custom,std
cargo test --features=custom,std,zerocopy

# TODO: add aarch64-based macOS runner when it's supported by Github Actions
main-tests:
Expand Down Expand Up @@ -80,7 +80,7 @@ jobs:
- name: Install multilib
run: sudo apt-get update && sudo apt-get install gcc-multilib
- uses: Swatinem/rust-cache@v2
- run: cargo test --target=${{ matrix.target }} --features=std
- run: cargo test --target=${{ matrix.target }} --features=std,zerocopy

ios-tests:
name: iOS Simulator Test
Expand Down Expand Up @@ -128,7 +128,7 @@ jobs:
with:
toolchain: ${{ matrix.toolchain }}
- uses: Swatinem/rust-cache@v2
- run: cargo test --features=std
- run: cargo test --features=std,zerocopy

cross-tests:
name: Cross Test
Expand All @@ -152,7 +152,7 @@ jobs:
wget -O - $URL | tar -xz -C ~/.cargo/bin
cross --version
- name: Test
run: cross test --no-fail-fast --target=${{ matrix.target }} --features=std
run: cross test --no-fail-fast --target=${{ matrix.target }} --features=std,zerocopy

macos-link:
name: macOS ARM64 Build/Link
Expand All @@ -164,9 +164,9 @@ jobs:
targets: aarch64-apple-darwin, aarch64-apple-ios
components: rust-src
- uses: Swatinem/rust-cache@v2
- run: cargo test --no-run --target=aarch64-apple-darwin --features=std
- run: cargo test --no-run --target=aarch64-apple-ios --features=std
- run: cargo test --no-run --target=aarch64-apple-watchos-sim -Zbuild-std --features=std
- run: cargo test --no-run --target=aarch64-apple-darwin --features=std,zerocopy
- run: cargo test --no-run --target=aarch64-apple-ios --features=std,zerocopy
- run: cargo test --no-run --target=aarch64-apple-watchos-sim -Zbuild-std --features=std,zerocopy

cross-link:
name: Cross Build/Link
Expand All @@ -188,7 +188,7 @@ jobs:
wget -O - $URL | tar -xz -C ~/.cargo/bin
cross --version
- name: Build Tests
run: cross test --no-run --target=${{ matrix.target }} --features=std
run: cross test --no-run --target=${{ matrix.target }} --features=std,zerocopy

web-tests:
name: Web Test
Expand Down Expand Up @@ -284,7 +284,7 @@ jobs:
targets: ${{ matrix.target }}
- uses: Swatinem/rust-cache@v2
- name: Build
run: cargo build --target=${{ matrix.target }} --features=std
run: cargo build --target=${{ matrix.target }} --features=std,zerocopy

build-tier3:
name: Tier 3 Build
Expand Down Expand Up @@ -348,6 +348,6 @@ jobs:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- name: clippy
run: cargo clippy --all --features=custom,std
run: cargo clippy --all --features=custom,std,zerocopy
- name: fmt
run: cargo fmt --all -- --check
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ exclude = [".*"]

[dependencies]
cfg-if = "1"
# WARNING: Enabling this dependency bumps MSRV to 1.60
zerocopy = { version = "0.7", optional = true, default-features = false }

# When built as part of libstd
compiler_builtins = { version = "0.1", optional = true }
Expand Down Expand Up @@ -49,7 +51,7 @@ rustc-dep-of-std = [
test-in-browser = []

[package.metadata.docs.rs]
features = ["std", "custom"]
features = ["std", "custom", "zerocopy"]
rustdoc-args = ["--cfg", "docsrs"]

# workaround for https://github.com/cross-rs/cross/issues/1345
Expand Down
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@
#[macro_use]
extern crate cfg_if;

#[cfg(feature = "zerocopy")]
pub use zerocopy;

use crate::util::{slice_as_uninit_mut, slice_assume_init_mut};
use core::mem::MaybeUninit;

Expand Down Expand Up @@ -350,3 +353,33 @@ pub fn getrandom_uninit(dest: &mut [MaybeUninit<u8>]) -> Result<&mut [u8], Error
// since it returned `Ok`.
Ok(unsafe { slice_assume_init_mut(dest) })
}

/// Generate a random value of type `T` implementing the [`zerocopy::FromBytes`] trait.
///
/// # Examples
/// ```
/// # fn main() -> Result<(), getrandom::Error> {
/// let key: [u8; 16] = getrandom::value()?;
/// let keys: [[u8; 16]; 64] = getrandom::value()?;
/// let random_u32: u32 = getrandom::value()?;
/// let random_u64s: [u64; 100] = getrandom::value()?;
/// # Ok(()) }
/// ```
#[cfg(feature = "zerocopy")]
#[inline]
pub fn value<T: zerocopy::FromBytes + Sized>() -> Result<T, Error> {
let mut value = MaybeUninit::<T>::uninit();
// SAFETY: it's safe to cast `&mut MaybeUninit<T>` to `&mut [MaybeUninit<u8>]`
// with slice length equal to `size_of::<T>()`. The compiler will ensure that
// `T` isn't too large.
unsafe {
let ptr: *mut MaybeUninit<u8> = value.as_mut_ptr().cast();
let size = core::mem::size_of::<T>();
let uninit_bytes = core::slice::from_raw_parts_mut(ptr, size);
getrandom_uninit(uninit_bytes)?;
};
// SAFETY: when `getrandom_uninit` returns `Ok` all bytes in `uninit_bytes`
// (and thus in `value`) are properly initialized. Any bit-sequence is valid
// for `T: FromBytes`, so we can safely execute `assume_init` on `value`.
Ok(unsafe { value.assume_init() })
}