| use core::sync::atomic::{AtomicU8, Ordering::Relaxed}; | ||
| /// Lazily caches a `bool` in an `AtomicU8`. | ||
| /// | ||
| /// Initialization is intentionally unsynchronized: concurrent callers may race | ||
| /// and run `init` more than once. Once a value is produced, it is cached and | ||
| /// reused by subsequent calls. | ||
| /// | ||
| /// Uses `Relaxed` ordering because this helper only publishes the cached | ||
| /// value itself. | ||
| pub(crate) struct LazyBool(AtomicU8); | ||
| impl LazyBool { | ||
| const UNINIT: u8 = u8::MAX; | ||
| /// Create new `LazyBool`. | ||
| pub const fn new() -> Self { | ||
| Self(AtomicU8::new(Self::UNINIT)) | ||
| } | ||
| /// Call the `init` closure and return the result after caching it. | ||
| #[cold] | ||
| fn cold_init(&self, init: impl FnOnce() -> bool) -> bool { | ||
| let val = u8::from(init()); | ||
| self.0.store(val, Relaxed); | ||
| val != 0 | ||
| } | ||
| /// Retrieve the cached value if it was already initialized or call the `init` closure | ||
| /// and return the result after caching it. | ||
| #[inline] | ||
| pub fn unsync_init(&self, init: impl FnOnce() -> bool) -> bool { | ||
| let val = self.0.load(Relaxed); | ||
| if val == Self::UNINIT { | ||
| return self.cold_init(init); | ||
| } | ||
| val != 0 | ||
| } | ||
| } |
| use core::{ | ||
| convert::Infallible, | ||
| ptr::{self, NonNull}, | ||
| sync::atomic::{AtomicPtr, Ordering::Relaxed}, | ||
| }; | ||
| /// Lazily caches a non-null pointer in an `AtomicPtr`. | ||
| /// | ||
| /// Initialization is intentionally unsynchronized: concurrent callers may race | ||
| /// and run `init` more than once. Once a value is produced, it is cached and | ||
| /// reused by subsequent calls. | ||
| /// | ||
| /// For fallible initialization (`try_unsync_init`), only successful values are | ||
| /// cached; errors are returned to the caller and are not cached. | ||
| /// | ||
| /// Uses `Ordering::Relaxed` because this helper only publishes the cached | ||
| /// pointer value. Callers must not rely on this mechanism to synchronize | ||
| /// unrelated memory side effects performed by `init`. | ||
| pub(crate) struct LazyPtr<T>(AtomicPtr<T>); | ||
| impl<T> LazyPtr<T> { | ||
| /// Create new `LazyPtr`. | ||
| pub const fn new() -> Self { | ||
| Self(AtomicPtr::new(ptr::null_mut())) | ||
| } | ||
| /// Call the `init` closure and return the result after caching it in the case of success. | ||
| #[cold] | ||
| fn cold_init<E>(&self, init: impl FnOnce() -> Result<NonNull<T>, E>) -> Result<NonNull<T>, E> { | ||
| let val = init()?; | ||
| self.0.store(val.as_ptr(), Relaxed); | ||
| Ok(val) | ||
| } | ||
| /// Retrieve the cached value if it was already initialized or call the potentially fallible | ||
| /// `init` closure and return the result after caching it in the case of success. | ||
| #[inline] | ||
| pub fn try_unsync_init<E>( | ||
| &self, | ||
| init: impl FnOnce() -> Result<NonNull<T>, E>, | ||
| ) -> Result<NonNull<T>, E> { | ||
| let p = self.0.load(Relaxed); | ||
| match NonNull::new(p) { | ||
| Some(val) => Ok(val), | ||
| None => self.cold_init(init), | ||
| } | ||
| } | ||
| /// Retrieve the cached value if it was already initialized or call the `init` closure | ||
| /// and return the result after caching it. | ||
| #[inline] | ||
| #[allow(dead_code, reason = "Some modules use only `try_unsync_init`")] | ||
| pub fn unsync_init(&self, init: impl FnOnce() -> NonNull<T>) -> NonNull<T> { | ||
| let Ok(p): Result<_, Infallible> = self.try_unsync_init(|| Ok(init())); | ||
| p | ||
| } | ||
| } |
| { | ||
| "git": { | ||
| "sha1": "314fd5ab3e6d9ef2ec90243894731865a725417d" | ||
| "sha1": "4d826731b20a09e69cca91c66aea57ab3cf00072" | ||
| }, | ||
| "path_in_vcs": "" | ||
| } |
+49
-56
@@ -7,5 +7,5 @@ # This file is automatically @generated by Cargo. | ||
| name = "anyhow" | ||
| version = "1.0.100" | ||
| version = "1.0.102" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" | ||
| checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" | ||
@@ -31,11 +31,11 @@ [[package]] | ||
| name = "bitflags" | ||
| version = "2.10.0" | ||
| version = "2.11.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" | ||
| checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" | ||
| [[package]] | ||
| name = "bumpalo" | ||
| version = "3.19.1" | ||
| version = "3.20.2" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" | ||
| checksum = "5d20789868f4b01b2f2caec9f5c4e0213b41e3e5702a50157d699ae31ced2fcb" | ||
@@ -50,5 +50,5 @@ [[package]] | ||
| name = "cc" | ||
| version = "1.2.55" | ||
| version = "1.2.56" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "47b26a0954ae34af09b50f0de26458fa95369a0d478d8236d3f93082b219bd29" | ||
| checksum = "aebf35691d1bfb0ac386a69bac2fde4dd276fb618cf8bf4f5318fe285e821bb2" | ||
| dependencies = [ | ||
@@ -85,17 +85,17 @@ "find-msvc-tools", | ||
| name = "futures-core" | ||
| version = "0.3.31" | ||
| version = "0.3.32" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" | ||
| checksum = "7e3450815272ef58cec6d564423f6e755e25379b217b0bc688e295ba24df6b1d" | ||
| [[package]] | ||
| name = "futures-task" | ||
| version = "0.3.31" | ||
| version = "0.3.32" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" | ||
| checksum = "037711b3d59c33004d3856fbdc83b99d4ff37a24768fa1be9ce3538a1cde4393" | ||
| [[package]] | ||
| name = "futures-util" | ||
| version = "0.3.31" | ||
| version = "0.3.32" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" | ||
| checksum = "389ca41296e6190b48053de0321d02a77f32f8a5d2461dd38762c0593805c6d6" | ||
| dependencies = [ | ||
@@ -105,3 +105,2 @@ "futures-core", | ||
| "pin-project-lite", | ||
| "pin-utils", | ||
| "slab", | ||
@@ -112,3 +111,3 @@ ] | ||
| name = "getrandom" | ||
| version = "0.4.1" | ||
| version = "0.4.2" | ||
| dependencies = [ | ||
@@ -173,5 +172,5 @@ "cfg-if", | ||
| name = "js-sys" | ||
| version = "0.3.85" | ||
| version = "0.3.91" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3" | ||
| checksum = "b49715b7073f385ba4bc528e5747d02e66cb39c6146efb66b781f131f0fb399c" | ||
| dependencies = [ | ||
@@ -190,5 +189,5 @@ "once_cell", | ||
| name = "libc" | ||
| version = "0.2.180" | ||
| version = "0.2.182" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" | ||
| checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112" | ||
@@ -209,5 +208,5 @@ [[package]] | ||
| name = "memchr" | ||
| version = "2.7.6" | ||
| version = "2.8.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" | ||
| checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" | ||
@@ -257,13 +256,7 @@ [[package]] | ||
| name = "pin-project-lite" | ||
| version = "0.2.16" | ||
| version = "0.2.17" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" | ||
| checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" | ||
| [[package]] | ||
| name = "pin-utils" | ||
| version = "0.1.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" | ||
| [[package]] | ||
| name = "prettyplease" | ||
@@ -298,5 +291,5 @@ version = "0.2.37" | ||
| name = "r-efi" | ||
| version = "5.3.0" | ||
| version = "6.0.0" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" | ||
| checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" | ||
@@ -387,5 +380,5 @@ [[package]] | ||
| name = "syn" | ||
| version = "2.0.114" | ||
| version = "2.0.117" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" | ||
| checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" | ||
| dependencies = [ | ||
@@ -399,5 +392,5 @@ "proc-macro2", | ||
| name = "unicode-ident" | ||
| version = "1.0.22" | ||
| version = "1.0.24" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" | ||
| checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" | ||
@@ -440,5 +433,5 @@ [[package]] | ||
| name = "wasm-bindgen" | ||
| version = "0.2.108" | ||
| version = "0.2.114" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566" | ||
| checksum = "6532f9a5c1ece3798cb1c2cfdba640b9b3ba884f5db45973a6f442510a87d38e" | ||
| dependencies = [ | ||
@@ -454,5 +447,5 @@ "cfg-if", | ||
| name = "wasm-bindgen-futures" | ||
| version = "0.4.58" | ||
| version = "0.4.64" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "70a6e77fd0ae8029c9ea0063f87c46fde723e7d887703d74ad2616d792e51e6f" | ||
| checksum = "e9c5522b3a28661442748e09d40924dfb9ca614b21c00d3fd135720e48b67db8" | ||
| dependencies = [ | ||
@@ -469,5 +462,5 @@ "cfg-if", | ||
| name = "wasm-bindgen-macro" | ||
| version = "0.2.108" | ||
| version = "0.2.114" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608" | ||
| checksum = "18a2d50fcf105fb33bb15f00e7a77b772945a2ee45dcf454961fd843e74c18e6" | ||
| dependencies = [ | ||
@@ -480,5 +473,5 @@ "quote", | ||
| name = "wasm-bindgen-macro-support" | ||
| version = "0.2.108" | ||
| version = "0.2.114" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55" | ||
| checksum = "03ce4caeaac547cdf713d280eda22a730824dd11e6b8c3ca9e42247b25c631e3" | ||
| dependencies = [ | ||
@@ -494,5 +487,5 @@ "bumpalo", | ||
| name = "wasm-bindgen-shared" | ||
| version = "0.2.108" | ||
| version = "0.2.114" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12" | ||
| checksum = "75a326b8c223ee17883a4251907455a2431acc2791c98c26279376490c378c16" | ||
| dependencies = [ | ||
@@ -504,5 +497,5 @@ "unicode-ident", | ||
| name = "wasm-bindgen-test" | ||
| version = "0.3.58" | ||
| version = "0.3.64" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "45649196a53b0b7a15101d845d44d2dda7374fc1b5b5e2bbf58b7577ff4b346d" | ||
| checksum = "6311c867385cc7d5602463b31825d454d0837a3aba7cdb5e56d5201792a3f7fe" | ||
| dependencies = [ | ||
@@ -527,5 +520,5 @@ "async-trait", | ||
| name = "wasm-bindgen-test-macro" | ||
| version = "0.3.58" | ||
| version = "0.3.64" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "f579cdd0123ac74b94e1a4a72bd963cf30ebac343f2df347da0b8df24cdebed2" | ||
| checksum = "67008cdde4769831958536b0f11b3bdd0380bde882be17fff9c2f34bb4549abd" | ||
| dependencies = [ | ||
@@ -539,5 +532,5 @@ "proc-macro2", | ||
| name = "wasm-bindgen-test-shared" | ||
| version = "0.2.108" | ||
| version = "0.2.114" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "a8145dd1593bf0fb137dbfa85b8be79ec560a447298955877804640e40c2d6ea" | ||
| checksum = "cfe29135b180b72b04c74aa97b2b4a2ef275161eff9a6c7955ea9eaedc7e1d4e" | ||
@@ -580,5 +573,5 @@ [[package]] | ||
| name = "web-sys" | ||
| version = "0.3.85" | ||
| version = "0.3.91" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "312e32e551d92129218ea9a2452120f4aabc03529ef03e4d0d82fb2780608598" | ||
| checksum = "854ba17bb104abfb26ba36da9729addc7ce7f06f5c0f90f3c391f8461cca21f9" | ||
| dependencies = [ | ||
@@ -703,4 +696,4 @@ "js-sys", | ||
| name = "zmij" | ||
| version = "1.0.19" | ||
| version = "1.0.21" | ||
| source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| checksum = "3ff05f8caa9038894637571ae6b9e29466c1f4f829d26c9b28f869a29cbe3445" | ||
| checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" |
+2
-2
@@ -16,3 +16,3 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| name = "getrandom" | ||
| version = "0.4.1" | ||
| version = "0.4.2" | ||
| authors = ["The Rand Project Developers"] | ||
@@ -112,3 +112,3 @@ build = "build.rs" | ||
| [target.'cfg(all(target_os = "uefi", getrandom_backend = "efi_rng"))'.dependencies.r-efi] | ||
| version = "5.1" | ||
| version = "6" | ||
| default-features = false | ||
@@ -115,0 +115,0 @@ |
+62
-43
@@ -7,2 +7,16 @@ # Changelog | ||
| ## [0.4.2] - 2026-03-03 | ||
| ### Changed | ||
| - Bump `r-efi` dependency to v6 [#814] | ||
| ### Fixed | ||
| - Read `errno` only when it is set [#810] | ||
| - Check the return value of `ProcessPrng` on Windows [#811] | ||
| [0.4.2]: https://github.com/rust-random/getrandom/compare/v0.4.1...v0.4.2 | ||
| [#810]: https://github.com/rust-random/getrandom/pull/810 | ||
| [#811]: https://github.com/rust-random/getrandom/pull/811 | ||
| [#814]: https://github.com/rust-random/getrandom/pull/814 | ||
| ## [0.4.1] - 2026-02-03 | ||
@@ -13,2 +27,3 @@ | ||
| [0.4.1]: https://github.com/rust-random/getrandom/compare/v0.4.0...v0.4.1 | ||
| [#801]: https://github.com/rust-random/getrandom/pull/801 | ||
@@ -28,2 +43,3 @@ | ||
| [0.4.0]: https://github.com/rust-random/getrandom/compare/v0.3.4...v0.4.0 | ||
| [#739]: https://github.com/rust-random/getrandom/pull/739 | ||
@@ -35,2 +51,3 @@ [#749]: https://github.com/rust-random/getrandom/pull/749 | ||
| [#794]: https://github.com/rust-random/getrandom/pull/794 | ||
| [#797]: https://github.com/rust-random/getrandom/pull/797 | ||
@@ -70,2 +87,3 @@ ## [0.3.4] - 2025-10-14 | ||
| [0.3.4]: https://github.com/rust-random/getrandom/compare/v0.3.3...v0.3.4 | ||
| [#667]: https://github.com/rust-random/getrandom/pull/667 | ||
@@ -91,2 +109,3 @@ [#671]: https://github.com/rust-random/getrandom/issues/671 | ||
| [0.3.3]: https://github.com/rust-random/getrandom/compare/v0.3.2...v0.3.3 | ||
| [#632]: https://github.com/rust-random/getrandom/pull/632 | ||
@@ -122,2 +141,3 @@ [#634]: https://github.com/rust-random/getrandom/pull/634 | ||
| [0.3.2]: https://github.com/rust-random/getrandom/compare/v0.3.1...v0.3.2 | ||
| [#570]: https://github.com/rust-random/getrandom/pull/570 | ||
@@ -144,2 +164,3 @@ [#572]: https://github.com/rust-random/getrandom/pull/572 | ||
| [0.3.1]: https://github.com/rust-random/getrandom/compare/v0.3.0...v0.3.1 | ||
| [#588]: https://github.com/rust-random/getrandom/pull/588 | ||
@@ -189,2 +210,3 @@ | ||
| [0.3.0]: https://github.com/rust-random/getrandom/compare/v0.2.15...v0.3.0 | ||
| [#415]: https://github.com/rust-random/getrandom/pull/415 | ||
@@ -218,2 +240,3 @@ [#440]: https://github.com/rust-random/getrandom/pull/440 | ||
| [0.2.17]: https://github.com/rust-random/getrandom/compare/v0.2.16...v0.2.17 | ||
| [#732]: https://github.com/rust-random/getrandom/pull/732 | ||
@@ -227,2 +250,3 @@ [#768]: https://github.com/rust-random/getrandom/pull/768 | ||
| [0.2.16]: https://github.com/rust-random/getrandom/compare/v0.2.15...v0.2.16 | ||
| [#654]: https://github.com/rust-random/getrandom/pull/654 | ||
@@ -238,2 +262,3 @@ | ||
| [0.2.15]: https://github.com/rust-random/getrandom/compare/v0.2.14...v0.2.15 | ||
| [#410]: https://github.com/rust-random/getrandom/pull/410 | ||
@@ -250,2 +275,3 @@ [#411]: https://github.com/rust-random/getrandom/pull/411 | ||
| [0.2.14]: https://github.com/rust-random/getrandom/compare/v0.2.13...v0.2.14 | ||
| [#408]: https://github.com/rust-random/getrandom/pull/408 | ||
@@ -265,2 +291,3 @@ | ||
| [0.2.13]: https://github.com/rust-random/getrandom/compare/v0.2.12...v0.2.13 | ||
| [#396]: https://github.com/rust-random/getrandom/pull/396 | ||
@@ -279,2 +306,3 @@ | ||
| [0.2.12]: https://github.com/rust-random/getrandom/compare/v0.2.11...v0.2.12 | ||
| [#385]: https://github.com/rust-random/getrandom/pull/385 | ||
@@ -293,2 +321,3 @@ [#386]: https://github.com/rust-random/getrandom/pull/386 | ||
| [0.2.11]: https://github.com/rust-random/getrandom/compare/v0.2.10...v0.2.11 | ||
| [#369]: https://github.com/rust-random/getrandom/pull/369 | ||
@@ -305,2 +334,3 @@ [#370]: https://github.com/rust-random/getrandom/pull/370 | ||
| [0.2.10]: https://github.com/rust-random/getrandom/compare/v0.2.9...v0.2.10 | ||
| [#359]: https://github.com/rust-random/getrandom/pull/359 | ||
@@ -339,2 +369,3 @@ [#362]: https://github.com/rust-random/getrandom/pull/362 | ||
| [0.2.9]: https://github.com/rust-random/getrandom/compare/v0.2.8...v0.2.9 | ||
| [#282]: https://github.com/rust-random/getrandom/pull/282 | ||
@@ -376,3 +407,3 @@ [#291]: https://github.com/rust-random/getrandom/pull/291 | ||
| [Web Cryptography API]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API | ||
| [0.2.8]: https://github.com/rust-random/getrandom/compare/v0.2.7...v0.2.8 | ||
| [#284]: https://github.com/rust-random/getrandom/pull/284 | ||
@@ -385,2 +416,3 @@ [#295]: https://github.com/rust-random/getrandom/pull/295 | ||
| [#276]: https://github.com/rust-random/getrandom/pull/276 | ||
| [Web Cryptography API]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API | ||
@@ -397,2 +429,3 @@ ## [0.2.7] - 2022-06-14 | ||
| [0.2.7]: https://github.com/rust-random/getrandom/compare/v0.2.6...v0.2.7 | ||
| [#263]: https://github.com/rust-random/getrandom/pull/263 | ||
@@ -410,2 +443,3 @@ [#260]: https://github.com/rust-random/getrandom/pull/260 | ||
| [0.2.6]: https://github.com/rust-random/getrandom/compare/v0.2.5...v0.2.6 | ||
| [#248]: https://github.com/rust-random/getrandom/pull/248 | ||
@@ -422,2 +456,3 @@ [#252]: https://github.com/rust-random/getrandom/pull/252 | ||
| [0.2.5]: https://github.com/rust-random/getrandom/compare/v0.2.4...v0.2.5 | ||
| [#234]: https://github.com/rust-random/getrandom/pull/234 | ||
@@ -437,2 +472,3 @@ [#244]: https://github.com/rust-random/getrandom/pull/244 | ||
| [0.2.4]: https://github.com/rust-random/getrandom/compare/v0.2.3...v0.2.4 | ||
| [#220]: https://github.com/rust-random/getrandom/pull/220 | ||
@@ -450,2 +486,3 @@ [#222]: https://github.com/rust-random/getrandom/pull/222 | ||
| [0.2.3]: https://github.com/rust-random/getrandom/compare/v0.2.2...v0.2.3 | ||
| [#205]: https://github.com/rust-random/getrandom/pull/205 | ||
@@ -460,2 +497,3 @@ [#210]: https://github.com/rust-random/getrandom/pull/210 | ||
| [0.2.2]: https://github.com/rust-random/getrandom/compare/v0.2.1...v0.2.2 | ||
| [#198]: https://github.com/rust-random/getrandom/pull/198 | ||
@@ -477,2 +515,3 @@ [#200]: https://github.com/rust-random/getrandom/pull/200 | ||
| [0.2.1]: https://github.com/rust-random/getrandom/compare/v0.2.0...v0.2.1 | ||
| [#165]: https://github.com/rust-random/getrandom/pull/165 | ||
@@ -503,2 +542,3 @@ [#166]: https://github.com/rust-random/getrandom/pull/166 | ||
| [0.2.0]: https://github.com/rust-random/getrandom/compare/v0.1.16...v0.2.0 | ||
| [#106]: https://github.com/rust-random/getrandom/pull/106 | ||
@@ -522,2 +562,3 @@ [#107]: https://github.com/rust-random/getrandom/pull/107 | ||
| [0.1.16]: https://github.com/rust-random/getrandom/compare/v0.1.15...v0.1.16 | ||
| [#173]: https://github.com/rust-random/getrandom/pull/173 | ||
@@ -532,2 +573,3 @@ [#171]: https://github.com/rust-random/getrandom/pull/171 | ||
| [0.1.15]: https://github.com/rust-random/getrandom/compare/v0.1.14...v0.1.15 | ||
| [#137]: https://github.com/rust-random/getrandom/pull/137 | ||
@@ -542,2 +584,3 @@ [#139]: https://github.com/rust-random/getrandom/pull/139 | ||
| [0.1.14]: https://github.com/rust-random/getrandom/compare/v0.1.13...v0.1.14 | ||
| [#125]: https://github.com/rust-random/getrandom/pull/125 | ||
@@ -561,2 +604,3 @@ [#126]: https://github.com/rust-random/getrandom/pull/126 | ||
| [0.1.13]: https://github.com/rust-random/getrandom/compare/v0.1.12...v0.1.13 | ||
| [#86]: https://github.com/rust-random/getrandom/pull/86 | ||
@@ -572,2 +616,3 @@ [#104]: https://github.com/rust-random/getrandom/pull/104 | ||
| [0.1.12]: https://github.com/rust-random/getrandom/compare/v0.1.11...v0.1.12 | ||
| [#100]: https://github.com/rust-random/getrandom/pull/100 | ||
@@ -580,2 +625,3 @@ | ||
| [0.1.11]: https://github.com/rust-random/getrandom/compare/v0.1.10...v0.1.11 | ||
| [#96]: https://github.com/rust-random/getrandom/pull/96 | ||
@@ -591,2 +637,3 @@ | ||
| [0.1.10]: https://github.com/rust-random/getrandom/compare/v0.1.9...v0.1.10 | ||
| [#90]: https://github.com/rust-random/getrandom/pull/90 | ||
@@ -606,2 +653,3 @@ [#92]: https://github.com/rust-random/getrandom/pull/92 | ||
| [0.1.9]: https://github.com/rust-random/getrandom/compare/v0.1.8...v0.1.9 | ||
| [#58]: https://github.com/rust-random/getrandom/pull/58 | ||
@@ -617,2 +665,3 @@ [#64]: https://github.com/rust-random/getrandom/pull/64 | ||
| [0.1.8]: https://github.com/rust-random/getrandom/compare/v0.1.7...v0.1.8 | ||
| [#74]: https://github.com/rust-random/getrandom/pull/74 | ||
@@ -637,2 +686,3 @@ | ||
| [0.1.7]: https://github.com/rust-random/getrandom/compare/v0.1.6...v0.1.7 | ||
| [#51]: https://github.com/rust-random/getrandom/pull/51 | ||
@@ -649,2 +699,3 @@ [#52]: https://github.com/rust-random/getrandom/pull/52 | ||
| [0.1.6]: https://github.com/rust-random/getrandom/compare/v0.1.5...v0.1.6 | ||
| [#48]: https://github.com/rust-random/getrandom/pull/48 | ||
@@ -660,2 +711,3 @@ | ||
| [0.1.5]: https://github.com/rust-random/getrandom/compare/v0.1.4...v0.1.5 | ||
| [#38]: https://github.com/rust-random/getrandom/issues/38 | ||
@@ -682,2 +734,3 @@ [#43]: https://github.com/rust-random/getrandom/pull/43 | ||
| [0.1.4]: https://github.com/rust-random/getrandom/compare/v0.1.3...v0.1.4 | ||
| [#30]: https://github.com/rust-random/getrandom/pull/30 | ||
@@ -698,56 +751,22 @@ [#13]: https://github.com/rust-random/getrandom/issues/13 | ||
| [0.1.3]: https://github.com/rust-random/getrandom/compare/v0.1.2...v0.1.3 | ||
| ## [0.1.2] - 2019-04-06 | ||
| - Add support for `wasm32-unknown-wasi` target. | ||
| [0.1.2]: https://github.com/rust-random/getrandom/compare/v0.1.1...v0.1.2 | ||
| ## [0.1.1] - 2019-04-05 | ||
| - Enable std functionality for CloudABI by default. | ||
| [0.1.1]: https://github.com/rust-random/getrandom/compare/v0.1.0...v0.1.1 | ||
| ## [0.1.0] - 2019-03-23 | ||
| Publish initial implementation. | ||
| [0.1.0]: https://github.com/rust-random/getrandom/compare/v0.0.0...v0.1.0 | ||
| ## [0.0.0] - 2019-01-19 | ||
| Publish an empty template library. | ||
| [0.4.1]: https://github.com/rust-random/getrandom/compare/v0.4.0...v0.4.1 | ||
| [0.4.0]: https://github.com/rust-random/getrandom/compare/v0.3.4...v0.4.0 | ||
| [0.3.4]: https://github.com/rust-random/getrandom/compare/v0.3.3...v0.3.4 | ||
| [0.3.3]: https://github.com/rust-random/getrandom/compare/v0.3.2...v0.3.3 | ||
| [0.3.2]: https://github.com/rust-random/getrandom/compare/v0.3.1...v0.3.2 | ||
| [0.3.1]: https://github.com/rust-random/getrandom/compare/v0.3.0...v0.3.1 | ||
| [0.3.0]: https://github.com/rust-random/getrandom/compare/v0.2.15...v0.3.0 | ||
| [0.2.17]: https://github.com/rust-random/getrandom/compare/v0.2.16...v0.2.17 | ||
| [0.2.16]: https://github.com/rust-random/getrandom/compare/v0.2.15...v0.2.16 | ||
| [0.2.15]: https://github.com/rust-random/getrandom/compare/v0.2.14...v0.2.15 | ||
| [0.2.14]: https://github.com/rust-random/getrandom/compare/v0.2.13...v0.2.14 | ||
| [0.2.13]: https://github.com/rust-random/getrandom/compare/v0.2.12...v0.2.13 | ||
| [0.2.12]: https://github.com/rust-random/getrandom/compare/v0.2.11...v0.2.12 | ||
| [0.2.11]: https://github.com/rust-random/getrandom/compare/v0.2.10...v0.2.11 | ||
| [0.2.10]: https://github.com/rust-random/getrandom/compare/v0.2.9...v0.2.10 | ||
| [0.2.9]: https://github.com/rust-random/getrandom/compare/v0.2.8...v0.2.9 | ||
| [0.2.8]: https://github.com/rust-random/getrandom/compare/v0.2.7...v0.2.8 | ||
| [0.2.7]: https://github.com/rust-random/getrandom/compare/v0.2.6...v0.2.7 | ||
| [0.2.6]: https://github.com/rust-random/getrandom/compare/v0.2.5...v0.2.6 | ||
| [0.2.5]: https://github.com/rust-random/getrandom/compare/v0.2.4...v0.2.5 | ||
| [0.2.4]: https://github.com/rust-random/getrandom/compare/v0.2.3...v0.2.4 | ||
| [0.2.3]: https://github.com/rust-random/getrandom/compare/v0.2.2...v0.2.3 | ||
| [0.2.2]: https://github.com/rust-random/getrandom/compare/v0.2.1...v0.2.2 | ||
| [0.2.1]: https://github.com/rust-random/getrandom/compare/v0.2.0...v0.2.1 | ||
| [0.2.0]: https://github.com/rust-random/getrandom/compare/v0.1.16...v0.2.0 | ||
| [0.1.16]: https://github.com/rust-random/getrandom/compare/v0.1.15...v0.1.16 | ||
| [0.1.15]: https://github.com/rust-random/getrandom/compare/v0.1.14...v0.1.15 | ||
| [0.1.14]: https://github.com/rust-random/getrandom/compare/v0.1.13...v0.1.14 | ||
| [0.1.13]: https://github.com/rust-random/getrandom/compare/v0.1.12...v0.1.13 | ||
| [0.1.12]: https://github.com/rust-random/getrandom/compare/v0.1.11...v0.1.12 | ||
| [0.1.11]: https://github.com/rust-random/getrandom/compare/v0.1.10...v0.1.11 | ||
| [0.1.10]: https://github.com/rust-random/getrandom/compare/v0.1.9...v0.1.10 | ||
| [0.1.9]: https://github.com/rust-random/getrandom/compare/v0.1.8...v0.1.9 | ||
| [0.1.8]: https://github.com/rust-random/getrandom/compare/v0.1.7...v0.1.8 | ||
| [0.1.7]: https://github.com/rust-random/getrandom/compare/v0.1.6...v0.1.7 | ||
| [0.1.6]: https://github.com/rust-random/getrandom/compare/v0.1.5...v0.1.6 | ||
| [0.1.5]: https://github.com/rust-random/getrandom/compare/v0.1.4...v0.1.5 | ||
| [0.1.4]: https://github.com/rust-random/getrandom/compare/v0.1.3...v0.1.4 | ||
| [0.1.3]: https://github.com/rust-random/getrandom/compare/v0.1.2...v0.1.3 | ||
| [0.1.2]: https://github.com/rust-random/getrandom/compare/v0.1.1...v0.1.2 | ||
| [0.1.1]: https://github.com/rust-random/getrandom/compare/v0.1.0...v0.1.1 | ||
| [0.1.0]: https://github.com/rust-random/getrandom/compare/v0.0.0...v0.1.0 | ||
| [0.0.0]: https://github.com/rust-random/getrandom/releases/tag/v0.0.0 |
@@ -5,4 +5,3 @@ //! Implementation for UEFI using EFI_RNG_PROTOCOL | ||
| mem::MaybeUninit, | ||
| ptr::{self, NonNull, null_mut}, | ||
| sync::atomic::{AtomicPtr, Ordering::Relaxed}, | ||
| ptr::{self, NonNull}, | ||
| }; | ||
@@ -21,4 +20,2 @@ use r_efi::{ | ||
| static RNG_PROTOCOL: AtomicPtr<rng::Protocol> = AtomicPtr::new(null_mut()); | ||
| #[cold] | ||
@@ -41,3 +38,3 @@ #[inline(never)] | ||
| &mut guid, | ||
| null_mut(), | ||
| ptr::null_mut(), | ||
| &mut buf_size, | ||
@@ -94,3 +91,2 @@ handles.as_mut_ptr(), | ||
| RNG_PROTOCOL.store(protocol.as_ptr(), Relaxed); | ||
| return Ok(protocol); | ||
@@ -103,7 +99,9 @@ } | ||
| pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
| let protocol = match NonNull::new(RNG_PROTOCOL.load(Relaxed)) { | ||
| Some(p) => p, | ||
| None => init()?, | ||
| }; | ||
| #[path = "../utils/lazy_ptr.rs"] | ||
| mod lazy; | ||
| static RNG_PROTOCOL: lazy::LazyPtr<rng::Protocol> = lazy::LazyPtr::new(); | ||
| let protocol = RNG_PROTOCOL.try_unsync_init(init)?; | ||
| let mut alg_guid = rng::ALGORITHM_RAW; | ||
@@ -110,0 +108,0 @@ let ret = unsafe { |
| //! Implementation using getentropy(2) | ||
| //! | ||
| //! When porting to a new target, ensure that its implementation follows the | ||
| //! POSIX conventions from | ||
| //! <https://pubs.opengroup.org/onlinepubs/9799919799/functions/getentropy.html>. | ||
| //! | ||
| //! Available since: | ||
@@ -20,7 +24,12 @@ //! - macOS 10.12 | ||
| pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
| for chunk in dest.chunks_mut(256) { | ||
| // https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/limits.h.html | ||
| // says `GETENTROPY_MAX` is at least 256. | ||
| const GETENTROPY_MAX: usize = 256; | ||
| for chunk in dest.chunks_mut(GETENTROPY_MAX) { | ||
| let ret = unsafe { libc::getentropy(chunk.as_mut_ptr().cast::<c_void>(), chunk.len()) }; | ||
| if ret != 0 { | ||
| let errno = utils::get_errno(); | ||
| return Err(Error::from_errno(errno)); | ||
| match ret { | ||
| 0 => continue, | ||
| -1 => return Err(Error::from_errno(utils::get_errno())), | ||
| _ => return Err(Error::UNEXPECTED), | ||
| } | ||
@@ -27,0 +36,0 @@ } |
@@ -7,4 +7,3 @@ //! Implementation for Linux / Android with `/dev/urandom` fallback | ||
| mem::{MaybeUninit, transmute}, | ||
| ptr::NonNull, | ||
| sync::atomic::{AtomicPtr, Ordering}, | ||
| ptr::{self, NonNull}, | ||
| }; | ||
@@ -21,4 +20,2 @@ use use_file::utils; | ||
| static GETRANDOM_FN: AtomicPtr<c_void> = AtomicPtr::new(core::ptr::null_mut()); | ||
| #[cold] | ||
@@ -29,7 +26,3 @@ #[inline(never)] | ||
| #[cfg(not(target_env = "musl"))] | ||
| let raw_ptr = { | ||
| static NAME: &[u8] = b"getrandom\0"; | ||
| let name_ptr = NAME.as_ptr().cast::<libc::c_char>(); | ||
| unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) } | ||
| }; | ||
| let raw_ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, c"getrandom".as_ptr()) }; | ||
| #[cfg(target_env = "musl")] | ||
@@ -43,6 +36,5 @@ let raw_ptr = { | ||
| Some(fptr) => { | ||
| let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) }; | ||
| let dangling_ptr = NonNull::dangling().as_ptr(); | ||
| let getrandom_fn = unsafe { transmute::<*mut c_void, GetRandomFn>(fptr.as_ptr()) }; | ||
| // Check that `getrandom` syscall is supported by kernel | ||
| let res = unsafe { getrandom_fn(dangling_ptr, 0, 0) }; | ||
| let res = unsafe { getrandom_fn(ptr::dangling_mut(), 0, 0) }; | ||
| if cfg!(getrandom_test_linux_fallback) { | ||
@@ -72,3 +64,2 @@ NOT_AVAILABLE | ||
| GETRANDOM_FN.store(res_ptr.as_ptr(), Ordering::Release); | ||
| res_ptr | ||
@@ -85,14 +76,8 @@ } | ||
| pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
| // Despite being only a single atomic variable, we still cannot always use | ||
| // Ordering::Relaxed, as we need to make sure a successful call to `init` | ||
| // is "ordered before" any data read through the returned pointer (which | ||
| // occurs when the function is called). Our implementation mirrors that of | ||
| // the one in libstd, meaning that the use of non-Relaxed operations is | ||
| // probably unnecessary. | ||
| let raw_ptr = GETRANDOM_FN.load(Ordering::Acquire); | ||
| let fptr = match NonNull::new(raw_ptr) { | ||
| Some(p) => p, | ||
| None => init(), | ||
| }; | ||
| #[path = "../utils/lazy_ptr.rs"] | ||
| mod lazy; | ||
| static GETRANDOM_FN: lazy::LazyPtr<c_void> = lazy::LazyPtr::new(); | ||
| let fptr = GETRANDOM_FN.unsync_init(init); | ||
| if fptr == NOT_AVAILABLE { | ||
@@ -102,3 +87,3 @@ use_file_fallback(dest) | ||
| // note: `transmute` is currently the only way to convert a pointer into a function reference | ||
| let getrandom_fn = unsafe { transmute::<NonNull<c_void>, GetRandomFn>(fptr) }; | ||
| let getrandom_fn = unsafe { transmute::<*mut c_void, GetRandomFn>(fptr.as_ptr()) }; | ||
| utils::sys_fill_exact(dest, |buf| unsafe { | ||
@@ -105,0 +90,0 @@ getrandom_fn(buf.as_mut_ptr().cast(), buf.len(), 0) |
+17
-25
@@ -11,4 +11,3 @@ //! Implementation for NetBSD | ||
| mem::{self, MaybeUninit}, | ||
| ptr, | ||
| sync::atomic::{AtomicPtr, Ordering}, | ||
| ptr::{self, NonNull}, | ||
| }; | ||
@@ -46,17 +45,14 @@ | ||
| static GETRANDOM: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut()); | ||
| #[cold] | ||
| #[inline(never)] | ||
| fn init() -> *mut c_void { | ||
| static NAME: &[u8] = b"getrandom\0"; | ||
| let name_ptr = NAME.as_ptr().cast::<libc::c_char>(); | ||
| let mut ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, name_ptr) }; | ||
| if ptr.is_null() || cfg!(getrandom_test_netbsd_fallback) { | ||
| // Verify `polyfill_using_kern_arand` has the right signature. | ||
| const POLYFILL: GetRandomFn = polyfill_using_kern_arand; | ||
| ptr = POLYFILL as *mut c_void; | ||
| fn init() -> NonNull<c_void> { | ||
| let ptr = unsafe { libc::dlsym(libc::RTLD_DEFAULT, c"getrandom".as_ptr()) }; | ||
| if !cfg!(getrandom_test_netbsd_fallback) { | ||
| if let Some(ptr) = NonNull::new(ptr) { | ||
| return ptr; | ||
| } | ||
| } | ||
| GETRANDOM.store(ptr, Ordering::Release); | ||
| ptr | ||
| // Verify `polyfill_using_kern_arand` has the right signature. | ||
| const POLYFILL: GetRandomFn = polyfill_using_kern_arand; | ||
| unsafe { NonNull::new_unchecked(POLYFILL as *mut c_void) } | ||
| } | ||
@@ -66,13 +62,9 @@ | ||
| pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
| // Despite being only a single atomic variable, we still cannot always use | ||
| // Ordering::Relaxed, as we need to make sure a successful call to `init` | ||
| // is "ordered before" any data read through the returned pointer (which | ||
| // occurs when the function is called). Our implementation mirrors that of | ||
| // the one in libstd, meaning that the use of non-Relaxed operations is | ||
| // probably unnecessary. | ||
| let mut fptr = GETRANDOM.load(Ordering::Acquire); | ||
| if fptr.is_null() { | ||
| fptr = init(); | ||
| } | ||
| let fptr = unsafe { mem::transmute::<*mut c_void, GetRandomFn>(fptr) }; | ||
| #[path = "../utils/lazy_ptr.rs"] | ||
| mod lazy; | ||
| static GETRANDOM_FN: lazy::LazyPtr<c_void> = lazy::LazyPtr::new(); | ||
| let fptr = GETRANDOM_FN.unsync_init(init); | ||
| let fptr = unsafe { mem::transmute::<*mut c_void, GetRandomFn>(fptr.as_ptr()) }; | ||
| utils::sys_fill_exact(dest, |buf| unsafe { | ||
@@ -79,0 +71,0 @@ fptr(buf.as_mut_ptr().cast::<c_void>(), buf.len(), 0) |
@@ -5,5 +5,2 @@ //! RDRAND backend for x86(-64) targets | ||
| #[path = "../utils/lazy.rs"] | ||
| mod lazy; | ||
| #[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))] | ||
@@ -24,4 +21,2 @@ compile_error!("`rdrand` backend can be enabled only for x86 and x86-64 targets!"); | ||
| static RDRAND_GOOD: lazy::LazyBool = lazy::LazyBool::new(); | ||
| // Recommendation from "Intel® Digital Random Number Generator (DRNG) Software | ||
@@ -77,3 +72,5 @@ // Implementation Guide" - Section 5.2.1 and "Intel® 64 and IA-32 Architectures | ||
| fn is_rdrand_good() -> bool { | ||
| #[cold] | ||
| #[inline(never)] | ||
| fn init() -> bool { | ||
| #[cfg(not(target_feature = "rdrand"))] | ||
@@ -121,2 +118,11 @@ { | ||
| fn is_rdrand_good() -> bool { | ||
| #[path = "../utils/lazy_bool.rs"] | ||
| mod lazy; | ||
| static RDRAND_GOOD: lazy::LazyBool = lazy::LazyBool::new(); | ||
| RDRAND_GOOD.unsync_init(init) | ||
| } | ||
| #[target_feature(enable = "rdrand")] | ||
@@ -169,3 +175,3 @@ fn rdrand_exact(dest: &mut [MaybeUninit<u8>]) -> Option<()> { | ||
| pub fn inner_u32() -> Result<u32, Error> { | ||
| if !RDRAND_GOOD.unsync_init(is_rdrand_good) { | ||
| if !is_rdrand_good() { | ||
| return Err(Error::NO_RDRAND); | ||
@@ -179,3 +185,3 @@ } | ||
| pub fn inner_u64() -> Result<u64, Error> { | ||
| if !RDRAND_GOOD.unsync_init(is_rdrand_good) { | ||
| if !is_rdrand_good() { | ||
| return Err(Error::NO_RDRAND); | ||
@@ -189,3 +195,3 @@ } | ||
| pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
| if !RDRAND_GOOD.unsync_init(is_rdrand_good) { | ||
| if !is_rdrand_good() { | ||
| return Err(Error::NO_RDRAND); | ||
@@ -192,0 +198,0 @@ } |
@@ -72,4 +72,5 @@ //! RNDR register backend for aarch64 targets | ||
| fn is_rndr_available() -> bool { | ||
| #[path = "../utils/lazy.rs"] | ||
| #[path = "../utils/lazy_bool.rs"] | ||
| mod lazy; | ||
| static RNDR_GOOD: lazy::LazyBool = lazy::LazyBool::new(); | ||
@@ -76,0 +77,0 @@ |
@@ -41,5 +41,9 @@ //! Implementation for VxWorks | ||
| let ret = unsafe { libc::randABytes(p, chunk_len) }; | ||
| if ret != 0 { | ||
| let errno = unsafe { libc::errnoGet() }; | ||
| return Err(Error::from_errno(errno)); | ||
| match ret { | ||
| 0 => continue, | ||
| -1 => { | ||
| let errno = unsafe { libc::errnoGet() }; | ||
| return Err(Error::from_errno(errno)); | ||
| } | ||
| _ => return Err(Error::UNEXPECTED), | ||
| } | ||
@@ -46,0 +50,0 @@ } |
@@ -52,7 +52,23 @@ //! Implementation for Windows 10 and later | ||
| let result = unsafe { ProcessPrng(dest.as_mut_ptr().cast::<u8>(), dest.len()) }; | ||
| // `ProcessPrng` is documented to always return TRUE. All potential errors are handled | ||
| // during loading of `BCryptPrimitive.dll`. See the "Process base PRNG" section | ||
| // in the aforementioned Windows RNG whitepaper for more information. | ||
| debug_assert!(result == TRUE); | ||
| Ok(()) | ||
| // On Windows 10 and later, `ProcessPrng` is documented to always return | ||
| // TRUE. All potential errors are handled during loading of | ||
| // `BCryptPrimitive.dll`. See the "Process base PRNG" section in the | ||
| // aforementioned Windows RNG whitepaper for more information. | ||
| // | ||
| // The Zig project found that Windows 8 implements `ProcessPrng` in a way | ||
| // that may fail and return a value other than `TRUE`. Although recent | ||
| // versions of the Rust toolchain do not support Windows 8, we cannot rule | ||
| // out this backend being used in an executable that will run on Windows 8 | ||
| // (e.g. a fork of this crate backported to have an MSRV lower than 1.76, | ||
| // or a fork of the Rust toolchain to support older Windows versions, or | ||
| // other build hacks). | ||
| // | ||
| // Further, Wine's implementation of `ProcessPrng` CAN fail, in every | ||
| // version through Wine 11.2, and this may be the case for any other Windows | ||
| // emulation layers. | ||
| if result == TRUE { | ||
| Ok(()) | ||
| } else { | ||
| Err(Error::UNEXPECTED) | ||
| } | ||
| } |
| //! Helpers built around pointer-sized atomics. | ||
| use core::sync::atomic::{AtomicUsize, Ordering}; | ||
| // This structure represents a lazily initialized static usize value. Useful | ||
| // when it is preferable to just rerun initialization instead of locking. | ||
| // unsync_init will invoke an init() function until it succeeds, then return the | ||
| // cached value for future calls. | ||
| // | ||
| // unsync_init supports init() "failing". If the init() method returns UNINIT, | ||
| // that value will be returned as normal, but will not be cached. | ||
| // | ||
| // Users should only depend on the _value_ returned by init() functions. | ||
| // Specifically, for the following init() function: | ||
| // fn init() -> usize { | ||
| // a(); | ||
| // let v = b(); | ||
| // c(); | ||
| // v | ||
| // } | ||
| // the effects of c() or writes to shared memory will not necessarily be | ||
| // observed and additional synchronization methods may be needed. | ||
| struct LazyUsize(AtomicUsize); | ||
| impl LazyUsize { | ||
| // The initialization is not completed. | ||
| const UNINIT: usize = usize::MAX; | ||
| const fn new() -> Self { | ||
| Self(AtomicUsize::new(Self::UNINIT)) | ||
| } | ||
| // Runs the init() function at most once, returning the value of some run of | ||
| // init(). Multiple callers can run their init() functions in parallel. | ||
| // init() should always return the same value, if it succeeds. | ||
| fn unsync_init(&self, init: impl FnOnce() -> usize) -> usize { | ||
| #[cold] | ||
| fn do_init(this: &LazyUsize, init: impl FnOnce() -> usize) -> usize { | ||
| let val = init(); | ||
| this.0.store(val, Ordering::Relaxed); | ||
| val | ||
| } | ||
| // Relaxed ordering is fine, as we only have a single atomic variable. | ||
| let val = self.0.load(Ordering::Relaxed); | ||
| if val != Self::UNINIT { | ||
| val | ||
| } else { | ||
| do_init(self, init) | ||
| } | ||
| } | ||
| } | ||
| // Identical to LazyUsize except with bool instead of usize. | ||
| pub(crate) struct LazyBool(LazyUsize); | ||
| impl LazyBool { | ||
| pub const fn new() -> Self { | ||
| Self(LazyUsize::new()) | ||
| } | ||
| pub fn unsync_init(&self, init: impl FnOnce() -> bool) -> bool { | ||
| self.0.unsync_init(|| usize::from(init())) != 0 | ||
| } | ||
| } |
Sorry, the diff of this file is not supported yet