summaryrefslogtreecommitdiff
path: root/vendor/rustix/src/rand/getrandom.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/rustix/src/rand/getrandom.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/rustix/src/rand/getrandom.rs')
-rw-r--r--vendor/rustix/src/rand/getrandom.rs43
1 files changed, 43 insertions, 0 deletions
diff --git a/vendor/rustix/src/rand/getrandom.rs b/vendor/rustix/src/rand/getrandom.rs
new file mode 100644
index 0000000..c7f117a
--- /dev/null
+++ b/vendor/rustix/src/rand/getrandom.rs
@@ -0,0 +1,43 @@
+#![allow(unsafe_code)]
+
+use crate::buffer::split_init;
+use crate::{backend, io};
+use core::mem::MaybeUninit;
+
+pub use backend::rand::types::GetRandomFlags;
+
+/// `getrandom(buf, flags)`—Reads a sequence of random bytes.
+///
+/// This is a very low-level API which may be difficult to use correctly. Most
+/// users should prefer to use [`getrandom`] or [`rand`] APIs instead.
+///
+/// [`getrandom`]: https://crates.io/crates/getrandom
+/// [`rand`]: https://crates.io/crates/rand
+///
+/// # References
+/// - [Linux]
+///
+/// [Linux]: https://man7.org/linux/man-pages/man2/getrandom.2.html
+#[inline]
+pub fn getrandom(buf: &mut [u8], flags: GetRandomFlags) -> io::Result<usize> {
+ unsafe { backend::rand::syscalls::getrandom(buf.as_mut_ptr(), buf.len(), flags) }
+}
+
+/// `getrandom(buf, flags)`—Reads a sequence of random bytes.
+///
+/// This is identical to [`getrandom`], except that it can read into
+/// uninitialized memory. It returns the slice that was initialized by this
+/// function and the slice that remains uninitialized.
+#[inline]
+pub fn getrandom_uninit(
+ buf: &mut [MaybeUninit<u8>],
+ flags: GetRandomFlags,
+) -> io::Result<(&mut [u8], &mut [MaybeUninit<u8>])> {
+ // Get number of initialized bytes.
+ let length = unsafe {
+ backend::rand::syscalls::getrandom(buf.as_mut_ptr() as *mut u8, buf.len(), flags)
+ };
+
+ // Split into the initialized and uninitialized portions.
+ Ok(unsafe { split_init(buf, length?) })
+}