moq_lite/coding/
version.rs1use crate::coding::*;
2
3use std::{fmt, ops::Deref};
4
5#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct Version(pub u64);
8
9#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct Alpn(pub &'static str);
12
13impl From<u64> for Version {
14 fn from(v: u64) -> Self {
15 Self(v)
16 }
17}
18
19impl From<Version> for u64 {
20 fn from(v: Version) -> Self {
21 v.0
22 }
23}
24
25impl<V> Decode<V> for Version {
26 fn decode<R: bytes::Buf>(r: &mut R, version: V) -> Result<Self, DecodeError> {
28 let v = u64::decode(r, version)?;
29 Ok(Self(v))
30 }
31}
32
33impl<V> Encode<V> for Version {
34 fn encode<W: bytes::BufMut>(&self, w: &mut W, version: V) {
35 self.0.encode(w, version);
36 }
37}
38
39impl fmt::Debug for Version {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 self.0.fmt(f)
42 }
43}
44
45#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
47pub struct Versions(Vec<Version>);
48
49impl<V: Clone> Decode<V> for Versions {
50 fn decode<R: bytes::Buf>(r: &mut R, version: V) -> Result<Self, DecodeError> {
52 let count = u64::decode(r, version.clone())?;
53 let mut vs = Vec::new();
54
55 for _ in 0..count {
56 let v = Version::decode(r, version.clone())?;
57 vs.push(v);
58 }
59
60 Ok(Self(vs))
61 }
62}
63
64impl<V: Clone> Encode<V> for Versions {
65 fn encode<W: bytes::BufMut>(&self, w: &mut W, version: V) {
67 self.0.len().encode(w, version.clone());
68
69 for v in &self.0 {
70 v.encode(w, version.clone());
71 }
72 }
73}
74
75impl Deref for Versions {
76 type Target = Vec<Version>;
77
78 fn deref(&self) -> &Self::Target {
79 &self.0
80 }
81}
82
83impl From<Vec<Version>> for Versions {
84 fn from(vs: Vec<Version>) -> Self {
85 Self(vs)
86 }
87}
88
89impl<const N: usize> From<[Version; N]> for Versions {
90 fn from(vs: [Version; N]) -> Self {
91 Self(vs.to_vec())
92 }
93}
94
95impl fmt::Debug for Versions {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 f.debug_list().entries(self.0.iter()).finish()
98 }
99}