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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use core::mem;
use std::io;
use crate::{Context, ContextUnit, Element, Preamble};
pub struct AtomicConfig;
impl Default for AtomicConfig {
fn default() -> Self {
AtomicConfig
}
}
impl From<&Config> for AtomicConfig {
fn from(_config: &Config) -> Self {
AtomicConfig
}
}
impl From<&Config> for Config {
fn from(config: &Config) -> Self {
*config
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Config {
pub zeroed_scalar_values: bool,
}
impl Default for Config {
fn default() -> Self {
Self::DEFAULT
}
}
impl Config {
pub const LEN: usize = mem::size_of::<bool>();
pub const DEFAULT: Self = Self {
zeroed_scalar_values: false,
};
pub const fn new() -> Self {
Self::DEFAULT
}
pub fn with_zeroed_scalar_values(&mut self, zeroed_scalar_values: bool) -> &mut Self {
self.zeroed_scalar_values = zeroed_scalar_values;
self
}
}
impl Element for Config {
type Config = AtomicConfig;
fn zeroed() -> Self {
Self::DEFAULT
}
fn len(_config: &Self::Config) -> usize {
Self::LEN
}
fn to_buffer(&self, _config: &Self::Config, context: &mut ContextUnit, buf: &mut [u8]) {
let _ = self
.zeroed_scalar_values
.encode(&AtomicConfig, context, buf);
}
fn try_from_buffer_in_place<S>(
&mut self,
config: &Self::Config,
context: &mut Context<S>,
buf: &[u8],
) -> io::Result<()>
where
S: io::Read + io::Seek,
{
Self::validate_buffer_len(config, buf.len())?;
let _ = self
.zeroed_scalar_values
.try_decode_in_place(&AtomicConfig, context, buf)?;
Ok(())
}
fn validate(&self, _preamble: &Preamble) -> io::Result<()> {
Ok(())
}
}
#[test]
fn builder_functions_works() {
assert!(
Config::new()
.with_zeroed_scalar_values(true)
.zeroed_scalar_values
);
assert!(
!Config::new()
.with_zeroed_scalar_values(false)
.zeroed_scalar_values
);
}
#[test]
fn zeroed_works() {
assert_eq!(Config::zeroed(), Config::DEFAULT)
}
#[test]
fn atomic_config_has_default() {
AtomicConfig::default();
}
#[test]
fn validate_works() {
Config::default()
.validate(&Default::default())
.expect("default config validate should pass");
}