Debian Codegraph @

debian/missing-sources/rand_core/src/impls.rs

raw symbols SCIP
1 // Copyright 2018 Developers of the Rand project.
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 //! Helper functions for implementing `RngCore` functions.
10 //!
11 //! For cross-platform reproducibility, these functions all use Little Endian:
12 //! least-significant part first. For example, `next_u64_via_u32` takes `u32`
13 //! values `x, y`, then outputs `(y << 32) | x`. To implement `next_u32`
14 //! from `next_u64` in little-endian order, one should use `next_u64() as u32`.
15 //!
16 //! Byte-swapping (like the std `to_le` functions) is only needed to convert
17 //! to/from byte sequences, and since its purpose is reproducibility,
18 //! non-reproducible sources (e.g. `OsRng`) need not bother with it.
19
20 use craterust-analyzer cargo rand_core 0.9.5 crate/::RngCorerust-analyzer cargo rand_core 0.9.5 RngCore#;
21
22 /// Implement `next_u64` via `next_u32`, little-endian order.
23 pub fn next_u64_via_u32rust-analyzer cargo rand_core 0.9.5 impls/next_u64_via_u32().<R: RngCorerust-analyzer cargo rand_core 0.9.5 RngCore# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(rng: &mut R) -> u64 {
24 // Use LE; we explicitly generate one value before the next.
25 let x = u64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u64][`From<u32>`]from().(rng.next_u32rust-analyzer cargo rand_core 0.9.5 RngCore#next_u32().());
26 let y = u64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u64][`From<u32>`]from().(rng.next_u32rust-analyzer cargo rand_core 0.9.5 RngCore#next_u32().());
27 (y rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`Shl<i32>`]shl().32) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitOr<Self>`]bitor().|rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitOr<Self>`]bitor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u64][`BitOr<Self>`]bitor().x
28 }
29
30 /// Implement `fill_bytes` via `next_u64` and `next_u32`, little-endian order.
31 ///
32 /// The fastest way to fill a slice is usually to work as long as possible with
33 /// integers. That is why this method mostly uses `next_u64`, and only when
34 /// there are 4 or less bytes remaining at the end of the slice it uses
35 /// `next_u32` once.
36 pub fn fill_bytes_via_nextrust-analyzer cargo rand_core 0.9.5 impls/fill_bytes_via_next().<R: RngCorerust-analyzer cargo rand_core 0.9.5 RngCore# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(rng: &mut R, dest: &mut [u8]) {
37 let mut left = dest;
38 while left.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().8 {
39 let (l, r) = { left }.split_at_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at_mut().(8);
40 left = r;
41 let chunk: [u8; 8] = rng.next_u64rust-analyzer cargo rand_core 0.9.5 RngCore#next_u64().().to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]to_le_bytes().();
42 l.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&chunk);
43 }
44 let n = left.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().();
45 if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().4 {
46 let chunk: [u8; 8] = rng.next_u64rust-analyzer cargo rand_core 0.9.5 RngCore#next_u64().().to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]to_le_bytes().();
47 left.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#n]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().);
48 } else if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 {
49 let chunk: [u8; 4] = rng.next_u32rust-analyzer cargo rand_core 0.9.5 RngCore#next_u32().().to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_le_bytes().();
50 left.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#n]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().);
51 }
52 }
53
54 pub(craterust-analyzer cargo rand_core 0.9.5 crate/) trait Observablerust-analyzer cargo rand_core 0.9.5 impls/Observable#: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# {
55 type Bytesrust-analyzer cargo rand_core 0.9.5 impls/Observable#Bytes#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>;
56 fn to_le_bytesrust-analyzer cargo rand_core 0.9.5 impls/Observable#to_le_bytes().(self) -> Self::Bytesrust-analyzer cargo rand_core 0.9.5 impls/Observable#Bytes#;
57 }
58 impl Observablerust-analyzer cargo rand_core 0.9.5 impls/Observable# for u32 {
59 type Bytesrust-analyzer cargo rand_core 0.9.5 impls/impl#[u32][Observable]Bytes# = [u8; 4];
60
61 fn to_le_bytesrust-analyzer cargo rand_core 0.9.5 impls/impl#[u32][Observable]to_le_bytes().(self) -> Selfrust-analyzer cargo rand_core 0.9.5 impls/impl#[u32][Observable]::Bytesrust-analyzer cargo rand_core 0.9.5 impls/Observable#Bytes# {
62 Selfrust-analyzer cargo rand_core 0.9.5 impls/impl#[u32][Observable]::to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]to_le_bytes().(self)
63 }
64 }
65 impl Observablerust-analyzer cargo rand_core 0.9.5 impls/Observable# for u64 {
66 type Bytesrust-analyzer cargo rand_core 0.9.5 impls/impl#[u64][Observable]Bytes# = [u8; 8];
67
68 fn to_le_bytesrust-analyzer cargo rand_core 0.9.5 impls/impl#[u64][Observable]to_le_bytes().(self) -> Selfrust-analyzer cargo rand_core 0.9.5 impls/impl#[u64][Observable]::Bytesrust-analyzer cargo rand_core 0.9.5 impls/Observable#Bytes# {
69 Selfrust-analyzer cargo rand_core 0.9.5 impls/impl#[u64][Observable]::to_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]to_le_bytes().(self)
70 }
71 }
72
73 /// Fill dest from src
74 ///
75 /// Returns `(n, byte_len)`. `src[..n]` is consumed,
76 /// `dest[..byte_len]` is filled. `src[n..]` and `dest[byte_len..]` are left
77 /// unaltered.
78 pub(craterust-analyzer cargo rand_core 0.9.5 crate/) fn fill_via_chunksrust-analyzer cargo rand_core 0.9.5 impls/fill_via_chunks().<T: Observablerust-analyzer cargo rand_core 0.9.5 impls/Observable#>(src: &[T], dest: &mut [u8]) -> (usize, usize) {
79 let size = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>();
80
81 // Always use little endian for portability of results.
82
83 let mut dest = dest.chunks_exact_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact_mut().(size);
84 let mut src = src.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().();
85
86 let zipped = dest.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(src.by_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#by_ref().());
87 let num_chunks = zipped.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator#len().();
88 zipped.for_eachrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#for_each().(|(dest, src)| dest.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(src.to_le_bytesrust-analyzer cargo rand_core 0.9.5 impls/Observable#to_le_bytes().().as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()));
89
90 let byte_len = num_chunks rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().size;
91 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(src) = src.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]next().() {
92 // We have consumed all full chunks of dest, but not src.
93 let dest = dest.into_remainderrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`ChunksExactMut<'a, T>`]into_remainder().();
94 let n = dest.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().();
95 if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 {
96 dest.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&src.to_le_bytesrust-analyzer cargo rand_core 0.9.5 impls/Observable#to_le_bytes().().as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#n]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
97 return (num_chunks rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1, byte_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().n);
98 }
99 }
100 (num_chunks, byte_len)
101 }
102
103 /// Implement `fill_bytes` by reading chunks from the output buffer of a block
104 /// based RNG.
105 ///
106 /// The return values are `(consumed_u32, filled_u8)`.
107 ///
108 /// `src` is not modified; it is taken as a `&mut` reference for backward
109 /// compatibility with previous versions that did change it.
110 ///
111 /// `filled_u8` is the number of filled bytes in `dest`, which may be less than
112 /// the length of `dest`.
113 /// `consumed_u32` is the number of words consumed from `src`, which is the same
114 /// as `filled_u8 / 4` rounded up.
115 ///
116 /// # Example
117 /// (from `IsaacRng`)
118 ///
119 /// ```ignore
120 /// fn fill_bytes(&mut self, dest: &mut [u8]) {
121 /// let mut read_len = 0;
122 /// while read_len < dest.len() {
123 /// if self.index >= self.rsl.len() {
124 /// self.isaac();
125 /// }
126 ///
127 /// let (consumed_u32, filled_u8) =
128 /// impls::fill_via_u32_chunks(&mut self.rsl[self.index..],
129 /// &mut dest[read_len..]);
130 ///
131 /// self.index += consumed_u32;
132 /// read_len += filled_u8;
133 /// }
134 /// }
135 /// ```
136 #[deprecated(since = "0.9.3", note = "use BlockRng instead")]
137 pub fn fill_via_u32_chunksrust-analyzer cargo rand_core 0.9.5 impls/fill_via_u32_chunks().(src: &mut [u32], dest: &mut [u8]) -> (usize, usize) {
138 fill_via_chunksrust-analyzer cargo rand_core 0.9.5 impls/fill_via_chunks().(src, dest)
139 }
140
141 /// Implement `fill_bytes` by reading chunks from the output buffer of a block
142 /// based RNG.
143 ///
144 /// The return values are `(consumed_u64, filled_u8)`.
145 ///
146 /// `src` is not modified; it is taken as a `&mut` reference for backward
147 /// compatibility with previous versions that did change it.
148 ///
149 /// `filled_u8` is the number of filled bytes in `dest`, which may be less than
150 /// the length of `dest`.
151 /// `consumed_u64` is the number of words consumed from `src`, which is the same
152 /// as `filled_u8 / 8` rounded up.
153 ///
154 /// See `fill_via_u32_chunks` for an example.
155 #[deprecated(since = "0.9.3", note = "use BlockRng64 instead")]
156 pub fn fill_via_u64_chunksrust-analyzer cargo rand_core 0.9.5 impls/fill_via_u64_chunks().(src: &mut [u64], dest: &mut [u8]) -> (usize, usize) {
157 fill_via_chunksrust-analyzer cargo rand_core 0.9.5 impls/fill_via_chunks().(src, dest)
158 }
159
160 /// Implement `next_u32` via `fill_bytes`, little-endian order.
161 pub fn next_u32_via_fillrust-analyzer cargo rand_core 0.9.5 impls/next_u32_via_fill().<R: RngCorerust-analyzer cargo rand_core 0.9.5 RngCore# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(rng: &mut R) -> u32 {
162 let mut buf = [0; 4];
163 rng.fill_bytesrust-analyzer cargo rand_core 0.9.5 RngCore#fill_bytes().(&mut buf);
164 u32::from_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_le_bytes().(buf)
165 }
166
167 /// Implement `next_u64` via `fill_bytes`, little-endian order.
168 pub fn next_u64_via_fillrust-analyzer cargo rand_core 0.9.5 impls/next_u64_via_fill().<R: RngCorerust-analyzer cargo rand_core 0.9.5 RngCore# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>(rng: &mut R) -> u64 {
169 let mut buf = [0; 8];
170 rng.fill_bytesrust-analyzer cargo rand_core 0.9.5 RngCore#fill_bytes().(&mut buf);
171 u64::from_le_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]from_le_bytes().(buf)
172 }
173
174 #[cfg(test)]
175 mod test {
176 use superrust-analyzer cargo rand_core 0.9.5 crate/::*;
177
178 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
179 fn test_fill_via_u32_chunks() {
180 let src_orig = [1u32, 2, 3];
181
182 let mut src = src_orig;
183 let mut dst = [0u8; 11];
184 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fill_via_chunks(&mut src, &mut dst), (3, 11));
185 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dst, [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0]);
186
187 let mut src = src_orig;
188 let mut dst = [0u8; 13];
189 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fill_via_chunks(&mut src, &mut dst), (3, 12));
190 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dst, [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0]);
191
192 let mut src = src_orig;
193 let mut dst = [0u8; 5];
194 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fill_via_chunks(&mut src, &mut dst), (2, 5));
195 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dst, [1, 0, 0, 0, 2]);
196 }
197
198 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
199 fn test_fill_via_u64_chunks() {
200 let src_orig = [1u64, 2];
201
202 let mut src = src_orig;
203 let mut dst = [0u8; 11];
204 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fill_via_chunks(&mut src, &mut dst), (2, 11));
205 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dst, [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0]);
206
207 let mut src = src_orig;
208 let mut dst = [0u8; 17];
209 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fill_via_chunks(&mut src, &mut dst), (2, 16));
210 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dst, [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0]);
211
212 let mut src = src_orig;
213 let mut dst = [0u8; 5];
214 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fill_via_chunks(&mut src, &mut dst), (1, 5));
215 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(dst, [1, 0, 0, 0, 0]);
216 }
217 }