]> git.proxmox.com Git - cargo.git/blob - vendor/rand-0.5.6/src/prng/isaac_array.rs
New upstream version 0.33.0
[cargo.git] / vendor / rand-0.5.6 / src / prng / isaac_array.rs
1 // Copyright 2017-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // https://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! ISAAC helper functions for 256-element arrays.
12
13 // Terrible workaround because arrays with more than 32 elements do not
14 // implement `AsRef`, `Default`, `Serialize`, `Deserialize`, or any other
15 // traits for that matter.
16
17 #[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
18
19 const RAND_SIZE_LEN: usize = 8;
20 const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
21
22
23 #[derive(Copy, Clone)]
24 #[allow(missing_debug_implementations)]
25 #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
26 pub struct IsaacArray<T> {
27 #[cfg_attr(feature="serde1",serde(with="isaac_array_serde"))]
28 #[cfg_attr(feature="serde1", serde(bound(
29 serialize = "T: Serialize",
30 deserialize = "T: Deserialize<'de> + Copy + Default")))]
31 inner: [T; RAND_SIZE]
32 }
33
34 impl<T> ::core::convert::AsRef<[T]> for IsaacArray<T> {
35 #[inline(always)]
36 fn as_ref(&self) -> &[T] {
37 &self.inner[..]
38 }
39 }
40
41 impl<T> ::core::convert::AsMut<[T]> for IsaacArray<T> {
42 #[inline(always)]
43 fn as_mut(&mut self) -> &mut [T] {
44 &mut self.inner[..]
45 }
46 }
47
48 impl<T> ::core::ops::Deref for IsaacArray<T> {
49 type Target = [T; RAND_SIZE];
50 #[inline(always)]
51 fn deref(&self) -> &Self::Target {
52 &self.inner
53 }
54 }
55
56 impl<T> ::core::ops::DerefMut for IsaacArray<T> {
57 #[inline(always)]
58 fn deref_mut(&mut self) -> &mut [T; RAND_SIZE] {
59 &mut self.inner
60 }
61 }
62
63 impl<T> ::core::default::Default for IsaacArray<T> where T: Copy + Default {
64 fn default() -> IsaacArray<T> {
65 IsaacArray { inner: [T::default(); RAND_SIZE] }
66 }
67 }
68
69
70 #[cfg(feature="serde1")]
71 pub(super) mod isaac_array_serde {
72 const RAND_SIZE_LEN: usize = 8;
73 const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
74
75 use serde::{Deserialize, Deserializer, Serialize, Serializer};
76 use serde::de::{Visitor,SeqAccess};
77 use serde::de;
78
79 use core::fmt;
80
81 pub fn serialize<T, S>(arr: &[T;RAND_SIZE], ser: S) -> Result<S::Ok, S::Error>
82 where
83 T: Serialize,
84 S: Serializer
85 {
86 use serde::ser::SerializeTuple;
87
88 let mut seq = ser.serialize_tuple(RAND_SIZE)?;
89
90 for e in arr.iter() {
91 seq.serialize_element(&e)?;
92 }
93
94 seq.end()
95 }
96
97 #[inline]
98 pub fn deserialize<'de, T, D>(de: D) -> Result<[T;RAND_SIZE], D::Error>
99 where
100 T: Deserialize<'de>+Default+Copy,
101 D: Deserializer<'de>,
102 {
103 use core::marker::PhantomData;
104 struct ArrayVisitor<T> {
105 _pd: PhantomData<T>,
106 };
107 impl<'de,T> Visitor<'de> for ArrayVisitor<T>
108 where
109 T: Deserialize<'de>+Default+Copy
110 {
111 type Value = [T; RAND_SIZE];
112
113 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
114 formatter.write_str("Isaac state array")
115 }
116
117 #[inline]
118 fn visit_seq<A>(self, mut seq: A) -> Result<[T; RAND_SIZE], A::Error>
119 where
120 A: SeqAccess<'de>,
121 {
122 let mut out = [Default::default();RAND_SIZE];
123
124 for i in 0..RAND_SIZE {
125 match seq.next_element()? {
126 Some(val) => out[i] = val,
127 None => return Err(de::Error::invalid_length(i, &self)),
128 };
129 }
130
131 Ok(out)
132 }
133 }
134
135 de.deserialize_tuple(RAND_SIZE, ArrayVisitor{_pd: PhantomData})
136 }
137 }