Coverage Report

Created: 2025-03-07 06:49

/rust/registry/src/index.crates.io-6f17d22bba15001f/getrandom-0.3.1/src/util.rs
Line
Count
Source (jump to first uncovered line)
1
#![allow(dead_code)]
2
use crate::Error;
3
use core::{mem::MaybeUninit, ptr, slice};
4
5
/// Polyfill for `maybe_uninit_slice` feature's
6
/// `MaybeUninit::slice_assume_init_mut`. Every element of `slice` must have
7
/// been initialized.
8
#[inline(always)]
9
#[allow(unused_unsafe)] // TODO(MSRV 1.65): Remove this.
10
366
pub unsafe fn slice_assume_init_mut<T>(slice: &mut [MaybeUninit<T>]) -> &mut [T] {
11
366
    let ptr = ptr_from_mut::<[MaybeUninit<T>]>(slice) as *mut [T];
12
366
    // SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
13
366
    unsafe { &mut *ptr }
14
366
}
getrandom::util::slice_assume_init_mut::<u8>
Line
Count
Source
10
366
pub unsafe fn slice_assume_init_mut<T>(slice: &mut [MaybeUninit<T>]) -> &mut [T] {
11
366
    let ptr = ptr_from_mut::<[MaybeUninit<T>]>(slice) as *mut [T];
12
366
    // SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
13
366
    unsafe { &mut *ptr }
14
366
}
Unexecuted instantiation: getrandom::util::slice_assume_init_mut::<u8>
Unexecuted instantiation: getrandom::util::slice_assume_init_mut::<u8>
15
16
#[inline]
17
0
pub fn uninit_slice_fill_zero(slice: &mut [MaybeUninit<u8>]) -> &mut [u8] {
18
0
    unsafe { ptr::write_bytes(slice.as_mut_ptr(), 0, slice.len()) };
19
0
    unsafe { slice_assume_init_mut(slice) }
20
0
}
21
22
#[inline(always)]
23
0
pub fn slice_as_uninit<T>(slice: &[T]) -> &[MaybeUninit<T>] {
24
0
    let ptr = ptr_from_ref::<[T]>(slice) as *const [MaybeUninit<T>];
25
0
    // SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
26
0
    unsafe { &*ptr }
27
0
}
28
29
/// View an mutable initialized array as potentially-uninitialized.
30
///
31
/// This is unsafe because it allows assigning uninitialized values into
32
/// `slice`, which would be undefined behavior.
33
#[inline(always)]
34
#[allow(unused_unsafe)] // TODO(MSRV 1.65): Remove this.
35
366
pub unsafe fn slice_as_uninit_mut<T>(slice: &mut [T]) -> &mut [MaybeUninit<T>] {
36
366
    let ptr = ptr_from_mut::<[T]>(slice) as *mut [MaybeUninit<T>];
37
366
    // SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
38
366
    unsafe { &mut *ptr }
39
366
}
getrandom::util::slice_as_uninit_mut::<u8>
Line
Count
Source
35
366
pub unsafe fn slice_as_uninit_mut<T>(slice: &mut [T]) -> &mut [MaybeUninit<T>] {
36
366
    let ptr = ptr_from_mut::<[T]>(slice) as *mut [MaybeUninit<T>];
37
366
    // SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
38
366
    unsafe { &mut *ptr }
39
366
}
Unexecuted instantiation: getrandom::util::slice_as_uninit_mut::<u8>
Unexecuted instantiation: getrandom::util::slice_as_uninit_mut::<_>
40
41
// TODO: MSRV(1.76.0): Replace with `core::ptr::from_mut`.
42
732
fn ptr_from_mut<T: ?Sized>(r: &mut T) -> *mut T {
43
732
    r
44
732
}
getrandom::util::ptr_from_mut::<[u8]>
Line
Count
Source
42
366
fn ptr_from_mut<T: ?Sized>(r: &mut T) -> *mut T {
43
366
    r
44
366
}
Unexecuted instantiation: getrandom::util::ptr_from_mut::<[u8]>
getrandom::util::ptr_from_mut::<[core::mem::maybe_uninit::MaybeUninit<u8>]>
Line
Count
Source
42
366
fn ptr_from_mut<T: ?Sized>(r: &mut T) -> *mut T {
43
366
    r
44
366
}
45
46
// TODO: MSRV(1.76.0): Replace with `core::ptr::from_ref`.
47
0
fn ptr_from_ref<T: ?Sized>(r: &T) -> *const T {
48
0
    r
49
0
}
50
51
/// Default implementation of `inner_u32` on top of `fill_uninit`
52
0
pub fn inner_u32() -> Result<u32, Error> {
53
0
    let mut res = MaybeUninit::<u32>::uninit();
54
0
    // SAFETY: the created slice has the same size as `res`
55
0
    let dst = unsafe {
56
0
        let p: *mut MaybeUninit<u8> = res.as_mut_ptr().cast();
57
0
        slice::from_raw_parts_mut(p, core::mem::size_of::<u32>())
58
0
    };
59
0
    crate::fill_uninit(dst)?;
60
    // SAFETY: `dst` has been fully initialized by `imp::fill_inner`
61
    // since it returned `Ok`.
62
0
    Ok(unsafe { res.assume_init() })
63
0
}
64
65
/// Default implementation of `inner_u64` on top of `fill_uninit`
66
0
pub fn inner_u64() -> Result<u64, Error> {
67
0
    let mut res = MaybeUninit::<u64>::uninit();
68
0
    // SAFETY: the created slice has the same size as `res`
69
0
    let dst = unsafe {
70
0
        let p: *mut MaybeUninit<u8> = res.as_mut_ptr().cast();
71
0
        slice::from_raw_parts_mut(p, core::mem::size_of::<u64>())
72
0
    };
73
0
    crate::fill_uninit(dst)?;
74
    // SAFETY: `dst` has been fully initialized by `imp::fill_inner`
75
    // since it returned `Ok`.
76
0
    Ok(unsafe { res.assume_init() })
77
0
}
78
79
/// Truncates `u64` and returns the lower 32 bits as `u32`
80
0
pub(crate) fn truncate(val: u64) -> u32 {
81
0
    u32::try_from(val & u64::from(u32::MAX)).expect("The higher 32 bits are masked")
82
0
}