1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2018-2019 Cryptape Technologies LLC.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Use [`rand`] for random number generation.
//!
//! [`rand`]: https://crates.io/crates/rand

use crate::fixed_uint::UintConstructor;
use crate::utils;
use quote::quote;

impl UintConstructor {
    pub fn with_rand(&self) {
        self.with_rand_defun_pub();
    }

    fn with_rand_defun_pub(&self) {
        let name = &self.ts.name;
        let bytes_size = &self.ts.bytes_size;
        let inner_type = &self.ts.inner_type;
        let loop_unit_amount = &utils::pure_uint_list_to_ts(0..self.info.unit_amount);
        let part = quote!(
            #[cfg(feature = "support_rand")]
            impl rand::AsByteSliceMut for #name {
                #[inline]
                fn as_byte_slice_mut(&mut self) -> &mut [u8] {
                    let inner = self.mut_inner();
                    unsafe {
                        &mut *(inner as *mut #inner_type as *mut [u8; #bytes_size])
                    }
                }
                #[inline]
                fn to_le(&mut self) {
                    let inner = self.mut_inner();
                    #({
                        let idx = #loop_unit_amount;
                        inner[idx] = inner[idx].to_le();
                    })*
                }
            }
        );
        self.implt(part);
        let part = quote!(
            /// Create a random fixed uint with a input random core.
            #[cfg(feature = "support_rand")]
            #[inline]
            pub fn random<R: rand::RngCore>(rng: &mut R) -> Self {
                use rand::Rng;
                let mut ret = Self::default();
                rng.fill(&mut ret);
                ret
            }
            /// Create a random fixed uint.
            #[cfg(feature = "support_rand")]
            #[inline]
            pub fn thread_random() -> Self {
                let mut rng = rand::thread_rng();
                Self::random(&mut rng)
            }
        );
        self.defun(part);
    }
}