Skip to content

Commit

Permalink
tls_codec: Rename functions in DeserializeBytes trait (#1251)
Browse files Browse the repository at this point in the history
* rename functions in DeserializeBytes trait, using _bytes postfix
* remove unnecessary qualifications
* remove deserialize_bytes from Deserialize trait
* add Changelog
  • Loading branch information
kkohbrok authored Nov 14, 2023
1 parent ce9d249 commit b01d57f
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 141 deletions.
16 changes: 16 additions & 0 deletions tls_codec/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- [#1251](https://github.com/RustCrypto/formats/pull/1251): Add `_bytes` suffix to function names in the `DeserializeBytes` trait to avoid collisions with function names in the `Deserialize` trait

### Removed

- [#1251](https://github.com/RustCrypto/formats/pull/1251): Remove the `tls_deserialize_bytes` function from the `Deserialize` trait
6 changes: 2 additions & 4 deletions tls_codec/benches/quic_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ fn vector(c: &mut Criterion) {
},
|serialized_long_vec| {
let _deserialized_long_vec =
<Vec<u8> as Deserialize>::tls_deserialize(&mut serialized_long_vec.as_slice())
.unwrap();
Vec::<u8>::tls_deserialize(&mut serialized_long_vec.as_slice()).unwrap();
},
BatchSize::SmallInput,
)
Expand All @@ -47,8 +46,7 @@ fn byte_vector(c: &mut Criterion) {
},
|serialized_long_vec| {
let _deserialized_long_vec =
<VLBytes as Deserialize>::tls_deserialize(&mut serialized_long_vec.as_slice())
.unwrap();
VLBytes::tls_deserialize(&mut serialized_long_vec.as_slice()).unwrap();
},
BatchSize::SmallInput,
)
Expand Down
12 changes: 4 additions & 8 deletions tls_codec/benches/tls_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ fn vector(c: &mut Criterion) {
TlsSliceU32(&long_vector).tls_serialize_detached().unwrap()
},
|serialized_long_vec| {
let _deserialized_long_vec = <TlsVecU32<u8> as Deserialize>::tls_deserialize(
&mut serialized_long_vec.as_slice(),
)
.unwrap();
let _deserialized_long_vec =
TlsVecU32::<u8>::tls_deserialize(&mut serialized_long_vec.as_slice()).unwrap();
},
BatchSize::SmallInput,
)
Expand All @@ -49,10 +47,8 @@ fn byte_vector(c: &mut Criterion) {
.unwrap()
},
|serialized_long_vec| {
let _deserialized_long_vec = <TlsVecU32<u8> as Deserialize>::tls_deserialize(
&mut serialized_long_vec.as_slice(),
)
.unwrap();
let _deserialized_long_vec =
TlsVecU32::<u8>::tls_deserialize(&mut serialized_long_vec.as_slice()).unwrap();
},
BatchSize::SmallInput,
)
Expand Down
10 changes: 5 additions & 5 deletions tls_codec/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1005,8 +1005,8 @@ fn impl_deserialize_bytes(parsed_ast: TlsStruct) -> TokenStream2 {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
quote! {
impl #impl_generics tls_codec::DeserializeBytes for #ident #ty_generics #where_clause {
fn tls_deserialize(bytes: &[u8]) -> core::result::Result<(Self, &[u8]), tls_codec::Error> {
#(let (#members_values, bytes) = #prefixes::tls_deserialize(bytes)?;)*
fn tls_deserialize_bytes(bytes: &[u8]) -> core::result::Result<(Self, &[u8]), tls_codec::Error> {
#(let (#members_values, bytes) = #prefixes::tls_deserialize_bytes(bytes)?;)*
Ok((Self {
#(#members: #members_values,)*
#(#members_default: Default::default(),)*
Expand Down Expand Up @@ -1049,7 +1049,7 @@ fn impl_deserialize_bytes(parsed_ast: TlsStruct) -> TokenStream2 {
.collect::<Vec<_>>();
quote! {
#discriminant => {
#(let (#member_values, remainder) = #prefixes::tls_deserialize(remainder)?;)*
#(let (#member_values, remainder) = #prefixes::tls_deserialize_bytes(remainder)?;)*
let result = #ident::#variant_id { #(#members: #member_values,)* };
Ok((result, remainder))
},
Expand All @@ -1059,9 +1059,9 @@ fn impl_deserialize_bytes(parsed_ast: TlsStruct) -> TokenStream2 {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
quote! {
impl #impl_generics tls_codec::DeserializeBytes for #ident #ty_generics #where_clause {
fn tls_deserialize(bytes: &[u8]) -> core::result::Result<(Self, &[u8]), tls_codec::Error> {
fn tls_deserialize_bytes(bytes: &[u8]) -> core::result::Result<(Self, &[u8]), tls_codec::Error> {
#discriminant_constants
let (discriminant, remainder) = <#repr as tls_codec::DeserializeBytes>::tls_deserialize(bytes)?;
let (discriminant, remainder) = #repr::tls_deserialize_bytes(bytes)?;
match discriminant {
#(#arms)*
_ => {
Expand Down
27 changes: 11 additions & 16 deletions tls_codec/derive/tests/decode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "std")]
use tls_codec::{
Deserialize, Error, Serialize, Size, TlsSliceU16, TlsVecU16, TlsVecU32, TlsVecU8, VLBytes,
Deserialize, DeserializeBytes, Error, Serialize, Size, TlsSliceU16, TlsVecU16, TlsVecU32,
TlsVecU8, VLBytes,
};
use tls_codec_derive::{TlsDeserialize, TlsDeserializeBytes, TlsSerialize, TlsSize};

Expand Down Expand Up @@ -58,8 +59,7 @@ fn tuple_struct() {
let serialized_t1 = t1.tls_serialize_detached().unwrap();
let deserialized_t1 = TupleStruct1::tls_deserialize(&mut serialized_t1.as_slice()).unwrap();
let (deserialized_bytes_t1, _remainder) =
<TupleStruct1 as tls_codec::DeserializeBytes>::tls_deserialize(serialized_t1.as_slice())
.unwrap();
TupleStruct1::tls_deserialize_bytes(serialized_t1.as_slice()).unwrap();
assert_eq!(t1, deserialized_t1);
assert_eq!(t1, deserialized_bytes_t1);
assert_eq!(
Expand All @@ -75,8 +75,7 @@ fn tuple_struct() {
let serialized_t2 = t2.tls_serialize_detached().unwrap();
let deserialized_t2 = TupleStruct::tls_deserialize(&mut serialized_t2.as_slice()).unwrap();
let (deserialized_bytes_t2, _remainder) =
<TupleStruct as tls_codec::DeserializeBytes>::tls_deserialize(serialized_t2.as_slice())
.unwrap();
TupleStruct::tls_deserialize_bytes(serialized_t2.as_slice()).unwrap();
assert_eq!(t2, deserialized_t2);
assert_eq!(t2, deserialized_bytes_t2);
assert_eq!(
Expand All @@ -94,8 +93,7 @@ fn simple_enum() {
let b = &[0u8, 5] as &[u8];
let mut b_reader = b;
let deserialized = ExtensionType::tls_deserialize(&mut b_reader).unwrap();
let (deserialized_bytes, _remainder) =
<ExtensionType as tls_codec::DeserializeBytes>::tls_deserialize(b).unwrap();
let (deserialized_bytes, _remainder) = ExtensionType::tls_deserialize_bytes(b).unwrap();
assert_eq!(ExtensionType::RatchetTree, deserialized);
assert_eq!(ExtensionType::RatchetTree, deserialized_bytes);

Expand All @@ -109,8 +107,7 @@ fn simple_enum() {
for variant in variants.iter() {
let deserialized = ExtensionType::tls_deserialize(&mut b_reader).unwrap();
assert_eq!(variant, &deserialized);
let (deserialized_bytes, remainder) =
<ExtensionType as tls_codec::DeserializeBytes>::tls_deserialize(b).unwrap();
let (deserialized_bytes, remainder) = ExtensionType::tls_deserialize_bytes(b).unwrap();
b = remainder;
assert_eq!(variant, &deserialized_bytes);
}
Expand All @@ -129,11 +126,9 @@ fn deserialize_tls_vec() {
assert_eq!(long_vector.len(), deserialized_long_vec.len());
assert_eq!(long_vector, deserialized_long_vec);
let (deserialized_long_vec_bytes, _remainder): (Vec<ExtensionStruct>, &[u8]) =
<TlsVecU16<ExtensionStruct> as tls_codec::DeserializeBytes>::tls_deserialize(
serialized_long_vec.as_slice(),
)
.map(|(v, r)| (v.into(), r))
.unwrap();
TlsVecU16::<ExtensionStruct>::tls_deserialize_bytes(serialized_long_vec.as_slice())
.map(|(v, r)| (v.into(), r))
.unwrap();
assert_eq!(long_vector.len(), deserialized_long_vec_bytes.len());
assert_eq!(long_vector, deserialized_long_vec_bytes);
}
Expand Down Expand Up @@ -304,8 +299,8 @@ mod custom_bytes {
TlsByteSliceU32(v).tls_serialize(writer)
}

pub fn tls_deserialize(bytes: &[u8]) -> Result<(Vec<u8>, &[u8]), tls_codec::Error> {
let (vec, remainder) = TlsByteVecU32::tls_deserialize(bytes)?;
pub fn tls_deserialize_bytes(bytes: &[u8]) -> Result<(Vec<u8>, &[u8]), tls_codec::Error> {
let (vec, remainder) = TlsByteVecU32::tls_deserialize_bytes(bytes)?;
Ok((vec.into_vec(), remainder))
}
}
Expand Down
73 changes: 32 additions & 41 deletions tls_codec/derive/tests/decode_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ struct SomeValue {
#[test]
fn simple_enum() {
let serialized = ExtensionType::KeyId.tls_serialize().unwrap();
let (deserialized, rest) =
<ExtensionType as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
let (deserialized, rest) = ExtensionType::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, ExtensionType::KeyId);
assert_eq!(rest, []);
assert!(rest.is_empty());
let serialized = ExtensionType::SomethingElse.tls_serialize().unwrap();
let (deserialized, rest) =
<ExtensionType as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
let (deserialized, rest) = ExtensionType::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, ExtensionType::SomethingElse);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[test]
Expand All @@ -50,10 +48,9 @@ fn simple_struct() {
additional_data: None,
};
let serialized = extension.tls_serialize().unwrap();
let (deserialized, rest) =
<ExtensionStruct as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
let (deserialized, rest) = ExtensionStruct::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, extension);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[test]
Expand All @@ -65,19 +62,18 @@ fn tuple_struct() {
};
let x = TupleStruct(ext, 6);
let serialized = x.tls_serialize().unwrap();
let (deserialized, rest) =
<TupleStruct as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
let (deserialized, rest) = TupleStruct::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[test]
fn byte_arrays() {
let x = [0u8, 1, 2, 3];
let serialized = x.tls_serialize().unwrap();
let (deserialized, rest) = <[u8; 4] as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
let (deserialized, rest) = <[u8; 4]>::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[derive(TlsSerializeBytes, TlsDeserializeBytes, TlsSize, Debug, PartialEq)]
Expand All @@ -98,10 +94,10 @@ mod custom {
v.tls_serialize()
}

pub fn tls_deserialize<T: DeserializeBytes>(
pub fn tls_deserialize_bytes<T: DeserializeBytes>(
bytes: &[u8],
) -> Result<(T, &[u8]), tls_codec::Error> {
<T as DeserializeBytes>::tls_deserialize(bytes)
T::tls_deserialize_bytes(bytes)
}
}

Expand All @@ -113,9 +109,9 @@ fn custom() {
};
let serialized = x.tls_serialize().unwrap();
assert_eq!(vec![3, 0, 1, 2, 3], serialized);
let (deserialized, rest) = <Custom as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
let (deserialized, rest) = Custom::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[derive(TlsSerializeBytes, TlsDeserializeBytes, TlsSize, Debug, PartialEq)]
Expand All @@ -128,10 +124,9 @@ enum EnumWithTupleVariant {
fn enum_with_tuple_variant() {
let x = EnumWithTupleVariant::A(3, 4);
let serialized = x.tls_serialize().unwrap();
let (deserialized, rest) =
<EnumWithTupleVariant as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
let (deserialized, rest) = EnumWithTupleVariant::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[derive(TlsSerializeBytes, TlsDeserializeBytes, TlsSize, Debug, PartialEq)]
Expand All @@ -144,10 +139,9 @@ enum EnumWithStructVariant {
fn enum_with_struct_variant() {
let x = EnumWithStructVariant::A { foo: 3, bar: 4 };
let serialized = x.tls_serialize().unwrap();
let (deserialized, rest) =
<EnumWithStructVariant as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
let (deserialized, rest) = EnumWithStructVariant::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[derive(TlsSerializeBytes, TlsDeserializeBytes, TlsSize, Debug, PartialEq)]
Expand All @@ -164,19 +158,19 @@ fn enum_with_data_and_discriminant() {
let serialized = x.tls_serialize().unwrap();

let (deserialized, rest) =
<EnumWithDataAndDiscriminant as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
EnumWithDataAndDiscriminant::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[test]
fn discriminant_is_incremented_implicitly() {
let x = EnumWithDataAndDiscriminant::B;
let serialized = x.tls_serialize().unwrap();
let (deserialized, rest) =
<EnumWithDataAndDiscriminant as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
EnumWithDataAndDiscriminant::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

mod discriminant {
Expand Down Expand Up @@ -209,26 +203,23 @@ fn enum_with_data_and_const_discriminant() {
let serialized = x.tls_serialize().unwrap();
assert_eq!(vec![0, 3, 4], serialized);
let (deserialized, rest) =
<EnumWithDataAndConstDiscriminant as DeserializeBytes>::tls_deserialize(&serialized)
.unwrap();
EnumWithDataAndConstDiscriminant::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());

let x = EnumWithDataAndConstDiscriminant::B;
let serialized = x.tls_serialize().unwrap();
let (deserialized, rest) =
<EnumWithDataAndConstDiscriminant as DeserializeBytes>::tls_deserialize(&serialized)
.unwrap();
EnumWithDataAndConstDiscriminant::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());

let x = EnumWithDataAndConstDiscriminant::C;
let serialized = x.tls_serialize().unwrap();
let (deserialized, rest) =
<EnumWithDataAndConstDiscriminant as DeserializeBytes>::tls_deserialize(&serialized)
.unwrap();
EnumWithDataAndConstDiscriminant::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[derive(TlsSerializeBytes, TlsDeserializeBytes, TlsSize, Debug, PartialEq)]
Expand All @@ -242,9 +233,9 @@ fn enum_with_custom_serialized_field() {
let x = EnumWithCustomSerializedField::A(vec![1, 2, 3]);
let serialized = x.tls_serialize().unwrap();
let (deserialized, rest) =
<EnumWithCustomSerializedField as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
EnumWithCustomSerializedField::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, x);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[test]
Expand All @@ -254,9 +245,9 @@ fn that_skip_attribute_on_struct_works() {
T: std::fmt::Debug + PartialEq + SerializeBytes + Size,
{
let serialized = test.tls_serialize().unwrap();
let (deserialized, rest) = <T as DeserializeBytes>::tls_deserialize(&serialized).unwrap();
let (deserialized, rest) = T::tls_deserialize_bytes(&serialized).unwrap();
assert_eq!(deserialized, expected);
assert_eq!(rest, []);
assert!(rest.is_empty());
}

#[derive(Debug, PartialEq, TlsSerializeBytes, TlsDeserializeBytes, TlsSize)]
Expand Down
2 changes: 1 addition & 1 deletion tls_codec/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<const LEN: usize> Deserialize for [u8; LEN] {

impl<const LEN: usize> DeserializeBytes for [u8; LEN] {
#[inline]
fn tls_deserialize(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
fn tls_deserialize_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
let out = bytes
.get(..LEN)
.ok_or(Error::EndOfStream)?
Expand Down
Loading

0 comments on commit b01d57f

Please sign in to comment.