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 5 use core::ops::Deref; 6 7 use 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(all(feature = "ffi", not(test)), safer_ffi_gen::ffi_type)] 15 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] 16 #[repr(transparent)] 17 pub struct ProtocolVersion(u16); 18 19 impl From<u16> for ProtocolVersion { from(value: u16) -> Self20 fn from(value: u16) -> Self { 21 ProtocolVersion(value) 22 } 23 } 24 25 impl From<ProtocolVersion> for u16 { from(value: ProtocolVersion) -> Self26 fn from(value: ProtocolVersion) -> Self { 27 value.0 28 } 29 } 30 31 impl Deref for ProtocolVersion { 32 type Target = u16; 33 deref(&self) -> &Self::Target34 fn deref(&self) -> &Self::Target { 35 &self.0 36 } 37 } 38 39 impl ProtocolVersion { 40 /// MLS version 1.0 41 pub const MLS_10: ProtocolVersion = ProtocolVersion(1); 42 43 /// Protocol version from a raw value, useful for testing. new(value: u16) -> ProtocolVersion44 pub const fn new(value: u16) -> ProtocolVersion { 45 ProtocolVersion(value) 46 } 47 48 /// Raw numerical wrapped value. raw_value(&self) -> u1649 pub const fn raw_value(&self) -> u16 { 50 self.0 51 } 52 53 /// An iterator over all of the defined MLS versions. all() -> impl Iterator<Item = ProtocolVersion>54 pub fn all() -> impl Iterator<Item = ProtocolVersion> { 55 [Self::MLS_10].into_iter() 56 } 57 } 58