Skip to content

Commit a930897

Browse files
committed
Add fns BlockRng::remaining_results, from_core_and_remaining_results
1 parent 5d57a94 commit a930897

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/block.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,38 @@ impl<R: BlockRngCore> BlockRng<R> {
164164
}
165165
}
166166

167+
impl<R: BlockRngCore<Item = u32>> BlockRng<R> {
168+
/// Access the unused part of the results buffer
169+
///
170+
/// This is a low-level interface. Results are not automatically marked as
171+
/// consumed.
172+
#[inline]
173+
pub fn remaining_results(&self) -> &[u32] {
174+
&self.results.as_ref()[self.index..]
175+
}
176+
177+
/// Reconstruct from a core and a remaining-results buffer.
178+
///
179+
/// This may be used to deserialize using a `core` and the output of
180+
/// [`Self::remaining_results`].
181+
///
182+
/// Returns `None` if `remaining_results` is too long.
183+
pub fn from_core_and_remaining_results(core: R, remaining_results: &[u32]) -> Option<Self> {
184+
let mut results = R::Results::default();
185+
if remaining_results.len() <= results.as_ref().len() {
186+
let index = results.as_ref().len() - remaining_results.len();
187+
results.as_mut()[index..].copy_from_slice(remaining_results);
188+
Some(BlockRng {
189+
results,
190+
index,
191+
core,
192+
})
193+
} else {
194+
None
195+
}
196+
}
197+
}
198+
167199
impl<R: BlockRngCore<Item = u32>> RngCore for BlockRng<R> {
168200
#[inline]
169201
fn next_u32(&mut self) -> u32 {

0 commit comments

Comments
 (0)