Skip to content

Commit 515ad2c

Browse files
committed
Rework fallability
1 parent 0e626c7 commit 515ad2c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+824
-784
lines changed

.github/workflows/gh-pages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
rm target/doc/.lock
3636
3737
- name: Setup Pages
38-
uses: actions/configure-pages@v4
38+
uses: actions/configure-pages@v5
3939

4040
- name: Upload artifact
4141
uses: actions/upload-pages-artifact@v3

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ A [separate changelog is kept for rand_core](rand_core/CHANGELOG.md).
99
You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.html) useful.
1010

1111
## [Unreleased]
12+
- Add `rand::distributions::WeightedIndex::{weight, weights, total_weight}` (#1420)
1213
- Bump the MSRV to 1.61.0
1314

1415
## [0.9.0-alpha.1] - 2024-03-18

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ compiler versions will be compatible. This is especially true of Rand's
9292
experimental `simd_support` feature.
9393

9494
Rand supports limited functionality in `no_std` mode (enabled via
95-
`default-features = false`). In this case, `OsRng` and `from_entropy` are
95+
`default-features = false`). In this case, `OsRng` and `from_os_rng` are
9696
unavailable (unless `getrandom` is enabled), large parts of `seq` are
9797
unavailable (unless `alloc` is enabled), and `thread_rng` and `random` are
9898
unavailable.

rand_chacha/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Links:
3636
`rand_chacha` is `no_std` compatible when disabling default features; the `std`
3737
feature can be explicitly required to re-enable `std` support. Using `std`
3838
allows detection of CPU features and thus better optimisation. Using `std`
39-
also enables `getrandom` functionality, such as `ChaCha20Rng::from_entropy()`.
39+
also enables `getrandom` functionality, such as `ChaCha20Rng::from_os_rng()`.
4040

4141

4242
# License

rand_chacha/src/chacha.rs

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
use self::core::fmt;
1515
use crate::guts::ChaCha;
1616
use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng};
17-
use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
17+
use rand_core::{CryptoRng, RngCore, SeedableRng};
1818

19-
#[cfg(feature = "serde1")] use serde::{Serialize, Deserialize, Serializer, Deserializer};
19+
#[cfg(feature = "serde1")]
20+
use serde::{Deserialize, Deserializer, Serialize, Serializer};
2021

2122
// NB. this must remain consistent with some currently hard-coded numbers in this module
2223
const BUF_BLOCKS: u8 = 4;
@@ -68,7 +69,7 @@ impl<T> fmt::Debug for Array64<T> {
6869
}
6970

