diff options
author | garhve <git@garhve.com> | 2022-12-20 11:07:35 +0800 |
---|---|---|
committer | garhve <git@garhve.com> | 2022-12-20 11:07:35 +0800 |
commit | b5aa889f7fced8ba2cc1698ae9696d7bd0ca8ab5 (patch) | |
tree | 6fb532d8da57cf502b2a06844a743bf12789306a /rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs | |
parent | 369d11587339ce74f8ebc76f2607fe55545eaf7d (diff) |
remove compiled binary
Diffstat (limited to 'rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs')
7 files changed, 0 insertions, 2019 deletions
diff --git a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/adapter/mod.rs.html b/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/adapter/mod.rs.html deleted file mode 100644 index a272f93..0000000 --- a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/adapter/mod.rs.html +++ /dev/null @@ -1,35 +0,0 @@ -<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `/home/pico/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.5/src/rngs/adapter/mod.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>mod.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../../normalize.css"><link rel="stylesheet" href="../../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../../ayu.css" disabled><link rel="stylesheet" href="../../../../dark.css" disabled><link rel="stylesheet" href="../../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../../storage.js"></script><script defer src="../../../../source-script.js"></script><script defer src="../../../../source-files.js"></script><script defer src="../../../../main.js"></script><noscript><link rel="stylesheet" href="../../../../noscript.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"></nav><main><div class="width-limiter"><nav class="sub"><a class="sub-logo-container" href="../../../../rand/index.html"> - <img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><form class="search-form"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../../wheel.svg"></a></div></form></nav><section id="main-content" class="content"><div class="example-wrap"><pre class="src-line-numbers"><span id="1">1</span> -<span id="2">2</span> -<span id="3">3</span> -<span id="4">4</span> -<span id="5">5</span> -<span id="6">6</span> -<span id="7">7</span> -<span id="8">8</span> -<span id="9">9</span> -<span id="10">10</span> -<span id="11">11</span> -<span id="12">12</span> -<span id="13">13</span> -<span id="14">14</span> -<span id="15">15</span> -<span id="16">16</span> -</pre><pre class="rust"><code><span class="comment">// Copyright 2018 Developers of the Rand project. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -</span><span class="doccomment">//! Wrappers / adapters forming RNGs - -</span><span class="kw">mod </span>read; -<span class="kw">mod </span>reseeding; - -<span class="attribute">#[allow(deprecated)] -</span><span class="kw">pub use </span><span class="self">self</span>::read::{ReadError, ReadRng}; -<span class="kw">pub use </span><span class="self">self</span>::reseeding::ReseedingRng; -</code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../../" data-current-crate="rand" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1)" ></div></body></html>
\ No newline at end of file diff --git a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/adapter/read.rs.html b/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/adapter/read.rs.html deleted file mode 100644 index d16c0fb..0000000 --- a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/adapter/read.rs.html +++ /dev/null @@ -1,303 +0,0 @@ -<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `/home/pico/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.5/src/rngs/adapter/read.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>read.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../../normalize.css"><link rel="stylesheet" href="../../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../../ayu.css" disabled><link rel="stylesheet" href="../../../../dark.css" disabled><link rel="stylesheet" href="../../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../../storage.js"></script><script defer src="../../../../source-script.js"></script><script defer src="../../../../source-files.js"></script><script defer src="../../../../main.js"></script><noscript><link rel="stylesheet" href="../../../../noscript.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"></nav><main><div class="width-limiter"><nav class="sub"><a class="sub-logo-container" href="../../../../rand/index.html"> - <img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><form class="search-form"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../../wheel.svg"></a></div></form></nav><section id="main-content" class="content"><div class="example-wrap"><pre class="src-line-numbers"><span id="1">1</span> -<span id="2">2</span> -<span id="3">3</span> -<span id="4">4</span> -<span id="5">5</span> -<span id="6">6</span> -<span id="7">7</span> -<span id="8">8</span> -<span id="9">9</span> -<span id="10">10</span> -<span id="11">11</span> -<span id="12">12</span> -<span id="13">13</span> -<span id="14">14</span> -<span id="15">15</span> -<span id="16">16</span> -<span id="17">17</span> -<span id="18">18</span> -<span id="19">19</span> -<span id="20">20</span> -<span id="21">21</span> -<span id="22">22</span> -<span id="23">23</span> -<span id="24">24</span> -<span id="25">25</span> -<span id="26">26</span> -<span id="27">27</span> -<span id="28">28</span> -<span id="29">29</span> -<span id="30">30</span> -<span id="31">31</span> -<span id="32">32</span> -<span id="33">33</span> -<span id="34">34</span> -<span id="35">35</span> -<span id="36">36</span> -<span id="37">37</span> -<span id="38">38</span> -<span id="39">39</span> -<span id="40">40</span> -<span id="41">41</span> -<span id="42">42</span> -<span id="43">43</span> -<span id="44">44</span> -<span id="45">45</span> -<span id="46">46</span> -<span id="47">47</span> -<span id="48">48</span> -<span id="49">49</span> -<span id="50">50</span> -<span id="51">51</span> -<span id="52">52</span> -<span id="53">53</span> -<span id="54">54</span> -<span id="55">55</span> -<span id="56">56</span> -<span id="57">57</span> -<span id="58">58</span> -<span id="59">59</span> -<span id="60">60</span> -<span id="61">61</span> -<span id="62">62</span> -<span id="63">63</span> -<span id="64">64</span> -<span id="65">65</span> -<span id="66">66</span> -<span id="67">67</span> -<span id="68">68</span> -<span id="69">69</span> -<span id="70">70</span> -<span id="71">71</span> -<span id="72">72</span> -<span id="73">73</span> -<span id="74">74</span> -<span id="75">75</span> -<span id="76">76</span> -<span id="77">77</span> -<span id="78">78</span> -<span id="79">79</span> -<span id="80">80</span> -<span id="81">81</span> -<span id="82">82</span> -<span id="83">83</span> -<span id="84">84</span> -<span id="85">85</span> -<span id="86">86</span> -<span id="87">87</span> -<span id="88">88</span> -<span id="89">89</span> -<span id="90">90</span> -<span id="91">91</span> -<span id="92">92</span> -<span id="93">93</span> -<span id="94">94</span> -<span id="95">95</span> -<span id="96">96</span> -<span id="97">97</span> -<span id="98">98</span> -<span id="99">99</span> -<span id="100">100</span> -<span id="101">101</span> -<span id="102">102</span> -<span id="103">103</span> -<span id="104">104</span> -<span id="105">105</span> -<span id="106">106</span> -<span id="107">107</span> -<span id="108">108</span> -<span id="109">109</span> -<span id="110">110</span> -<span id="111">111</span> -<span id="112">112</span> -<span id="113">113</span> -<span id="114">114</span> -<span id="115">115</span> -<span id="116">116</span> -<span id="117">117</span> -<span id="118">118</span> -<span id="119">119</span> -<span id="120">120</span> -<span id="121">121</span> -<span id="122">122</span> -<span id="123">123</span> -<span id="124">124</span> -<span id="125">125</span> -<span id="126">126</span> -<span id="127">127</span> -<span id="128">128</span> -<span id="129">129</span> -<span id="130">130</span> -<span id="131">131</span> -<span id="132">132</span> -<span id="133">133</span> -<span id="134">134</span> -<span id="135">135</span> -<span id="136">136</span> -<span id="137">137</span> -<span id="138">138</span> -<span id="139">139</span> -<span id="140">140</span> -<span id="141">141</span> -<span id="142">142</span> -<span id="143">143</span> -<span id="144">144</span> -<span id="145">145</span> -<span id="146">146</span> -<span id="147">147</span> -<span id="148">148</span> -<span id="149">149</span> -<span id="150">150</span> -</pre><pre class="rust"><code><span class="comment">// Copyright 2018 Developers of the Rand project. -// Copyright 2013 The Rust Project Developers. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -</span><span class="doccomment">//! A wrapper around any Read to treat it as an RNG. - -</span><span class="attribute">#![allow(deprecated)] - -</span><span class="kw">use </span>std::fmt; -<span class="kw">use </span>std::io::Read; - -<span class="kw">use </span>rand_core::{impls, Error, RngCore}; - - -<span class="doccomment">/// An RNG that reads random bytes straight from any type supporting -/// [`std::io::Read`], for example files. -/// -/// This will work best with an infinite reader, but that is not required. -/// -/// This can be used with `/dev/urandom` on Unix but it is recommended to use -/// [`OsRng`] instead. -/// -/// # Panics -/// -/// `ReadRng` uses [`std::io::Read::read_exact`], which retries on interrupts. -/// All other errors from the underlying reader, including when it does not -/// have enough data, will only be reported through [`try_fill_bytes`]. -/// The other [`RngCore`] methods will panic in case of an error. -/// -/// [`OsRng`]: crate::rngs::OsRng -/// [`try_fill_bytes`]: RngCore::try_fill_bytes -</span><span class="attribute">#[derive(Debug)] -#[deprecated(since=<span class="string">"0.8.4"</span>, note=<span class="string">"removal due to lack of usage"</span>)] -</span><span class="kw">pub struct </span>ReadRng<R> { - reader: R, -} - -<span class="kw">impl</span><R: Read> ReadRng<R> { - <span class="doccomment">/// Create a new `ReadRng` from a `Read`. - </span><span class="kw">pub fn </span>new(r: R) -> ReadRng<R> { - ReadRng { reader: r } - } -} - -<span class="kw">impl</span><R: Read> RngCore <span class="kw">for </span>ReadRng<R> { - <span class="kw">fn </span>next_u32(<span class="kw-2">&mut </span><span class="self">self</span>) -> u32 { - impls::next_u32_via_fill(<span class="self">self</span>) - } - - <span class="kw">fn </span>next_u64(<span class="kw-2">&mut </span><span class="self">self</span>) -> u64 { - impls::next_u64_via_fill(<span class="self">self</span>) - } - - <span class="kw">fn </span>fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) { - <span class="self">self</span>.try_fill_bytes(dest).unwrap_or_else(|err| { - <span class="macro">panic!</span>( - <span class="string">"reading random bytes from Read implementation failed; error: {}"</span>, - err - ) - }); - } - - <span class="kw">fn </span>try_fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) -> <span class="prelude-ty">Result</span><(), Error> { - <span class="kw">if </span>dest.is_empty() { - <span class="kw">return </span><span class="prelude-val">Ok</span>(()); - } - <span class="comment">// Use `std::io::read_exact`, which retries on `ErrorKind::Interrupted`. - </span><span class="self">self</span>.reader - .read_exact(dest) - .map_err(|e| Error::new(ReadError(e))) - } -} - -<span class="doccomment">/// `ReadRng` error type -</span><span class="attribute">#[derive(Debug)] -#[deprecated(since=<span class="string">"0.8.4"</span>)] -</span><span class="kw">pub struct </span>ReadError(std::io::Error); - -<span class="kw">impl </span>fmt::Display <span class="kw">for </span>ReadError { - <span class="kw">fn </span>fmt(<span class="kw-2">&</span><span class="self">self</span>, f: <span class="kw-2">&mut </span>fmt::Formatter) -> fmt::Result { - <span class="macro">write!</span>(f, <span class="string">"ReadError: {}"</span>, <span class="self">self</span>.<span class="number">0</span>) - } -} - -<span class="kw">impl </span>std::error::Error <span class="kw">for </span>ReadError { - <span class="kw">fn </span>source(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="prelude-ty">Option</span><<span class="kw-2">&</span>(<span class="kw">dyn </span>std::error::Error + <span class="lifetime">'static</span>)> { - <span class="prelude-val">Some</span>(<span class="kw-2">&</span><span class="self">self</span>.<span class="number">0</span>) - } -} - - -<span class="attribute">#[cfg(test)] -</span><span class="kw">mod </span>test { - <span class="kw">use </span>std::println; - - <span class="kw">use </span><span class="kw">super</span>::ReadRng; - <span class="kw">use </span><span class="kw">crate</span>::RngCore; - - <span class="attribute">#[test] - </span><span class="kw">fn </span>test_reader_rng_u64() { - <span class="comment">// transmute from the target to avoid endianness concerns. - </span><span class="attribute">#[rustfmt::skip] - </span><span class="kw">let </span>v = [<span class="number">0u8</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, - <span class="number">0</span>, <span class="number">4</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">2</span>, - <span class="number">5</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]; - <span class="kw">let </span><span class="kw-2">mut </span>rng = ReadRng::new(<span class="kw-2">&</span>v[..]); - - <span class="macro">assert_eq!</span>(rng.next_u64(), <span class="number">1 </span><< <span class="number">56</span>); - <span class="macro">assert_eq!</span>(rng.next_u64(), (<span class="number">2 </span><< <span class="number">56</span>) + (<span class="number">3 </span><< <span class="number">32</span>) + (<span class="number">4 </span><< <span class="number">8</span>)); - <span class="macro">assert_eq!</span>(rng.next_u64(), <span class="number">5</span>); - } - - <span class="attribute">#[test] - </span><span class="kw">fn </span>test_reader_rng_u32() { - <span class="kw">let </span>v = [<span class="number">0u8</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">2</span>, <span class="number">0</span>, <span class="number">3</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>]; - <span class="kw">let </span><span class="kw-2">mut </span>rng = ReadRng::new(<span class="kw-2">&</span>v[..]); - - <span class="macro">assert_eq!</span>(rng.next_u32(), <span class="number">1 </span><< <span class="number">24</span>); - <span class="macro">assert_eq!</span>(rng.next_u32(), <span class="number">2 </span><< <span class="number">16</span>); - <span class="macro">assert_eq!</span>(rng.next_u32(), <span class="number">3</span>); - } - - <span class="attribute">#[test] - </span><span class="kw">fn </span>test_reader_rng_fill_bytes() { - <span class="kw">let </span>v = [<span class="number">1u8</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]; - <span class="kw">let </span><span class="kw-2">mut </span>w = [<span class="number">0u8</span>; <span class="number">8</span>]; - - <span class="kw">let </span><span class="kw-2">mut </span>rng = ReadRng::new(<span class="kw-2">&</span>v[..]); - rng.fill_bytes(<span class="kw-2">&mut </span>w); - - <span class="macro">assert!</span>(v == w); - } - - <span class="attribute">#[test] - </span><span class="kw">fn </span>test_reader_rng_insufficient_bytes() { - <span class="kw">let </span>v = [<span class="number">1u8</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]; - <span class="kw">let </span><span class="kw-2">mut </span>w = [<span class="number">0u8</span>; <span class="number">9</span>]; - - <span class="kw">let </span><span class="kw-2">mut </span>rng = ReadRng::new(<span class="kw-2">&</span>v[..]); - - <span class="kw">let </span>result = rng.try_fill_bytes(<span class="kw-2">&mut </span>w); - <span class="macro">assert!</span>(result.is_err()); - <span class="macro">println!</span>(<span class="string">"Error: {}"</span>, result.unwrap_err()); - } -} -</code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../../" data-current-crate="rand" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1)" ></div></body></html>
\ No newline at end of file diff --git a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/adapter/reseeding.rs.html b/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/adapter/reseeding.rs.html deleted file mode 100644 index 93f4eaf..0000000 --- a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/adapter/reseeding.rs.html +++ /dev/null @@ -1,775 +0,0 @@ -<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `/home/pico/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.5/src/rngs/adapter/reseeding.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>reseeding.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../../normalize.css"><link rel="stylesheet" href="../../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../../ayu.css" disabled><link rel="stylesheet" href="../../../../dark.css" disabled><link rel="stylesheet" href="../../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../../storage.js"></script><script defer src="../../../../source-script.js"></script><script defer src="../../../../source-files.js"></script><script defer src="../../../../main.js"></script><noscript><link rel="stylesheet" href="../../../../noscript.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"></nav><main><div class="width-limiter"><nav class="sub"><a class="sub-logo-container" href="../../../../rand/index.html"> - <img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><form class="search-form"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../../wheel.svg"></a></div></form></nav><section id="main-content" class="content"><div class="example-wrap"><pre class="src-line-numbers"><span id="1">1</span> -<span id="2">2</span> -<span id="3">3</span> -<span id="4">4</span> -<span id="5">5</span> -<span id="6">6</span> -<span id="7">7</span> -<span id="8">8</span> -<span id="9">9</span> -<span id="10">10</span> -<span id="11">11</span> -<span id="12">12</span> -<span id="13">13</span> -<span id="14">14</span> -<span id="15">15</span> -<span id="16">16</span> -<span id="17">17</span> -<span id="18">18</span> -<span id="19">19</span> -<span id="20">20</span> -<span id="21">21</span> -<span id="22">22</span> -<span id="23">23</span> -<span id="24">24</span> -<span id="25">25</span> -<span id="26">26</span> -<span id="27">27</span> -<span id="28">28</span> -<span id="29">29</span> -<span id="30">30</span> -<span id="31">31</span> -<span id="32">32</span> -<span id="33">33</span> -<span id="34">34</span> -<span id="35">35</span> -<span id="36">36</span> -<span id="37">37</span> -<span id="38">38</span> -<span id="39">39</span> -<span id="40">40</span> -<span id="41">41</span> -<span id="42">42</span> -<span id="43">43</span> -<span id="44">44</span> -<span id="45">45</span> -<span id="46">46</span> -<span id="47">47</span> -<span id="48">48</span> -<span id="49">49</span> -<span id="50">50</span> -<span id="51">51</span> -<span id="52">52</span> -<span id="53">53</span> -<span id="54">54</span> -<span id="55">55</span> -<span id="56">56</span> -<span id="57">57</span> -<span id="58">58</span> -<span id="59">59</span> -<span id="60">60</span> -<span id="61">61</span> -<span id="62">62</span> -<span id="63">63</span> -<span id="64">64</span> -<span id="65">65</span> -<span id="66">66</span> -<span id="67">67</span> -<span id="68">68</span> -<span id="69">69</span> -<span id="70">70</span> -<span id="71">71</span> -<span id="72">72</span> -<span id="73">73</span> -<span id="74">74</span> -<span id="75">75</span> -<span id="76">76</span> -<span id="77">77</span> -<span id="78">78</span> -<span id="79">79</span> -<span id="80">80</span> -<span id="81">81</span> -<span id="82">82</span> -<span id="83">83</span> -<span id="84">84</span> -<span id="85">85</span> -<span id="86">86</span> -<span id="87">87</span> -<span id="88">88</span> -<span id="89">89</span> -<span id="90">90</span> -<span id="91">91</span> -<span id="92">92</span> -<span id="93">93</span> -<span id="94">94</span> -<span id="95">95</span> -<span id="96">96</span> -<span id="97">97</span> -<span id="98">98</span> -<span id="99">99</span> -<span id="100">100</span> -<span id="101">101</span> -<span id="102">102</span> -<span id="103">103</span> -<span id="104">104</span> -<span id="105">105</span> -<span id="106">106</span> -<span id="107">107</span> -<span id="108">108</span> -<span id="109">109</span> -<span id="110">110</span> -<span id="111">111</span> -<span id="112">112</span> -<span id="113">113</span> -<span id="114">114</span> -<span id="115">115</span> -<span id="116">116</span> -<span id="117">117</span> -<span id="118">118</span> -<span id="119">119</span> -<span id="120">120</span> -<span id="121">121</span> -<span id="122">122</span> -<span id="123">123</span> -<span id="124">124</span> -<span id="125">125</span> -<span id="126">126</span> -<span id="127">127</span> -<span id="128">128</span> -<span id="129">129</span> -<span id="130">130</span> -<span id="131">131</span> -<span id="132">132</span> -<span id="133">133</span> -<span id="134">134</span> -<span id="135">135</span> -<span id="136">136</span> -<span id="137">137</span> -<span id="138">138</span> -<span id="139">139</span> -<span id="140">140</span> -<span id="141">141</span> -<span id="142">142</span> -<span id="143">143</span> -<span id="144">144</span> -<span id="145">145</span> -<span id="146">146</span> -<span id="147">147</span> -<span id="148">148</span> -<span id="149">149</span> -<span id="150">150</span> -<span id="151">151</span> -<span id="152">152</span> -<span id="153">153</span> -<span id="154">154</span> -<span id="155">155</span> -<span id="156">156</span> -<span id="157">157</span> -<span id="158">158</span> -<span id="159">159</span> -<span id="160">160</span> -<span id="161">161</span> -<span id="162">162</span> -<span id="163">163</span> -<span id="164">164</span> -<span id="165">165</span> -<span id="166">166</span> -<span id="167">167</span> -<span id="168">168</span> -<span id="169">169</span> -<span id="170">170</span> -<span id="171">171</span> -<span id="172">172</span> -<span id="173">173</span> -<span id="174">174</span> -<span id="175">175</span> -<span id="176">176</span> -<span id="177">177</span> -<span id="178">178</span> -<span id="179">179</span> -<span id="180">180</span> -<span id="181">181</span> -<span id="182">182</span> -<span id="183">183</span> -<span id="184">184</span> -<span id="185">185</span> -<span id="186">186</span> -<span id="187">187</span> -<span id="188">188</span> -<span id="189">189</span> -<span id="190">190</span> -<span id="191">191</span> -<span id="192">192</span> -<span id="193">193</span> -<span id="194">194</span> -<span id="195">195</span> -<span id="196">196</span> -<span id="197">197</span> -<span id="198">198</span> -<span id="199">199</span> -<span id="200">200</span> -<span id="201">201</span> -<span id="202">202</span> -<span id="203">203</span> -<span id="204">204</span> -<span id="205">205</span> -<span id="206">206</span> -<span id="207">207</span> -<span id="208">208</span> -<span id="209">209</span> -<span id="210">210</span> -<span id="211">211</span> -<span id="212">212</span> -<span id="213">213</span> -<span id="214">214</span> -<span id="215">215</span> -<span id="216">216</span> -<span id="217">217</span> -<span id="218">218</span> -<span id="219">219</span> -<span id="220">220</span> -<span id="221">221</span> -<span id="222">222</span> -<span id="223">223</span> -<span id="224">224</span> -<span id="225">225</span> -<span id="226">226</span> -<span id="227">227</span> -<span id="228">228</span> -<span id="229">229</span> -<span id="230">230</span> -<span id="231">231</span> -<span id="232">232</span> -<span id="233">233</span> -<span id="234">234</span> -<span id="235">235</span> -<span id="236">236</span> -<span id="237">237</span> -<span id="238">238</span> -<span id="239">239</span> -<span id="240">240</span> -<span id="241">241</span> -<span id="242">242</span> -<span id="243">243</span> -<span id="244">244</span> -<span id="245">245</span> -<span id="246">246</span> -<span id="247">247</span> -<span id="248">248</span> -<span id="249">249</span> -<span id="250">250</span> -<span id="251">251</span> -<span id="252">252</span> -<span id="253">253</span> -<span id="254">254</span> -<span id="255">255</span> -<span id="256">256</span> -<span id="257">257</span> -<span id="258">258</span> -<span id="259">259</span> -<span id="260">260</span> -<span id="261">261</span> -<span id="262">262</span> -<span id="263">263</span> -<span id="264">264</span> -<span id="265">265</span> -<span id="266">266</span> -<span id="267">267</span> -<span id="268">268</span> -<span id="269">269</span> -<span id="270">270</span> -<span id="271">271</span> -<span id="272">272</span> -<span id="273">273</span> -<span id="274">274</span> -<span id="275">275</span> -<span id="276">276</span> -<span id="277">277</span> -<span id="278">278</span> -<span id="279">279</span> -<span id="280">280</span> -<span id="281">281</span> -<span id="282">282</span> -<span id="283">283</span> -<span id="284">284</span> -<span id="285">285</span> -<span id="286">286</span> -<span id="287">287</span> -<span id="288">288</span> -<span id="289">289</span> -<span id="290">290</span> -<span id="291">291</span> -<span id="292">292</span> -<span id="293">293</span> -<span id="294">294</span> -<span id="295">295</span> -<span id="296">296</span> -<span id="297">297</span> -<span id="298">298</span> -<span id="299">299</span> -<span id="300">300</span> -<span id="301">301</span> -<span id="302">302</span> -<span id="303">303</span> -<span id="304">304</span> -<span id="305">305</span> -<span id="306">306</span> -<span id="307">307</span> -<span id="308">308</span> -<span id="309">309</span> -<span id="310">310</span> -<span id="311">311</span> -<span id="312">312</span> -<span id="313">313</span> -<span id="314">314</span> -<span id="315">315</span> -<span id="316">316</span> -<span id="317">317</span> -<span id="318">318</span> -<span id="319">319</span> -<span id="320">320</span> -<span id="321">321</span> -<span id="322">322</span> -<span id="323">323</span> -<span id="324">324</span> -<span id="325">325</span> -<span id="326">326</span> -<span id="327">327</span> -<span id="328">328</span> -<span id="329">329</span> -<span id="330">330</span> -<span id="331">331</span> -<span id="332">332</span> -<span id="333">333</span> -<span id="334">334</span> -<span id="335">335</span> -<span id="336">336</span> -<span id="337">337</span> -<span id="338">338</span> -<span id="339">339</span> -<span id="340">340</span> -<span id="341">341</span> -<span id="342">342</span> -<span id="343">343</span> -<span id="344">344</span> -<span id="345">345</span> -<span id="346">346</span> -<span id="347">347</span> -<span id="348">348</span> -<span id="349">349</span> -<span id="350">350</span> -<span id="351">351</span> -<span id="352">352</span> -<span id="353">353</span> -<span id="354">354</span> -<span id="355">355</span> -<span id="356">356</span> -<span id="357">357</span> -<span id="358">358</span> -<span id="359">359</span> -<span id="360">360</span> -<span id="361">361</span> -<span id="362">362</span> -<span id="363">363</span> -<span id="364">364</span> -<span id="365">365</span> -<span id="366">366</span> -<span id="367">367</span> -<span id="368">368</span> -<span id="369">369</span> -<span id="370">370</span> -<span id="371">371</span> -<span id="372">372</span> -<span id="373">373</span> -<span id="374">374</span> -<span id="375">375</span> -<span id="376">376</span> -<span id="377">377</span> -<span id="378">378</span> -<span id="379">379</span> -<span id="380">380</span> -<span id="381">381</span> -<span id="382">382</span> -<span id="383">383</span> -<span id="384">384</span> -<span id="385">385</span> -<span id="386">386</span> -</pre><pre class="rust"><code><span class="comment">// Copyright 2018 Developers of the Rand project. -// Copyright 2013 The Rust Project Developers. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -</span><span class="doccomment">//! A wrapper around another PRNG that reseeds it after it -//! generates a certain number of random bytes. - -</span><span class="kw">use </span>core::mem::size_of; - -<span class="kw">use </span>rand_core::block::{BlockRng, BlockRngCore}; -<span class="kw">use </span>rand_core::{CryptoRng, Error, RngCore, SeedableRng}; - -<span class="doccomment">/// A wrapper around any PRNG that implements [`BlockRngCore`], that adds the -/// ability to reseed it. -/// -/// `ReseedingRng` reseeds the underlying PRNG in the following cases: -/// -/// - On a manual call to [`reseed()`]. -/// - After `clone()`, the clone will be reseeded on first use. -/// - When a process is forked on UNIX, the RNGs in both the parent and child -/// processes will be reseeded just before the next call to -/// [`BlockRngCore::generate`], i.e. "soon". For ChaCha and Hc128 this is a -/// maximum of fifteen `u32` values before reseeding. -/// - After the PRNG has generated a configurable number of random bytes. -/// -/// # When should reseeding after a fixed number of generated bytes be used? -/// -/// Reseeding after a fixed number of generated bytes is never strictly -/// *necessary*. Cryptographic PRNGs don't have a limited number of bytes they -/// can output, or at least not a limit reachable in any practical way. There is -/// no such thing as 'running out of entropy'. -/// -/// Occasionally reseeding can be seen as some form of 'security in depth'. Even -/// if in the future a cryptographic weakness is found in the CSPRNG being used, -/// or a flaw in the implementation, occasionally reseeding should make -/// exploiting it much more difficult or even impossible. -/// -/// Use [`ReseedingRng::new`] with a `threshold` of `0` to disable reseeding -/// after a fixed number of generated bytes. -/// -/// # Limitations -/// -/// It is recommended that a `ReseedingRng` (including `ThreadRng`) not be used -/// from a fork handler. -/// Use `OsRng` or `getrandom`, or defer your use of the RNG until later. -/// -/// # Error handling -/// -/// Although unlikely, reseeding the wrapped PRNG can fail. `ReseedingRng` will -/// never panic but try to handle the error intelligently through some -/// combination of retrying and delaying reseeding until later. -/// If handling the source error fails `ReseedingRng` will continue generating -/// data from the wrapped PRNG without reseeding. -/// -/// Manually calling [`reseed()`] will not have this retry or delay logic, but -/// reports the error. -/// -/// # Example -/// -/// ``` -/// use rand::prelude::*; -/// use rand_chacha::ChaCha20Core; // Internal part of ChaChaRng that -/// // implements BlockRngCore -/// use rand::rngs::OsRng; -/// use rand::rngs::adapter::ReseedingRng; -/// -/// let prng = ChaCha20Core::from_entropy(); -/// let mut reseeding_rng = ReseedingRng::new(prng, 0, OsRng); -/// -/// println!("{}", reseeding_rng.gen::<u64>()); -/// -/// let mut cloned_rng = reseeding_rng.clone(); -/// assert!(reseeding_rng.gen::<u64>() != cloned_rng.gen::<u64>()); -/// ``` -/// -/// [`BlockRngCore`]: rand_core::block::BlockRngCore -/// [`ReseedingRng::new`]: ReseedingRng::new -/// [`reseed()`]: ReseedingRng::reseed -</span><span class="attribute">#[derive(Debug)] -</span><span class="kw">pub struct </span>ReseedingRng<R, Rsdr>(BlockRng<ReseedingCore<R, Rsdr>>) -<span class="kw">where - </span>R: BlockRngCore + SeedableRng, - Rsdr: RngCore; - -<span class="kw">impl</span><R, Rsdr> ReseedingRng<R, Rsdr> -<span class="kw">where - </span>R: BlockRngCore + SeedableRng, - Rsdr: RngCore, -{ - <span class="doccomment">/// Create a new `ReseedingRng` from an existing PRNG, combined with a RNG - /// to use as reseeder. - /// - /// `threshold` sets the number of generated bytes after which to reseed the - /// PRNG. Set it to zero to never reseed based on the number of generated - /// values. - </span><span class="kw">pub fn </span>new(rng: R, threshold: u64, reseeder: Rsdr) -> <span class="self">Self </span>{ - ReseedingRng(BlockRng::new(ReseedingCore::new(rng, threshold, reseeder))) - } - - <span class="doccomment">/// Reseed the internal PRNG. - </span><span class="kw">pub fn </span>reseed(<span class="kw-2">&mut </span><span class="self">self</span>) -> <span class="prelude-ty">Result</span><(), Error> { - <span class="self">self</span>.<span class="number">0</span>.core.reseed() - } -} - -<span class="comment">// TODO: this should be implemented for any type where the inner type -// implements RngCore, but we can't specify that because ReseedingCore is private -</span><span class="kw">impl</span><R, Rsdr: RngCore> RngCore <span class="kw">for </span>ReseedingRng<R, Rsdr> -<span class="kw">where - </span>R: BlockRngCore<Item = u32> + SeedableRng, - <R <span class="kw">as </span>BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>, -{ - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>next_u32(<span class="kw-2">&mut </span><span class="self">self</span>) -> u32 { - <span class="self">self</span>.<span class="number">0</span>.next_u32() - } - - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>next_u64(<span class="kw-2">&mut </span><span class="self">self</span>) -> u64 { - <span class="self">self</span>.<span class="number">0</span>.next_u64() - } - - <span class="kw">fn </span>fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) { - <span class="self">self</span>.<span class="number">0</span>.fill_bytes(dest) - } - - <span class="kw">fn </span>try_fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) -> <span class="prelude-ty">Result</span><(), Error> { - <span class="self">self</span>.<span class="number">0</span>.try_fill_bytes(dest) - } -} - -<span class="kw">impl</span><R, Rsdr> Clone <span class="kw">for </span>ReseedingRng<R, Rsdr> -<span class="kw">where - </span>R: BlockRngCore + SeedableRng + Clone, - Rsdr: RngCore + Clone, -{ - <span class="kw">fn </span>clone(<span class="kw-2">&</span><span class="self">self</span>) -> ReseedingRng<R, Rsdr> { - <span class="comment">// Recreating `BlockRng` seems easier than cloning it and resetting - // the index. - </span>ReseedingRng(BlockRng::new(<span class="self">self</span>.<span class="number">0</span>.core.clone())) - } -} - -<span class="kw">impl</span><R, Rsdr> CryptoRng <span class="kw">for </span>ReseedingRng<R, Rsdr> -<span class="kw">where - </span>R: BlockRngCore + SeedableRng + CryptoRng, - Rsdr: RngCore + CryptoRng, -{ -} - -<span class="attribute">#[derive(Debug)] -</span><span class="kw">struct </span>ReseedingCore<R, Rsdr> { - inner: R, - reseeder: Rsdr, - threshold: i64, - bytes_until_reseed: i64, - fork_counter: usize, -} - -<span class="kw">impl</span><R, Rsdr> BlockRngCore <span class="kw">for </span>ReseedingCore<R, Rsdr> -<span class="kw">where - </span>R: BlockRngCore + SeedableRng, - Rsdr: RngCore, -{ - <span class="kw">type </span>Item = <R <span class="kw">as </span>BlockRngCore>::Item; - <span class="kw">type </span>Results = <R <span class="kw">as </span>BlockRngCore>::Results; - - <span class="kw">fn </span>generate(<span class="kw-2">&mut </span><span class="self">self</span>, results: <span class="kw-2">&mut </span><span class="self">Self</span>::Results) { - <span class="kw">let </span>global_fork_counter = fork::get_fork_counter(); - <span class="kw">if </span><span class="self">self</span>.bytes_until_reseed <= <span class="number">0 </span>|| <span class="self">self</span>.is_forked(global_fork_counter) { - <span class="comment">// We get better performance by not calling only `reseed` here - // and continuing with the rest of the function, but by directly - // returning from a non-inlined function. - </span><span class="kw">return </span><span class="self">self</span>.reseed_and_generate(results, global_fork_counter); - } - <span class="kw">let </span>num_bytes = results.as_ref().len() * size_of::<<span class="self">Self</span>::Item>(); - <span class="self">self</span>.bytes_until_reseed -= num_bytes <span class="kw">as </span>i64; - <span class="self">self</span>.inner.generate(results); - } -} - -<span class="kw">impl</span><R, Rsdr> ReseedingCore<R, Rsdr> -<span class="kw">where - </span>R: BlockRngCore + SeedableRng, - Rsdr: RngCore, -{ - <span class="doccomment">/// Create a new `ReseedingCore`. - </span><span class="kw">fn </span>new(rng: R, threshold: u64, reseeder: Rsdr) -> <span class="self">Self </span>{ - <span class="kw">use </span>::core::i64::MAX; - fork::register_fork_handler(); - - <span class="comment">// Because generating more values than `i64::MAX` takes centuries on - // current hardware, we just clamp to that value. - // Also we set a threshold of 0, which indicates no limit, to that - // value. - </span><span class="kw">let </span>threshold = <span class="kw">if </span>threshold == <span class="number">0 </span>{ - MAX - } <span class="kw">else if </span>threshold <= MAX <span class="kw">as </span>u64 { - threshold <span class="kw">as </span>i64 - } <span class="kw">else </span>{ - MAX - }; - - ReseedingCore { - inner: rng, - reseeder, - threshold: threshold <span class="kw">as </span>i64, - bytes_until_reseed: threshold <span class="kw">as </span>i64, - fork_counter: <span class="number">0</span>, - } - } - - <span class="doccomment">/// Reseed the internal PRNG. - </span><span class="kw">fn </span>reseed(<span class="kw-2">&mut </span><span class="self">self</span>) -> <span class="prelude-ty">Result</span><(), Error> { - R::from_rng(<span class="kw-2">&mut </span><span class="self">self</span>.reseeder).map(|result| { - <span class="self">self</span>.bytes_until_reseed = <span class="self">self</span>.threshold; - <span class="self">self</span>.inner = result - }) - } - - <span class="kw">fn </span>is_forked(<span class="kw-2">&</span><span class="self">self</span>, global_fork_counter: usize) -> bool { - <span class="comment">// In theory, on 32-bit platforms, it is possible for - // `global_fork_counter` to wrap around after ~4e9 forks. - // - // This check will detect a fork in the normal case where - // `fork_counter < global_fork_counter`, and also when the difference - // between both is greater than `isize::MAX` (wrapped around). - // - // It will still fail to detect a fork if there have been more than - // `isize::MAX` forks, without any reseed in between. Seems unlikely - // enough. - </span>(<span class="self">self</span>.fork_counter.wrapping_sub(global_fork_counter) <span class="kw">as </span>isize) < <span class="number">0 - </span>} - - <span class="attribute">#[inline(never)] - </span><span class="kw">fn </span>reseed_and_generate( - <span class="kw-2">&mut </span><span class="self">self</span>, results: <span class="kw-2">&mut </span><<span class="self">Self </span><span class="kw">as </span>BlockRngCore>::Results, global_fork_counter: usize, - ) { - <span class="attribute">#![allow(clippy::if_same_then_else)] </span><span class="comment">// false positive - </span><span class="kw">if </span><span class="self">self</span>.is_forked(global_fork_counter) { - <span class="macro">info!</span>(<span class="string">"Fork detected, reseeding RNG"</span>); - } <span class="kw">else </span>{ - <span class="macro">trace!</span>(<span class="string">"Reseeding RNG (periodic reseed)"</span>); - } - - <span class="kw">let </span>num_bytes = results.as_ref().len() * size_of::<<R <span class="kw">as </span>BlockRngCore>::Item>(); - - <span class="kw">if let </span><span class="prelude-val">Err</span>(e) = <span class="self">self</span>.reseed() { - <span class="macro">warn!</span>(<span class="string">"Reseeding RNG failed: {}"</span>, e); - <span class="kw">let _ </span>= e; - } - <span class="self">self</span>.fork_counter = global_fork_counter; - - <span class="self">self</span>.bytes_until_reseed = <span class="self">self</span>.threshold - num_bytes <span class="kw">as </span>i64; - <span class="self">self</span>.inner.generate(results); - } -} - -<span class="kw">impl</span><R, Rsdr> Clone <span class="kw">for </span>ReseedingCore<R, Rsdr> -<span class="kw">where - </span>R: BlockRngCore + SeedableRng + Clone, - Rsdr: RngCore + Clone, -{ - <span class="kw">fn </span>clone(<span class="kw-2">&</span><span class="self">self</span>) -> ReseedingCore<R, Rsdr> { - ReseedingCore { - inner: <span class="self">self</span>.inner.clone(), - reseeder: <span class="self">self</span>.reseeder.clone(), - threshold: <span class="self">self</span>.threshold, - bytes_until_reseed: <span class="number">0</span>, <span class="comment">// reseed clone on first use - </span>fork_counter: <span class="self">self</span>.fork_counter, - } - } -} - -<span class="kw">impl</span><R, Rsdr> CryptoRng <span class="kw">for </span>ReseedingCore<R, Rsdr> -<span class="kw">where - </span>R: BlockRngCore + SeedableRng + CryptoRng, - Rsdr: RngCore + CryptoRng, -{ -} - - -<span class="attribute">#[cfg(all(unix, not(target_os = <span class="string">"emscripten"</span>)))] -</span><span class="kw">mod </span>fork { - <span class="kw">use </span>core::sync::atomic::{AtomicUsize, Ordering}; - <span class="kw">use </span>std::sync::Once; - - <span class="comment">// Fork protection - // - // We implement fork protection on Unix using `pthread_atfork`. - // When the process is forked, we increment `RESEEDING_RNG_FORK_COUNTER`. - // Every `ReseedingRng` stores the last known value of the static in - // `fork_counter`. If the cached `fork_counter` is less than - // `RESEEDING_RNG_FORK_COUNTER`, it is time to reseed this RNG. - // - // If reseeding fails, we don't deal with this by setting a delay, but just - // don't update `fork_counter`, so a reseed is attempted as soon as - // possible. - - </span><span class="kw">static </span>RESEEDING_RNG_FORK_COUNTER: AtomicUsize = AtomicUsize::new(<span class="number">0</span>); - - <span class="kw">pub fn </span>get_fork_counter() -> usize { - RESEEDING_RNG_FORK_COUNTER.load(Ordering::Relaxed) - } - - <span class="kw">extern </span><span class="string">"C" </span><span class="kw">fn </span>fork_handler() { - <span class="comment">// Note: fetch_add is defined to wrap on overflow - // (which is what we want). - </span>RESEEDING_RNG_FORK_COUNTER.fetch_add(<span class="number">1</span>, Ordering::Relaxed); - } - - <span class="kw">pub fn </span>register_fork_handler() { - <span class="kw">static </span>REGISTER: Once = Once::new(); - REGISTER.call_once(|| { - <span class="comment">// Bump the counter before and after forking (see #1169): - </span><span class="kw">let </span>ret = <span class="kw">unsafe </span>{ libc::pthread_atfork( - <span class="prelude-val">Some</span>(fork_handler), - <span class="prelude-val">Some</span>(fork_handler), - <span class="prelude-val">Some</span>(fork_handler), - ) }; - <span class="kw">if </span>ret != <span class="number">0 </span>{ - <span class="macro">panic!</span>(<span class="string">"libc::pthread_atfork failed with code {}"</span>, ret); - } - }); - } -} - -<span class="attribute">#[cfg(not(all(unix, not(target_os = <span class="string">"emscripten"</span>))))] -</span><span class="kw">mod </span>fork { - <span class="kw">pub fn </span>get_fork_counter() -> usize { - <span class="number">0 - </span>} - <span class="kw">pub fn </span>register_fork_handler() {} -} - - -<span class="attribute">#[cfg(feature = <span class="string">"std_rng"</span>)] -#[cfg(test)] -</span><span class="kw">mod </span>test { - <span class="kw">use </span><span class="kw">super</span>::ReseedingRng; - <span class="kw">use </span><span class="kw">crate</span>::rngs::mock::StepRng; - <span class="kw">use </span><span class="kw">crate</span>::rngs::std::Core; - <span class="kw">use crate</span>::{Rng, SeedableRng}; - - <span class="attribute">#[test] - </span><span class="kw">fn </span>test_reseeding() { - <span class="kw">let </span><span class="kw-2">mut </span>zero = StepRng::new(<span class="number">0</span>, <span class="number">0</span>); - <span class="kw">let </span>rng = Core::from_rng(<span class="kw-2">&mut </span>zero).unwrap(); - <span class="kw">let </span>thresh = <span class="number">1</span>; <span class="comment">// reseed every time the buffer is exhausted - </span><span class="kw">let </span><span class="kw-2">mut </span>reseeding = ReseedingRng::new(rng, thresh, zero); - - <span class="comment">// RNG buffer size is [u32; 64] - // Debug is only implemented up to length 32 so use two arrays - </span><span class="kw">let </span><span class="kw-2">mut </span>buf = ([<span class="number">0u32</span>; <span class="number">32</span>], [<span class="number">0u32</span>; <span class="number">32</span>]); - reseeding.fill(<span class="kw-2">&mut </span>buf.<span class="number">0</span>); - reseeding.fill(<span class="kw-2">&mut </span>buf.<span class="number">1</span>); - <span class="kw">let </span>seq = buf; - <span class="kw">for _ in </span><span class="number">0</span>..<span class="number">10 </span>{ - reseeding.fill(<span class="kw-2">&mut </span>buf.<span class="number">0</span>); - reseeding.fill(<span class="kw-2">&mut </span>buf.<span class="number">1</span>); - <span class="macro">assert_eq!</span>(buf, seq); - } - } - - <span class="attribute">#[test] - </span><span class="kw">fn </span>test_clone_reseeding() { - <span class="attribute">#![allow(clippy::redundant_clone)] - - </span><span class="kw">let </span><span class="kw-2">mut </span>zero = StepRng::new(<span class="number">0</span>, <span class="number">0</span>); - <span class="kw">let </span>rng = Core::from_rng(<span class="kw-2">&mut </span>zero).unwrap(); - <span class="kw">let </span><span class="kw-2">mut </span>rng1 = ReseedingRng::new(rng, <span class="number">32 </span>* <span class="number">4</span>, zero); - - <span class="kw">let </span>first: u32 = rng1.gen(); - <span class="kw">for _ in </span><span class="number">0</span>..<span class="number">10 </span>{ - <span class="kw">let _ </span>= rng1.gen::<u32>(); - } - - <span class="kw">let </span><span class="kw-2">mut </span>rng2 = rng1.clone(); - <span class="macro">assert_eq!</span>(first, rng2.gen::<u32>()); - } -} -</code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../../" data-current-crate="rand" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1)" ></div></body></html>
\ No newline at end of file diff --git a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/mock.rs.html b/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/mock.rs.html deleted file mode 100644 index 819baf1..0000000 --- a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/mock.rs.html +++ /dev/null @@ -1,177 +0,0 @@ -<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `/home/pico/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.5/src/rngs/mock.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>mock.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"></nav><main><div class="width-limiter"><nav class="sub"><a class="sub-logo-container" href="../../../rand/index.html"> - <img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><form class="search-form"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></form></nav><section id="main-content" class="content"><div class="example-wrap"><pre class="src-line-numbers"><span id="1">1</span> -<span id="2">2</span> -<span id="3">3</span> -<span id="4">4</span> -<span id="5">5</span> -<span id="6">6</span> -<span id="7">7</span> -<span id="8">8</span> -<span id="9">9</span> -<span id="10">10</span> -<span id="11">11</span> -<span id="12">12</span> -<span id="13">13</span> -<span id="14">14</span> -<span id="15">15</span> -<span id="16">16</span> -<span id="17">17</span> -<span id="18">18</span> -<span id="19">19</span> -<span id="20">20</span> -<span id="21">21</span> -<span id="22">22</span> -<span id="23">23</span> -<span id="24">24</span> -<span id="25">25</span> -<span id="26">26</span> -<span id="27">27</span> -<span id="28">28</span> -<span id="29">29</span> -<span id="30">30</span> -<span id="31">31</span> -<span id="32">32</span> -<span id="33">33</span> -<span id="34">34</span> -<span id="35">35</span> -<span id="36">36</span> -<span id="37">37</span> -<span id="38">38</span> -<span id="39">39</span> -<span id="40">40</span> -<span id="41">41</span> -<span id="42">42</span> -<span id="43">43</span> -<span id="44">44</span> -<span id="45">45</span> -<span id="46">46</span> -<span id="47">47</span> -<span id="48">48</span> -<span id="49">49</span> -<span id="50">50</span> -<span id="51">51</span> -<span id="52">52</span> -<span id="53">53</span> -<span id="54">54</span> -<span id="55">55</span> -<span id="56">56</span> -<span id="57">57</span> -<span id="58">58</span> -<span id="59">59</span> -<span id="60">60</span> -<span id="61">61</span> -<span id="62">62</span> -<span id="63">63</span> -<span id="64">64</span> -<span id="65">65</span> -<span id="66">66</span> -<span id="67">67</span> -<span id="68">68</span> -<span id="69">69</span> -<span id="70">70</span> -<span id="71">71</span> -<span id="72">72</span> -<span id="73">73</span> -<span id="74">74</span> -<span id="75">75</span> -<span id="76">76</span> -<span id="77">77</span> -<span id="78">78</span> -<span id="79">79</span> -<span id="80">80</span> -<span id="81">81</span> -<span id="82">82</span> -<span id="83">83</span> -<span id="84">84</span> -<span id="85">85</span> -<span id="86">86</span> -<span id="87">87</span> -</pre><pre class="rust"><code><span class="comment">// Copyright 2018 Developers of the Rand project. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -</span><span class="doccomment">//! Mock random number generator - -</span><span class="kw">use </span>rand_core::{impls, Error, RngCore}; - -<span class="attribute">#[cfg(feature = <span class="string">"serde1"</span>)] -</span><span class="kw">use </span>serde::{Serialize, Deserialize}; - -<span class="doccomment">/// A simple implementation of `RngCore` for testing purposes. -/// -/// This generates an arithmetic sequence (i.e. adds a constant each step) -/// over a `u64` number, using wrapping arithmetic. If the increment is 0 -/// the generator yields a constant. -/// -/// ``` -/// use rand::Rng; -/// use rand::rngs::mock::StepRng; -/// -/// let mut my_rng = StepRng::new(2, 1); -/// let sample: [u64; 3] = my_rng.gen(); -/// assert_eq!(sample, [2, 3, 4]); -/// ``` -</span><span class="attribute">#[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature = <span class="string">"serde1"</span>, derive(Serialize, Deserialize))] -</span><span class="kw">pub struct </span>StepRng { - v: u64, - a: u64, -} - -<span class="kw">impl </span>StepRng { - <span class="doccomment">/// Create a `StepRng`, yielding an arithmetic sequence starting with - /// `initial` and incremented by `increment` each time. - </span><span class="kw">pub fn </span>new(initial: u64, increment: u64) -> <span class="self">Self </span>{ - StepRng { - v: initial, - a: increment, - } - } -} - -<span class="kw">impl </span>RngCore <span class="kw">for </span>StepRng { - <span class="attribute">#[inline] - </span><span class="kw">fn </span>next_u32(<span class="kw-2">&mut </span><span class="self">self</span>) -> u32 { - <span class="self">self</span>.next_u64() <span class="kw">as </span>u32 - } - - <span class="attribute">#[inline] - </span><span class="kw">fn </span>next_u64(<span class="kw-2">&mut </span><span class="self">self</span>) -> u64 { - <span class="kw">let </span>result = <span class="self">self</span>.v; - <span class="self">self</span>.v = <span class="self">self</span>.v.wrapping_add(<span class="self">self</span>.a); - result - } - - <span class="attribute">#[inline] - </span><span class="kw">fn </span>fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) { - impls::fill_bytes_via_next(<span class="self">self</span>, dest); - } - - <span class="attribute">#[inline] - </span><span class="kw">fn </span>try_fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) -> <span class="prelude-ty">Result</span><(), Error> { - <span class="self">self</span>.fill_bytes(dest); - <span class="prelude-val">Ok</span>(()) - } -} - -<span class="attribute">#[cfg(test)] -</span><span class="kw">mod </span>tests { - <span class="attribute">#[test] - #[cfg(feature = <span class="string">"serde1"</span>)] - </span><span class="kw">fn </span>test_serialization_step_rng() { - <span class="kw">use </span><span class="kw">super</span>::StepRng; - - <span class="kw">let </span>some_rng = StepRng::new(<span class="number">42</span>, <span class="number">7</span>); - <span class="kw">let </span>de_some_rng: StepRng = - bincode::deserialize(<span class="kw-2">&</span>bincode::serialize(<span class="kw-2">&</span>some_rng).unwrap()).unwrap(); - <span class="macro">assert_eq!</span>(some_rng.v, de_some_rng.v); - <span class="macro">assert_eq!</span>(some_rng.a, de_some_rng.a); - - } -} -</code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="rand" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1)" ></div></body></html>
\ No newline at end of file diff --git a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/mod.rs.html b/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/mod.rs.html deleted file mode 100644 index 48767fa..0000000 --- a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/mod.rs.html +++ /dev/null @@ -1,241 +0,0 @@ -<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `/home/pico/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.5/src/rngs/mod.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>mod.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"></nav><main><div class="width-limiter"><nav class="sub"><a class="sub-logo-container" href="../../../rand/index.html"> - <img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><form class="search-form"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></form></nav><section id="main-content" class="content"><div class="example-wrap"><pre class="src-line-numbers"><span id="1">1</span> -<span id="2">2</span> -<span id="3">3</span> -<span id="4">4</span> -<span id="5">5</span> -<span id="6">6</span> -<span id="7">7</span> -<span id="8">8</span> -<span id="9">9</span> -<span id="10">10</span> -<span id="11">11</span> -<span id="12">12</span> -<span id="13">13</span> -<span id="14">14</span> -<span id="15">15</span> -<span id="16">16</span> -<span id="17">17</span> -<span id="18">18</span> -<span id="19">19</span> -<span id="20">20</span> -<span id="21">21</span> -<span id="22">22</span> -<span id="23">23</span> -<span id="24">24</span> -<span id="25">25</span> -<span id="26">26</span> -<span id="27">27</span> -<span id="28">28</span> -<span id="29">29</span> -<span id="30">30</span> -<span id="31">31</span> -<span id="32">32</span> -<span id="33">33</span> -<span id="34">34</span> -<span id="35">35</span> -<span id="36">36</span> -<span id="37">37</span> -<span id="38">38</span> -<span id="39">39</span> -<span id="40">40</span> -<span id="41">41</span> -<span id="42">42</span> -<span id="43">43</span> -<span id="44">44</span> -<span id="45">45</span> -<span id="46">46</span> -<span id="47">47</span> -<span id="48">48</span> -<span id="49">49</span> -<span id="50">50</span> -<span id="51">51</span> -<span id="52">52</span> -<span id="53">53</span> -<span id="54">54</span> -<span id="55">55</span> -<span id="56">56</span> -<span id="57">57</span> -<span id="58">58</span> -<span id="59">59</span> -<span id="60">60</span> -<span id="61">61</span> -<span id="62">62</span> -<span id="63">63</span> -<span id="64">64</span> -<span id="65">65</span> -<span id="66">66</span> -<span id="67">67</span> -<span id="68">68</span> -<span id="69">69</span> -<span id="70">70</span> -<span id="71">71</span> -<span id="72">72</span> -<span id="73">73</span> -<span id="74">74</span> -<span id="75">75</span> -<span id="76">76</span> -<span id="77">77</span> -<span id="78">78</span> -<span id="79">79</span> -<span id="80">80</span> -<span id="81">81</span> -<span id="82">82</span> -<span id="83">83</span> -<span id="84">84</span> -<span id="85">85</span> -<span id="86">86</span> -<span id="87">87</span> -<span id="88">88</span> -<span id="89">89</span> -<span id="90">90</span> -<span id="91">91</span> -<span id="92">92</span> -<span id="93">93</span> -<span id="94">94</span> -<span id="95">95</span> -<span id="96">96</span> -<span id="97">97</span> -<span id="98">98</span> -<span id="99">99</span> -<span id="100">100</span> -<span id="101">101</span> -<span id="102">102</span> -<span id="103">103</span> -<span id="104">104</span> -<span id="105">105</span> -<span id="106">106</span> -<span id="107">107</span> -<span id="108">108</span> -<span id="109">109</span> -<span id="110">110</span> -<span id="111">111</span> -<span id="112">112</span> -<span id="113">113</span> -<span id="114">114</span> -<span id="115">115</span> -<span id="116">116</span> -<span id="117">117</span> -<span id="118">118</span> -<span id="119">119</span> -</pre><pre class="rust"><code><span class="comment">// Copyright 2018 Developers of the Rand project. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -</span><span class="doccomment">//! Random number generators and adapters -//! -//! ## Background: Random number generators (RNGs) -//! -//! Computers cannot produce random numbers from nowhere. We classify -//! random number generators as follows: -//! -//! - "True" random number generators (TRNGs) use hard-to-predict data sources -//! (e.g. the high-resolution parts of event timings and sensor jitter) to -//! harvest random bit-sequences, apply algorithms to remove bias and -//! estimate available entropy, then combine these bits into a byte-sequence -//! or an entropy pool. This job is usually done by the operating system or -//! a hardware generator (HRNG). -//! - "Pseudo"-random number generators (PRNGs) use algorithms to transform a -//! seed into a sequence of pseudo-random numbers. These generators can be -//! fast and produce well-distributed unpredictable random numbers (or not). -//! They are usually deterministic: given algorithm and seed, the output -//! sequence can be reproduced. They have finite period and eventually loop; -//! with many algorithms this period is fixed and can be proven sufficiently -//! long, while others are chaotic and the period depends on the seed. -//! - "Cryptographically secure" pseudo-random number generators (CSPRNGs) -//! are the sub-set of PRNGs which are secure. Security of the generator -//! relies both on hiding the internal state and using a strong algorithm. -//! -//! ## Traits and functionality -//! -//! All RNGs implement the [`RngCore`] trait, as a consequence of which the -//! [`Rng`] extension trait is automatically implemented. Secure RNGs may -//! additionally implement the [`CryptoRng`] trait. -//! -//! All PRNGs require a seed to produce their random number sequence. The -//! [`SeedableRng`] trait provides three ways of constructing PRNGs: -//! -//! - `from_seed` accepts a type specific to the PRNG -//! - `from_rng` allows a PRNG to be seeded from any other RNG -//! - `seed_from_u64` allows any PRNG to be seeded from a `u64` insecurely -//! - `from_entropy` securely seeds a PRNG from fresh entropy -//! -//! Use the [`rand_core`] crate when implementing your own RNGs. -//! -//! ## Our generators -//! -//! This crate provides several random number generators: -//! -//! - [`OsRng`] is an interface to the operating system's random number -//! source. Typically the operating system uses a CSPRNG with entropy -//! provided by a TRNG and some type of on-going re-seeding. -//! - [`ThreadRng`], provided by the [`thread_rng`] function, is a handle to a -//! thread-local CSPRNG with periodic seeding from [`OsRng`]. Because this -//! is local, it is typically much faster than [`OsRng`]. It should be -//! secure, though the paranoid may prefer [`OsRng`]. -//! - [`StdRng`] is a CSPRNG chosen for good performance and trust of security -//! (based on reviews, maturity and usage). The current algorithm is ChaCha12, -//! which is well established and rigorously analysed. -//! [`StdRng`] provides the algorithm used by [`ThreadRng`] but without -//! periodic reseeding. -//! - [`SmallRng`] is an **insecure** PRNG designed to be fast, simple, require -//! little memory, and have good output quality. -//! -//! The algorithms selected for [`StdRng`] and [`SmallRng`] may change in any -//! release and may be platform-dependent, therefore they should be considered -//! **not reproducible**. -//! -//! ## Additional generators -//! -//! **TRNGs**: The [`rdrand`] crate provides an interface to the RDRAND and -//! RDSEED instructions available in modern Intel and AMD CPUs. -//! The [`rand_jitter`] crate provides a user-space implementation of -//! entropy harvesting from CPU timer jitter, but is very slow and has -//! [security issues](https://github.com/rust-random/rand/issues/699). -//! -//! **PRNGs**: Several companion crates are available, providing individual or -//! families of PRNG algorithms. These provide the implementations behind -//! [`StdRng`] and [`SmallRng`] but can also be used directly, indeed *should* -//! be used directly when **reproducibility** matters. -//! Some suggestions are: [`rand_chacha`], [`rand_pcg`], [`rand_xoshiro`]. -//! A full list can be found by searching for crates with the [`rng` tag]. -//! -//! [`Rng`]: crate::Rng -//! [`RngCore`]: crate::RngCore -//! [`CryptoRng`]: crate::CryptoRng -//! [`SeedableRng`]: crate::SeedableRng -//! [`thread_rng`]: crate::thread_rng -//! [`rdrand`]: https://crates.io/crates/rdrand -//! [`rand_jitter`]: https://crates.io/crates/rand_jitter -//! [`rand_chacha`]: https://crates.io/crates/rand_chacha -//! [`rand_pcg`]: https://crates.io/crates/rand_pcg -//! [`rand_xoshiro`]: https://crates.io/crates/rand_xoshiro -//! [`rng` tag]: https://crates.io/keywords/rng - -</span><span class="attribute">#[cfg_attr(doc_cfg, doc(cfg(feature = <span class="string">"std"</span>)))] -#[cfg(feature = <span class="string">"std"</span>)] </span><span class="kw">pub mod </span>adapter; - -<span class="kw">pub mod </span>mock; <span class="comment">// Public so we don't export `StepRng` directly, making it a bit - // more clear it is intended for testing. - -</span><span class="attribute">#[cfg(all(feature = <span class="string">"small_rng"</span>, target_pointer_width = <span class="string">"64"</span>))] -</span><span class="kw">mod </span>xoshiro256plusplus; -<span class="attribute">#[cfg(all(feature = <span class="string">"small_rng"</span>, not(target_pointer_width = <span class="string">"64"</span>)))] -</span><span class="kw">mod </span>xoshiro128plusplus; -<span class="attribute">#[cfg(feature = <span class="string">"small_rng"</span>)] </span><span class="kw">mod </span>small; - -<span class="attribute">#[cfg(feature = <span class="string">"std_rng"</span>)] </span><span class="kw">mod </span>std; -<span class="attribute">#[cfg(all(feature = <span class="string">"std"</span>, feature = <span class="string">"std_rng"</span>))] </span><span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">mod </span>thread; - -<span class="attribute">#[cfg(feature = <span class="string">"small_rng"</span>)] </span><span class="kw">pub use </span><span class="self">self</span>::small::SmallRng; -<span class="attribute">#[cfg(feature = <span class="string">"std_rng"</span>)] </span><span class="kw">pub use </span><span class="self">self</span>::std::StdRng; -<span class="attribute">#[cfg(all(feature = <span class="string">"std"</span>, feature = <span class="string">"std_rng"</span>))] </span><span class="kw">pub use </span><span class="self">self</span>::thread::ThreadRng; - -<span class="attribute">#[cfg_attr(doc_cfg, doc(cfg(feature = <span class="string">"getrandom"</span>)))] -#[cfg(feature = <span class="string">"getrandom"</span>)] </span><span class="kw">pub use </span>rand_core::OsRng; -</code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="rand" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1)" ></div></body></html>
\ No newline at end of file diff --git a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/std.rs.html b/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/std.rs.html deleted file mode 100644 index a7d324f..0000000 --- a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/std.rs.html +++ /dev/null @@ -1,199 +0,0 @@ -<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `/home/pico/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.5/src/rngs/std.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>std.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"></nav><main><div class="width-limiter"><nav class="sub"><a class="sub-logo-container" href="../../../rand/index.html"> - <img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><form class="search-form"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></form></nav><section id="main-content" class="content"><div class="example-wrap"><pre class="src-line-numbers"><span id="1">1</span> -<span id="2">2</span> -<span id="3">3</span> -<span id="4">4</span> -<span id="5">5</span> -<span id="6">6</span> -<span id="7">7</span> -<span id="8">8</span> -<span id="9">9</span> -<span id="10">10</span> -<span id="11">11</span> -<span id="12">12</span> -<span id="13">13</span> -<span id="14">14</span> -<span id="15">15</span> -<span id="16">16</span> -<span id="17">17</span> -<span id="18">18</span> -<span id="19">19</span> -<span id="20">20</span> -<span id="21">21</span> -<span id="22">22</span> -<span id="23">23</span> -<span id="24">24</span> -<span id="25">25</span> -<span id="26">26</span> -<span id="27">27</span> -<span id="28">28</span> -<span id="29">29</span> -<span id="30">30</span> -<span id="31">31</span> -<span id="32">32</span> -<span id="33">33</span> -<span id="34">34</span> -<span id="35">35</span> -<span id="36">36</span> -<span id="37">37</span> -<span id="38">38</span> -<span id="39">39</span> -<span id="40">40</span> -<span id="41">41</span> -<span id="42">42</span> -<span id="43">43</span> -<span id="44">44</span> -<span id="45">45</span> -<span id="46">46</span> -<span id="47">47</span> -<span id="48">48</span> -<span id="49">49</span> -<span id="50">50</span> -<span id="51">51</span> -<span id="52">52</span> -<span id="53">53</span> -<span id="54">54</span> -<span id="55">55</span> -<span id="56">56</span> -<span id="57">57</span> -<span id="58">58</span> -<span id="59">59</span> -<span id="60">60</span> -<span id="61">61</span> -<span id="62">62</span> -<span id="63">63</span> -<span id="64">64</span> -<span id="65">65</span> -<span id="66">66</span> -<span id="67">67</span> -<span id="68">68</span> -<span id="69">69</span> -<span id="70">70</span> -<span id="71">71</span> -<span id="72">72</span> -<span id="73">73</span> -<span id="74">74</span> -<span id="75">75</span> -<span id="76">76</span> -<span id="77">77</span> -<span id="78">78</span> -<span id="79">79</span> -<span id="80">80</span> -<span id="81">81</span> -<span id="82">82</span> -<span id="83">83</span> -<span id="84">84</span> -<span id="85">85</span> -<span id="86">86</span> -<span id="87">87</span> -<span id="88">88</span> -<span id="89">89</span> -<span id="90">90</span> -<span id="91">91</span> -<span id="92">92</span> -<span id="93">93</span> -<span id="94">94</span> -<span id="95">95</span> -<span id="96">96</span> -<span id="97">97</span> -<span id="98">98</span> -</pre><pre class="rust"><code><span class="comment">// Copyright 2018 Developers of the Rand project. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -</span><span class="doccomment">//! The standard RNG - -</span><span class="kw">use crate</span>::{CryptoRng, Error, RngCore, SeedableRng}; - -<span class="kw">pub</span>(<span class="kw">crate</span>) <span class="kw">use </span>rand_chacha::ChaCha12Core <span class="kw">as </span>Core; - -<span class="kw">use </span>rand_chacha::ChaCha12Rng <span class="kw">as </span>Rng; - -<span class="doccomment">/// The standard RNG. The PRNG algorithm in `StdRng` is chosen to be efficient -/// on the current platform, to be statistically strong and unpredictable -/// (meaning a cryptographically secure PRNG). -/// -/// The current algorithm used is the ChaCha block cipher with 12 rounds. Please -/// see this relevant [rand issue] for the discussion. This may change as new -/// evidence of cipher security and performance becomes available. -/// -/// The algorithm is deterministic but should not be considered reproducible -/// due to dependence on configuration and possible replacement in future -/// library versions. For a secure reproducible generator, we recommend use of -/// the [rand_chacha] crate directly. -/// -/// [rand_chacha]: https://crates.io/crates/rand_chacha -/// [rand issue]: https://github.com/rust-random/rand/issues/932 -</span><span class="attribute">#[cfg_attr(doc_cfg, doc(cfg(feature = <span class="string">"std_rng"</span>)))] -#[derive(Clone, Debug, PartialEq, Eq)] -</span><span class="kw">pub struct </span>StdRng(Rng); - -<span class="kw">impl </span>RngCore <span class="kw">for </span>StdRng { - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>next_u32(<span class="kw-2">&mut </span><span class="self">self</span>) -> u32 { - <span class="self">self</span>.<span class="number">0</span>.next_u32() - } - - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>next_u64(<span class="kw-2">&mut </span><span class="self">self</span>) -> u64 { - <span class="self">self</span>.<span class="number">0</span>.next_u64() - } - - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) { - <span class="self">self</span>.<span class="number">0</span>.fill_bytes(dest); - } - - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>try_fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) -> <span class="prelude-ty">Result</span><(), Error> { - <span class="self">self</span>.<span class="number">0</span>.try_fill_bytes(dest) - } -} - -<span class="kw">impl </span>SeedableRng <span class="kw">for </span>StdRng { - <span class="kw">type </span>Seed = <Rng <span class="kw">as </span>SeedableRng>::Seed; - - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>from_seed(seed: <span class="self">Self</span>::Seed) -> <span class="self">Self </span>{ - StdRng(Rng::from_seed(seed)) - } - - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>from_rng<R: RngCore>(rng: R) -> <span class="prelude-ty">Result</span><<span class="self">Self</span>, Error> { - Rng::from_rng(rng).map(StdRng) - } -} - -<span class="kw">impl </span>CryptoRng <span class="kw">for </span>StdRng {} - - -<span class="attribute">#[cfg(test)] -</span><span class="kw">mod </span>test { - <span class="kw">use </span><span class="kw">crate</span>::rngs::StdRng; - <span class="kw">use crate</span>::{RngCore, SeedableRng}; - - <span class="attribute">#[test] - </span><span class="kw">fn </span>test_stdrng_construction() { - <span class="comment">// Test value-stability of StdRng. This is expected to break any time - // the algorithm is changed. - </span><span class="attribute">#[rustfmt::skip] - </span><span class="kw">let </span>seed = [<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>, <span class="number">23</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>, <span class="number">200</span>,<span class="number">1</span>,<span class="number">0</span>,<span class="number">0</span>, <span class="number">210</span>,<span class="number">30</span>,<span class="number">0</span>,<span class="number">0</span>, - <span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>, <span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>, <span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>, <span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>]; - - <span class="kw">let </span>target = [<span class="number">10719222850664546238</span>, <span class="number">14064965282130556830</span>]; - - <span class="kw">let </span><span class="kw-2">mut </span>rng0 = StdRng::from_seed(seed); - <span class="kw">let </span>x0 = rng0.next_u64(); - - <span class="kw">let </span><span class="kw-2">mut </span>rng1 = StdRng::from_rng(rng0).unwrap(); - <span class="kw">let </span>x1 = rng1.next_u64(); - - <span class="macro">assert_eq!</span>([x0, x1], target); - } -} -</code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="rand" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1)" ></div></body></html>
\ No newline at end of file diff --git a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/thread.rs.html b/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/thread.rs.html deleted file mode 100644 index 5243758..0000000 --- a/rust/theBook/chapter-2-guessing-game/guessing_game/target/doc/src/rand/rngs/thread.rs.html +++ /dev/null @@ -1,289 +0,0 @@ -<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Source of the Rust file `/home/pico/.local/share/cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.5/src/rngs/thread.rs`."><meta name="keywords" content="rust, rustlang, rust-lang"><title>thread.rs - source</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../../normalize.css"><link rel="stylesheet" href="../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../../ayu.css" disabled><link rel="stylesheet" href="../../../dark.css" disabled><link rel="stylesheet" href="../../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../../storage.js"></script><script defer src="../../../source-script.js"></script><script defer src="../../../source-files.js"></script><script defer src="../../../main.js"></script><noscript><link rel="stylesheet" href="../../../noscript.css"></noscript><link rel="icon" href="https://www.rust-lang.org/favicon.ico"></head><body class="rustdoc source"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"></nav><main><div class="width-limiter"><nav class="sub"><a class="sub-logo-container" href="../../../rand/index.html"> - <img src="https://www.rust-lang.org/logos/rust-logo-128x128-blk.png" alt="logo"></a><form class="search-form"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../../wheel.svg"></a></div></form></nav><section id="main-content" class="content"><div class="example-wrap"><pre class="src-line-numbers"><span id="1">1</span> -<span id="2">2</span> -<span id="3">3</span> -<span id="4">4</span> -<span id="5">5</span> -<span id="6">6</span> -<span id="7">7</span> -<span id="8">8</span> -<span id="9">9</span> -<span id="10">10</span> -<span id="11">11</span> -<span id="12">12</span> -<span id="13">13</span> -<span id="14">14</span> -<span id="15">15</span> -<span id="16">16</span> -<span id="17">17</span> -<span id="18">18</span> -<span id="19">19</span> -<span id="20">20</span> -<span id="21">21</span> -<span id="22">22</span> -<span id="23">23</span> -<span id="24">24</span> -<span id="25">25</span> -<span id="26">26</span> -<span id="27">27</span> -<span id="28">28</span> -<span id="29">29</span> -<span id="30">30</span> -<span id="31">31</span> -<span id="32">32</span> -<span id="33">33</span> -<span id="34">34</span> -<span id="35">35</span> -<span id="36">36</span> -<span id="37">37</span> -<span id="38">38</span> -<span id="39">39</span> -<span id="40">40</span> -<span id="41">41</span> -<span id="42">42</span> -<span id="43">43</span> -<span id="44">44</span> -<span id="45">45</span> -<span id="46">46</span> -<span id="47">47</span> -<span id="48">48</span> -<span id="49">49</span> -<span id="50">50</span> -<span id="51">51</span> -<span id="52">52</span> -<span id="53">53</span> -<span id="54">54</span> -<span id="55">55</span> -<span id="56">56</span> -<span id="57">57</span> -<span id="58">58</span> -<span id="59">59</span> -<span id="60">60</span> -<span id="61">61</span> -<span id="62">62</span> -<span id="63">63</span> -<span id="64">64</span> -<span id="65">65</span> -<span id="66">66</span> -<span id="67">67</span> -<span id="68">68</span> -<span id="69">69</span> -<span id="70">70</span> -<span id="71">71</span> -<span id="72">72</span> -<span id="73">73</span> -<span id="74">74</span> -<span id="75">75</span> -<span id="76">76</span> -<span id="77">77</span> -<span id="78">78</span> -<span id="79">79</span> -<span id="80">80</span> -<span id="81">81</span> -<span id="82">82</span> -<span id="83">83</span> -<span id="84">84</span> -<span id="85">85</span> -<span id="86">86</span> -<span id="87">87</span> -<span id="88">88</span> -<span id="89">89</span> -<span id="90">90</span> -<span id="91">91</span> -<span id="92">92</span> -<span id="93">93</span> -<span id="94">94</span> -<span id="95">95</span> -<span id="96">96</span> -<span id="97">97</span> -<span id="98">98</span> -<span id="99">99</span> -<span id="100">100</span> -<span id="101">101</span> -<span id="102">102</span> -<span id="103">103</span> -<span id="104">104</span> -<span id="105">105</span> -<span id="106">106</span> -<span id="107">107</span> -<span id="108">108</span> -<span id="109">109</span> -<span id="110">110</span> -<span id="111">111</span> -<span id="112">112</span> -<span id="113">113</span> -<span id="114">114</span> -<span id="115">115</span> -<span id="116">116</span> -<span id="117">117</span> -<span id="118">118</span> -<span id="119">119</span> -<span id="120">120</span> -<span id="121">121</span> -<span id="122">122</span> -<span id="123">123</span> -<span id="124">124</span> -<span id="125">125</span> -<span id="126">126</span> -<span id="127">127</span> -<span id="128">128</span> -<span id="129">129</span> -<span id="130">130</span> -<span id="131">131</span> -<span id="132">132</span> -<span id="133">133</span> -<span id="134">134</span> -<span id="135">135</span> -<span id="136">136</span> -<span id="137">137</span> -<span id="138">138</span> -<span id="139">139</span> -<span id="140">140</span> -<span id="141">141</span> -<span id="142">142</span> -<span id="143">143</span> -</pre><pre class="rust"><code><span class="comment">// Copyright 2018 Developers of the Rand project. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -</span><span class="doccomment">//! Thread-local random number generator - -</span><span class="kw">use </span>core::cell::UnsafeCell; -<span class="kw">use </span>std::rc::Rc; -<span class="kw">use </span>std::thread_local; - -<span class="kw">use </span><span class="kw">super</span>::std::Core; -<span class="kw">use </span><span class="kw">crate</span>::rngs::adapter::ReseedingRng; -<span class="kw">use </span><span class="kw">crate</span>::rngs::OsRng; -<span class="kw">use crate</span>::{CryptoRng, Error, RngCore, SeedableRng}; - -<span class="comment">// Rationale for using `UnsafeCell` in `ThreadRng`: -// -// Previously we used a `RefCell`, with an overhead of ~15%. There will only -// ever be one mutable reference to the interior of the `UnsafeCell`, because -// we only have such a reference inside `next_u32`, `next_u64`, etc. Within a -// single thread (which is the definition of `ThreadRng`), there will only ever -// be one of these methods active at a time. -// -// A possible scenario where there could be multiple mutable references is if -// `ThreadRng` is used inside `next_u32` and co. But the implementation is -// completely under our control. We just have to ensure none of them use -// `ThreadRng` internally, which is nonsensical anyway. We should also never run -// `ThreadRng` in destructors of its implementation, which is also nonsensical. - - -// Number of generated bytes after which to reseed `ThreadRng`. -// According to benchmarks, reseeding has a noticeable impact with thresholds -// of 32 kB and less. We choose 64 kB to avoid significant overhead. -</span><span class="kw">const </span>THREAD_RNG_RESEED_THRESHOLD: u64 = <span class="number">1024 </span>* <span class="number">64</span>; - -<span class="doccomment">/// A reference to the thread-local generator -/// -/// An instance can be obtained via [`thread_rng`] or via `ThreadRng::default()`. -/// This handle is safe to use everywhere (including thread-local destructors), -/// though it is recommended not to use inside a fork handler. -/// The handle cannot be passed between threads (is not `Send` or `Sync`). -/// -/// `ThreadRng` uses the same PRNG as [`StdRng`] for security and performance -/// and is automatically seeded from [`OsRng`]. -/// -/// Unlike `StdRng`, `ThreadRng` uses the [`ReseedingRng`] wrapper to reseed -/// the PRNG from fresh entropy every 64 kiB of random data as well as after a -/// fork on Unix (though not quite immediately; see documentation of -/// [`ReseedingRng`]). -/// Note that the reseeding is done as an extra precaution against side-channel -/// attacks and mis-use (e.g. if somehow weak entropy were supplied initially). -/// The PRNG algorithms used are assumed to be secure. -/// -/// [`ReseedingRng`]: crate::rngs::adapter::ReseedingRng -/// [`StdRng`]: crate::rngs::StdRng -</span><span class="attribute">#[cfg_attr(doc_cfg, doc(cfg(all(feature = <span class="string">"std"</span>, feature = <span class="string">"std_rng"</span>))))] -#[derive(Clone, Debug)] -</span><span class="kw">pub struct </span>ThreadRng { - <span class="comment">// Rc is explicitly !Send and !Sync - </span>rng: Rc<UnsafeCell<ReseedingRng<Core, OsRng>>>, -} - -<span class="macro">thread_local!</span>( - <span class="comment">// We require Rc<..> to avoid premature freeing when thread_rng is used - // within thread-local destructors. See #968. - </span><span class="kw">static </span>THREAD_RNG_KEY: Rc<UnsafeCell<ReseedingRng<Core, OsRng>>> = { - <span class="kw">let </span>r = Core::from_rng(OsRng).unwrap_or_else(|err| - <span class="macro">panic!</span>(<span class="string">"could not initialize thread_rng: {}"</span>, err)); - <span class="kw">let </span>rng = ReseedingRng::new(r, - THREAD_RNG_RESEED_THRESHOLD, - OsRng); - Rc::new(UnsafeCell::new(rng)) - } -); - -<span class="doccomment">/// Retrieve the lazily-initialized thread-local random number generator, -/// seeded by the system. Intended to be used in method chaining style, -/// e.g. `thread_rng().gen::<i32>()`, or cached locally, e.g. -/// `let mut rng = thread_rng();`. Invoked by the `Default` trait, making -/// `ThreadRng::default()` equivalent. -/// -/// For more information see [`ThreadRng`]. -</span><span class="attribute">#[cfg_attr(doc_cfg, doc(cfg(all(feature = <span class="string">"std"</span>, feature = <span class="string">"std_rng"</span>))))] -</span><span class="kw">pub fn </span>thread_rng() -> ThreadRng { - <span class="kw">let </span>rng = THREAD_RNG_KEY.with(|t| t.clone()); - ThreadRng { rng } -} - -<span class="kw">impl </span>Default <span class="kw">for </span>ThreadRng { - <span class="kw">fn </span>default() -> ThreadRng { - <span class="kw">crate</span>::prelude::thread_rng() - } -} - -<span class="kw">impl </span>RngCore <span class="kw">for </span>ThreadRng { - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>next_u32(<span class="kw-2">&mut </span><span class="self">self</span>) -> u32 { - <span class="comment">// SAFETY: We must make sure to stop using `rng` before anyone else - // creates another mutable reference - </span><span class="kw">let </span>rng = <span class="kw">unsafe </span>{ <span class="kw-2">&mut *</span><span class="self">self</span>.rng.get() }; - rng.next_u32() - } - - <span class="attribute">#[inline(always)] - </span><span class="kw">fn </span>next_u64(<span class="kw-2">&mut </span><span class="self">self</span>) -> u64 { - <span class="comment">// SAFETY: We must make sure to stop using `rng` before anyone else - // creates another mutable reference - </span><span class="kw">let </span>rng = <span class="kw">unsafe </span>{ <span class="kw-2">&mut *</span><span class="self">self</span>.rng.get() }; - rng.next_u64() - } - - <span class="kw">fn </span>fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) { - <span class="comment">// SAFETY: We must make sure to stop using `rng` before anyone else - // creates another mutable reference - </span><span class="kw">let </span>rng = <span class="kw">unsafe </span>{ <span class="kw-2">&mut *</span><span class="self">self</span>.rng.get() }; - rng.fill_bytes(dest) - } - - <span class="kw">fn </span>try_fill_bytes(<span class="kw-2">&mut </span><span class="self">self</span>, dest: <span class="kw-2">&mut </span>[u8]) -> <span class="prelude-ty">Result</span><(), Error> { - <span class="comment">// SAFETY: We must make sure to stop using `rng` before anyone else - // creates another mutable reference - </span><span class="kw">let </span>rng = <span class="kw">unsafe </span>{ <span class="kw-2">&mut *</span><span class="self">self</span>.rng.get() }; - rng.try_fill_bytes(dest) - } -} - -<span class="kw">impl </span>CryptoRng <span class="kw">for </span>ThreadRng {} - - -<span class="attribute">#[cfg(test)] -</span><span class="kw">mod </span>test { - <span class="attribute">#[test] - </span><span class="kw">fn </span>test_thread_rng() { - <span class="kw">use </span><span class="kw">crate</span>::Rng; - <span class="kw">let </span><span class="kw-2">mut </span>r = <span class="kw">crate</span>::thread_rng(); - r.gen::<i32>(); - <span class="macro">assert_eq!</span>(r.gen_range(<span class="number">0</span>..<span class="number">1</span>), <span class="number">0</span>); - } -} -</code></pre></div> -</section></div></main><div id="rustdoc-vars" data-root-path="../../../" data-current-crate="rand" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0 (69f9c33d7 2022-12-12) (Arch Linux rust 1:1.66.0-1)" ></div></body></html>
\ No newline at end of file |