Skip to main content

deterministic_rand/
seed.rs

1
2//!
3//! Master seed.
4//!
5
6/// Define a private namespace for all its items.
7mod private
8{
9  #[ cfg( feature = "no_std" ) ]
10  extern crate alloc;
11  #[ cfg( feature = "no_std" ) ]
12  use alloc ::string;
13
14  /// Master seed.
15  #[ derive( Clone, Debug, PartialEq, Eq ) ]
16  pub struct Seed( String );
17
18  impl Seed
19  {
20  /// Creates new seed from a string.
21  pub fn new< IntoString >( value: IntoString ) -> Self
22  where
23   IntoString: Into< String >,
24  {
25   Self( value.into() )
26 }
27
28  /// Used for simplifying seed creation from a [`u64`] seed.
29  #[ must_use ] pub fn from_integer( src: u64 ) -> Self
30  {
31   Self( format!( "master_seed_{src}" ) )
32 }
33
34  /// Random string as seed.
35  pub fn random() -> Self
36  {
37   use rand :: { distributions ::Alphanumeric, Rng };
38   let str: String = rand ::thread_rng()
39   .sample_iter( &Alphanumeric )
40   .take( 16 )
41   .map(char ::from)
42   .collect();
43   debug_assert!( !str.is_empty() );
44   Self( str )
45 }
46
47  /// Returns inner seed string value.
48  #[ must_use ] pub fn into_inner( self ) -> String
49  {
50   self.0
51 }
52 }
53
54  impl Default for Seed
55  {
56  fn default() -> Self
57  {
58   Self( "master_seed".to_owned() )
59 }
60 }
61
62  impl< IntoString > From< IntoString > for Seed
63  where
64  IntoString: Into< String >,
65  {
66  #[ inline( always ) ]
67  fn from( src: IntoString ) -> Self
68  {
69   Self ::new( src )
70 }
71 }
72
73
74}
75
76crate ::mod_interface!
77{
78  orphan use Seed;
79}