Skip to main content

mls_rs_core/
protocol_version.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// Copyright by contributors to this project.
3// SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5use core::ops::Deref;
6
7use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize};
8
9/// Wrapper type representing a protocol version identifier.
10#[derive(
11    Debug, Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd, MlsSize, MlsEncode, MlsDecode,
12)]
13#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15#[repr(transparent)]
16pub struct ProtocolVersion(u16);
17
18impl From<u16> for ProtocolVersion {
19    fn from(value: u16) -> Self {
20        ProtocolVersion(value)
21    }
22}
23
24impl From<ProtocolVersion> for u16 {
25    fn from(value: ProtocolVersion) -> Self {
26        value.0
27    }
28}
29
30impl Deref for ProtocolVersion {
31    type Target = u16;
32
33    fn deref(&self) -> &Self::Target {
34        &self.0
35    }
36}
37
38impl ProtocolVersion {
39    /// MLS version 1.0
40    pub const MLS_10: ProtocolVersion = ProtocolVersion(1);
41
42    /// Protocol version from a raw value, useful for testing.
43    pub const fn new(value: u16) -> ProtocolVersion {
44        ProtocolVersion(value)
45    }
46
47    /// Raw numerical wrapped value.
48    pub const fn raw_value(&self) -> u16 {
49        self.0
50    }
51
52    /// An iterator over all of the defined MLS versions.
53    pub fn all() -> impl Iterator<Item = ProtocolVersion> {
54        [Self::MLS_10].into_iter()
55    }
56}