amplify/traits/
dumb.rs

1// Rust language amplification library providing multiple generic trait
2// implementations, type wrappers, derive macros and other language enhancements
3//
4// Written in 2019-2022 by
5//     Dr. Maxim Orlovsky <orlovsky@ubideco.org>
6//
7// To the extent possible under law, the author(s) have dedicated all
8// copyright and related and neighboring rights to this software to
9// the public domain worldwide. This software is distributed without
10// any warranty.
11//
12// You should have received a copy of the MIT License
13// along with this software.
14// If not, see <https://opensource.org/licenses/MIT>.
15
16use crate::{Array, Wrapper};
17
18/// Used as an alternative to default for test and prototyping purposes, when a
19/// type can't have a default value, but you need to generate some dumb data.
20pub trait Dumb
21where
22    Self: Sized,
23{
24    /// Returns an object initialized with dumb data
25    fn dumb() -> Self;
26}
27
28impl Dumb for u8 {
29    #[cfg(feature = "rand")]
30    fn dumb() -> Self {
31        use rand::RngCore;
32        rand::rng().next_u32().to_be_bytes()[0]
33    }
34
35    #[cfg(not(feature = "rand"))]
36    fn dumb() -> Self {
37        1
38    }
39}
40
41impl<T, const LEN: usize> Dumb for Array<T, LEN>
42where
43    T: Dumb + Copy,
44{
45    fn dumb() -> Self {
46        Self::from_inner([T::dumb(); LEN])
47    }
48}
49
50// TODO: Implement for main primitive types
51// TODO: Implement for main collection types
52// TODO: Implement for types defined in this crate