7071
macro_rules! chacha_impl {
71-
($ChaChaXCore:ident, $ChaChaXRng:ident, $rounds:expr, $doc:expr, $abst:ident) => {
72+
($ChaChaXCore:ident, $ChaChaXRng:ident, $rounds:expr, $doc:expr, $abst:ident,) => {
7273
#[doc=$doc]
7374
#[derive(Clone, PartialEq, Eq)]
7475
pub struct $ChaChaXCore {
@@ -85,6 +86,7 @@ macro_rules! chacha_impl {
8586
impl BlockRngCore for $ChaChaXCore {
8687
type Item = u32;
8788
type Results = Array64<u32>;
89+
8890
#[inline]
8991
fn generate(&mut self, r: &mut Self::Results) {
9092
self.state.refill4($rounds, &mut r.0);
@@ -93,9 +95,12 @@ macro_rules! chacha_impl {
9395

9496
impl SeedableRng for $ChaChaXCore {
9597
type Seed = [u8; 32];
98+
9699
#[inline]
97100
fn from_seed(seed: Self::Seed) -> Self {
98-
$ChaChaXCore { state: ChaCha::new(&seed, &[0u8; 8]) }
101+
$ChaChaXCore {
102+
state: ChaCha::new(&seed, &[0u8; 8]),
103+
}
99104
}
100105
}
101106

@@ -146,6 +151,7 @@ macro_rules! chacha_impl {
146151

147152
impl SeedableRng for $ChaChaXRng {
148153
type Seed = [u8; 32];
154+
149155
#[inline]
150156
fn from_seed(seed: Self::Seed) -> Self {
151157
let core = $ChaChaXCore::from_seed(seed);
@@ -160,18 +166,16 @@ macro_rules! chacha_impl {
160166
fn next_u32(&mut self) -> u32 {
161167
self.rng.next_u32()
162168
}
169+
163170
#[inline]
164171
fn next_u64(&mut self) -> u64 {
165172
self.rng.next_u64()
166173
}
174+
167175
#[inline]
168176
fn fill_bytes(&mut self, bytes: &mut [u8]) {
169177
self.rng.fill_bytes(bytes)
170178
}
171-
#[inline]
172-
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), Error> {
173-
self.rng.try_fill_bytes(bytes)
174-
}
175179
}
176180

177181
impl $ChaChaXRng {
@@ -209,11 +213,9 @@ macro_rules! chacha_impl {
209213
#[inline]
210214
pub fn set_word_pos(&mut self, word_offset: u128) {
211215
let block = (word_offset / u128::from(BLOCK_WORDS)) as u64;
216+
self.rng.core.state.set_block_pos(block);
212217
self.rng
213-
.core
214-
.state
215-
.set_block_pos(block);
216-
self.rng.generate_and_set((word_offset % u128::from(BLOCK_WORDS)) as usize);
218+
.generate_and_set((word_offset % u128::from(BLOCK_WORDS)) as usize);
217219
}
218220

219221
/// Set the stream number.
@@ -229,10 +231,7 @@ macro_rules! chacha_impl {
229231
/// indirectly via `set_word_pos`), but this is not directly supported.
230232
#[inline]
231233
pub fn set_stream(&mut self, stream: u64) {
232-
self.rng
233-
.core
234-
.state
235-
.set_nonce(stream);
234+
self.rng.core.state.set_nonce(stream);
236235
if self.rng.index() != 64 {
237236
let wp = self.get_word_pos();
238237
self.set_word_pos(wp);
@@ -242,24 +241,20 @@ macro_rules! chacha_impl {
242241
/// Get the stream number.
243242
#[inline]
244243
pub fn get_stream(&self) -> u64 {
245-
self.rng
246-
.core
247-
.state
248-
.get_nonce()
244+
self.rng.core.state.get_nonce()
249245
}
250246

251247
/// Get the seed.
252248
#[inline]
253249
pub fn get_seed(&self) -> [u8; 32] {
254-
self.rng
255-
.core
256-
.state
257-
.get_seed()
250+
self.rng.core.state.get_seed()
258251
}
259252
}
260253

261254
impl CryptoRng for $ChaChaXRng {}
262255

256+
rand_core::impl_try_crypto_rng_from_crypto_rng!($ChaChaXRng);
257+
263258
impl From<$ChaChaXCore> for $ChaChaXRng {
264259
fn from(core: $ChaChaXCore) -> Self {
265260
$ChaChaXRng {
@@ -286,22 +281,20 @@ macro_rules! chacha_impl {
286281
}
287282
#[cfg(feature = "serde1")]
288283
impl<'de> Deserialize<'de> for $ChaChaXRng {
289-
fn deserialize<D>(d: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
284+
fn deserialize<D>(d: D) -> Result<Self, D::Error>
285+
where D: Deserializer<'de> {
290286
$abst::$ChaChaXRng::deserialize(d).map(|x| Self::from(&x))
291287
}
292288
}
293289

294290
mod $abst {
295-
#[cfg(feature = "serde1")] use serde::{Serialize, Deserialize};
291+
#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize};
296292

297293
// The abstract state of a ChaCha stream, independent of implementation choices. The
298294
// comparison and serialization of this object is considered a semver-covered part of
299295
// the API.
300296
#[derive(Debug, PartialEq, Eq)]
301-
#[cfg_attr(
302-
feature = "serde1",
303-
derive(Serialize, Deserialize),
304-
)]
297+
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
305298
pub(crate) struct $ChaChaXRng {
306299
seed: [u8; 32],
307300
stream: u64,
@@ -331,27 +324,45 @@ macro_rules! chacha_impl {
331324
}
332325
}
333326
}
334-
}
327+
};
335328
}
336329

337-
chacha_impl!(ChaCha20Core, ChaCha20Rng, 10, "ChaCha with 20 rounds", abstract20);
338-
chacha_impl!(ChaCha12Core, ChaCha12Rng, 6, "ChaCha with 12 rounds", abstract12);
339-
chacha_impl!(ChaCha8Core, ChaCha8Rng, 4, "ChaCha with 8 rounds", abstract8);
330+
chacha_impl!(
331+
ChaCha20Core,
332+
ChaCha20Rng,
333+
10,
334+
"ChaCha with 20 rounds",
335+
abstract20,
336+
);
337+
chacha_impl!(
338+
ChaCha12Core,
339+
ChaCha12Rng,
340+
6,
341+
"ChaCha with 12 rounds",
342+
abstract12,
343+
);
344+
chacha_impl!(
345+
ChaCha8Core,
346+
ChaCha8Rng,
347+
4,
348+
"ChaCha with 8 rounds",
349+
abstract8,
350+
);
340351

341352
#[cfg(test)]
342353
mod test {
343354
use rand_core::{RngCore, SeedableRng};
344355

345-
#[cfg(feature = "serde1")] use super::{ChaCha20Rng, ChaCha12Rng, ChaCha8Rng};
356+
#[cfg(feature = "serde1")] use super::{ChaCha12Rng, ChaCha20Rng, ChaCha8Rng};
346357

347358
type ChaChaRng = super::ChaCha20Rng;
348359

349360
#[cfg(feature = "serde1")]
350361
#[test]
351362
fn test_chacha_serde_roundtrip() {
352363
let seed = [
353-
1, 0, 52, 0, 0, 0, 0, 0, 1, 0, 10, 0, 22, 32, 0, 0, 2, 0, 55, 49, 0, 11, 0, 0, 3, 0, 0, 0, 0,
354-
0, 2, 92,
364+
1, 0, 52, 0, 0, 0, 0, 0, 1, 0, 10, 0, 22, 32, 0, 0, 2, 0, 55, 49, 0, 11, 0, 0, 3, 0, 0,
365+
0, 0, 0, 2, 92,
355366
];
356367
let mut rng1 = ChaCha20Rng::from_seed(seed);
357368
let mut rng2 = ChaCha12Rng::from_seed(seed);
@@ -388,7 +399,7 @@ mod test {
388399
#[test]
389400
fn test_chacha_serde_format_stability() {
390401
let j = r#"{"seed":[4,8,15,16,23,42,4,8,15,16,23,42,4,8,15,16,23,42,4,8,15,16,23,42,4,8,15,16,23,42,4,8],"stream":27182818284,"word_pos":314159265359}"#;
391-
let r: ChaChaRng = serde_json::from_str(&j).unwrap();
402+
let r: ChaChaRng = serde_json::from_str(j).unwrap();
392403
let j1 = serde_json::to_string(&r).unwrap();
393404
assert_eq!(j, j1);
394405
}
@@ -402,7 +413,7 @@ mod test {
402413
let mut rng1 = ChaChaRng::from_seed(seed);
403414
assert_eq!(rng1.next_u32(), 137206642);
404415

405-
let mut rng2 = ChaChaRng::from_rng(rng1).unwrap();
416+
let mut rng2 = ChaChaRng::from_rng(&mut rng1);
406417
assert_eq!(rng2.next_u32(), 1325750369);
407418
}
408419

@@ -598,7 +609,7 @@ mod test {
598609

599610
#[test]
600611
fn test_chacha_word_pos_wrap_exact() {
601-
use super::{BUF_BLOCKS, BLOCK_WORDS};
612+
use super::{BLOCK_WORDS, BUF_BLOCKS};
602613
let mut rng = ChaChaRng::from_seed(Default::default());
603614
// refilling the buffer in set_word_pos will wrap the block counter to 0
604615
let last_block = (1 << 68) - u128::from(BUF_BLOCKS * BLOCK_WORDS);

rand_core/src/blanket_impls.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#[cfg(feature = "alloc")] use alloc::boxed::Box;
2+
3+
use crate::{CryptoRng, RngCore, TryCryptoRng, TryRngCore};
4+
5+
impl<'a, R: RngCore + ?Sized> RngCore for &'a mut R {
6+
#[inline(always)]
7+
fn next_u32(&mut self) -> u32 {
8+
R::next_u32(self)
9+
}
10+
11+
#[inline(always)]
12+
fn next_u64(&mut self) -> u64 {
13+
R::next_u64(self)
14+
}
15+
16+
#[inline(always)]
17+
fn fill_bytes(&mut self, dst: &mut [u8]) {
18+
R::fill_bytes(self, dst)
19+
}
20+
}
21+
22+
impl<'a, R: CryptoRng + ?Sized> CryptoRng for &'a mut R {}
23+
24+
impl<'a, R: TryRngCore + ?Sized> TryRngCore for &'a mut R {
25+
type Error = R::Error;
26+
27+
#[inline(always)]
28+
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
29+
R::try_next_u32(self)
30+
}
31+
32+
#[inline(always)]
33+
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
34+
R::try_next_u64(self)
35+
}
36+
37+
#[inline(always)]
38+
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
39+
R::try_fill_bytes(self, dst)
40+
}
41+
}
42+
43+
impl<'a, R: TryCryptoRng + ?Sized> TryCryptoRng for &'a mut R {}
44+
45+
#[cfg(feature = "alloc")]
46+
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
47+
impl<R: RngCore + ?Sized> RngCore for Box<R> {
48+
#[inline(always)]
49+
fn next_u32(&mut self) -> u32 {
50+
R::next_u32(self)
51+
}
52+
53+
#[inline(always)]
54+
fn next_u64(&mut self) -> u64 {
55+
R::next_u64(self)
56+
}
57+
58+
#[inline(always)]
59+
fn fill_bytes(&mut self, dest: &mut [u8]) {
60+
R::fill_bytes(self, dest)
61+
}
62+
}
63+
64+
#[cfg(feature = "alloc")]
65+
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
66+
impl<R: CryptoRng + ?Sized> CryptoRng for Box<R> {}
67+
68+
#[cfg(feature = "alloc")]
69+
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
70+
impl<R: TryRngCore + ?Sized> TryRngCore for Box<R> {
71+
type Error = R::Error;
72+
73+
#[inline(always)]
74+
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
75+
R::try_next_u32(self)
76+
}
77+
78+
#[inline(always)]
79+
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
80+
R::try_next_u64(self)
81+
}
82+
83+
#[inline(always)]
84+
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
85+
R::try_fill_bytes(self, dst)
86+
}
87+
}
88+
89+
#[cfg(feature = "alloc")]
90+
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
91+
impl<R: TryCryptoRng + ?Sized> TryCryptoRng for Box<R> {}

0 commit comments

Comments
 (0)