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
|
//! Wrapper object for encoding reference types.
// TODO(tarcieri): replace with blanket impls of `Encode(Value)` for reference types?
use crate::{Encode, EncodeValue, Length, Result, Tag, Tagged, ValueOrd, Writer};
use core::cmp::Ordering;
/// Reference encoder: wrapper type which impls `Encode` for any reference to a
/// type which impls the same.
pub struct EncodeRef<'a, T>(pub &'a T);
impl<'a, T> AsRef<T> for EncodeRef<'a, T> {
fn as_ref(&self) -> &T {
self.0
}
}
impl<'a, T> Encode for EncodeRef<'a, T>
where
T: Encode,
{
fn encoded_len(&self) -> Result<Length> {
self.0.encoded_len()
}
fn encode(&self, writer: &mut impl Writer) -> Result<()> {
self.0.encode(writer)
}
}
/// Reference value encoder: wrapper type which impls `EncodeValue` and `Tagged`
/// for any reference type which impls the same.
///
/// By virtue of the blanket impl, this type also impls `Encode`.
pub struct EncodeValueRef<'a, T>(pub &'a T);
impl<'a, T> AsRef<T> for EncodeValueRef<'a, T> {
fn as_ref(&self) -> &T {
self.0
}
}
impl<'a, T> EncodeValue for EncodeValueRef<'a, T>
where
T: EncodeValue,
{
fn value_len(&self) -> Result<Length> {
self.0.value_len()
}
fn encode_value(&self, writer: &mut impl Writer) -> Result<()> {
self.0.encode_value(writer)
}
}
impl<'a, T> Tagged for EncodeValueRef<'a, T>
where
T: Tagged,
{
fn tag(&self) -> Tag {
self.0.tag()
}
}
impl<'a, T> ValueOrd for EncodeValueRef<'a, T>
where
T: ValueOrd,
{
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
self.0.value_cmp(other.0)
}
}
|