1#![cfg_attr(not(feature = "std"), doc = "```ignore")]
6#![cfg_attr(feature = "std", doc = "```")]
7#![cfg_attr(not(feature = "std"), doc = "```ignore")]
24#![cfg_attr(feature = "std", doc = "```")]
25#![cfg_attr(not(feature = "std"), no_std)]
41
42use core::fmt::Debug;
43
44pub use rand_core_0_5;
46
47pub use rand_core_0_6;
49
50pub use rand_0_7;
52
53pub use rand_0_8;
55
56#[derive(Debug, Clone, PartialEq)]
58#[cfg_attr(feature = "defmt", derive(defmt::Format))]
59pub struct Forward<T>(pub T);
60
61pub trait ForwardCompat<T> {
63 fn forward(self) -> Forward<T>;
64}
65
66impl<T: rand_core_0_5::RngCore> ForwardCompat<T> for T {
67 fn forward(self) -> Forward<T> {
69 Forward(self)
70 }
71}
72
73impl<T: rand_core_0_5::RngCore> rand_core_0_6::RngCore for Forward<T> {
75 fn next_u32(&mut self) -> u32 {
76 self.0.next_u32()
77 }
78
79 fn next_u64(&mut self) -> u64 {
80 self.0.next_u64()
81 }
82
83 fn fill_bytes(&mut self, dest: &mut [u8]) {
84 self.0.fill_bytes(dest)
85 }
86
87 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_0_6::Error> {
88 let e = match self.0.try_fill_bytes(dest) {
90 Ok(_) => return Ok(()),
91 Err(e) => e,
92 };
93
94 if let Some(c) = e.code() {
96 return Err(rand_core_0_6::Error::from(c));
97 }
98
99 let c = unsafe { core::num::NonZeroU32::new_unchecked(getrandom::Error::CUSTOM_START) };
101 Err(rand_core_0_6::Error::from(c))
102 }
103}
104
105impl<T: rand_core_0_5::RngCore + rand_core_0_5::CryptoRng> rand_core_0_6::CryptoRng
107 for Forward<T>
108{
109}
110
111#[derive(Debug, Clone, PartialEq)]
113#[cfg_attr(feature = "defmt", derive(defmt::Format))]
114pub struct Backward<T>(pub T);
115
116pub trait BackwardCompat<T> {
118 fn backward(self) -> Backward<T>;
119}
120
121impl<T: rand_core_0_6::RngCore> BackwardCompat<T> for T {
122 fn backward(self) -> Backward<T> {
124 Backward(self)
125 }
126}
127
128impl<T: rand_core_0_6::RngCore> rand_core_0_5::RngCore for Backward<T> {
130 fn next_u32(&mut self) -> u32 {
131 self.0.next_u32()
132 }
133
134 fn next_u64(&mut self) -> u64 {
135 self.0.next_u64()
136 }
137
138 fn fill_bytes(&mut self, dest: &mut [u8]) {
139 self.0.fill_bytes(dest)
140 }
141
142 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_0_5::Error> {
143 let e = match self.0.try_fill_bytes(dest) {
145 Ok(_) => return Ok(()),
146 Err(e) => e,
147 };
148
149 if let Some(c) = e.code() {
151 return Err(rand_core_0_5::Error::from(c));
152 }
153
154 let c = unsafe { core::num::NonZeroU32::new_unchecked(getrandom::Error::CUSTOM_START) };
156 Err(rand_core_0_5::Error::from(c))
157 }
158}
159
160impl<T: rand_core_0_6::RngCore + rand_core_0_6::CryptoRng> rand_core_0_5::CryptoRng
162 for Backward<T>
163{
164}
165
166#[cfg(test)]
167mod tests {
168 #[test]
169 fn it_works() {
170 let result = 2 + 2;
171 assert_eq!(result, 4);
172 }
173}