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
use crate::{Decodable, Encodable, EncodeValue, Encoder, FixedTag, Length, Result, Tag};
pub trait Sequence<'a>: Decodable<'a> {
fn fields<F, T>(&self, f: F) -> Result<T>
where
F: FnOnce(&[&dyn Encodable]) -> Result<T>;
}
impl<'a, M> EncodeValue for M
where
M: Sequence<'a>,
{
fn value_len(&self) -> Result<Length> {
self.fields(|fields| {
fields
.iter()
.fold(Ok(Length::ZERO), |len, field| len + field.encoded_len()?)
})
}
fn encode_value(&self, encoder: &mut Encoder<'_>) -> Result<()> {
self.fields(|fields| {
for &field in fields {
field.encode(encoder)?;
}
Ok(())
})
}
}
impl<'a, M> FixedTag for M
where
M: Sequence<'a>,
{
const TAG: Tag = Tag::Sequence;
}