Skip to content

Commit

Permalink
tls_codec: Don't require std feature for consumers when deriving (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer authored Nov 19, 2023
1 parent a8a67d1 commit b495ba2
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tls_codec/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### 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
- [#1135](https://github.com/RustCrypto/formats/pull/1135): `no_std` support for the derive crate. This requires the `std` feature to be enabled when using derive with `Serialize` and `Deserialize`.

### Removed

Expand Down
4 changes: 2 additions & 2 deletions tls_codec/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tls_codec"
version = "0.4.0-pre.1"
version = "0.4.0-pre.2"
authors = ["RustCrypto Developers"]
license = "Apache-2.0 OR MIT"
documentation = "https://docs.rs/tls_codec/"
Expand All @@ -18,7 +18,7 @@ zeroize = { version = "1.7", default-features = false, features = [

# optional dependencies
arbitrary = { version = "1.3", features = ["derive"], optional = true }
tls_codec_derive = { version = "=0.4.0-pre.1", path = "./derive", optional = true }
tls_codec_derive = { version = "=0.4.0-pre.2", path = "./derive", optional = true }
serde = { version = "1.0.184", features = ["derive"], optional = true }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion tls_codec/derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tls_codec_derive"
version = "0.4.0-pre.1"
version = "0.4.0-pre.2"
authors = ["RustCrypto Developers"]
license = "Apache-2.0 OR MIT"
documentation = "https://docs.rs/tls_codec_derive/"
Expand Down
122 changes: 71 additions & 51 deletions tls_codec/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,34 +814,39 @@ fn impl_serialize(parsed_ast: TlsStruct, svariant: SerializeVariant) -> TokenStr

match svariant {
SerializeVariant::Write => {
quote! {
impl #impl_generics tls_codec::Serialize for #ident #ty_generics #where_clause {
#[cfg(feature = "std")]
fn tls_serialize<W: std::io::Write>(&self, writer: &mut W) -> core::result::Result<usize, tls_codec::Error> {
let mut written = 0usize;
#(
written += #prefixes::tls_serialize(&self.#members, writer)?;
)*
if cfg!(debug_assertions) {
let expected_written = tls_codec::Size::tls_serialized_len(&self);
debug_assert_eq!(written, expected_written, "Expected to serialize {} bytes but only {} were generated.", expected_written, written);
if written != expected_written {
Err(tls_codec::Error::EncodingError(format!("Expected to serialize {} bytes but only {} were generated.", expected_written, written)))
if cfg!(feature = "std") {
quote! {
impl #impl_generics tls_codec::Serialize for #ident #ty_generics #where_clause {
fn tls_serialize<W: std::io::Write>(&self, writer: &mut W) -> core::result::Result<usize, tls_codec::Error> {
let mut written = 0usize;
#(
written += #prefixes::tls_serialize(&self.#members, writer)?;
)*
if cfg!(debug_assertions) {
let expected_written = tls_codec::Size::tls_serialized_len(&self);
debug_assert_eq!(written, expected_written, "Expected to serialize {} bytes but only {} were generated.", expected_written, written);
if written != expected_written {
Err(tls_codec::Error::EncodingError(format!("Expected to serialize {} bytes but only {} were generated.", expected_written, written)))
} else {
Ok(written)
}
} else {
Ok(written)
}
} else {
Ok(written)
}
}
}

impl #impl_generics tls_codec::Serialize for &#ident #ty_generics #where_clause {
#[cfg(feature = "std")]
fn tls_serialize<W: std::io::Write>(&self, writer: &mut W) -> core::result::Result<usize, tls_codec::Error> {
tls_codec::Serialize::tls_serialize(*self, writer)
impl #impl_generics tls_codec::Serialize for &#ident #ty_generics #where_clause {
fn tls_serialize<W: std::io::Write>(&self, writer: &mut W) -> core::result::Result<usize, tls_codec::Error> {
tls_codec::Serialize::tls_serialize(*self, writer)
}
}
}
} else {
quote! {
impl #impl_generics tls_codec::Serialize for #ident #ty_generics #where_clause {}
impl #impl_generics tls_codec::Serialize for &#ident #ty_generics #where_clause {}
}
}
}
SerializeVariant::Bytes => {
Expand Down Expand Up @@ -926,23 +931,28 @@ fn impl_serialize(parsed_ast: TlsStruct, svariant: SerializeVariant) -> TokenStr

match svariant {
SerializeVariant::Write => {
quote! {
impl #impl_generics tls_codec::Serialize for #ident #ty_generics #where_clause {
#[cfg(feature = "std")]
fn tls_serialize<W: std::io::Write>(&self, writer: &mut W) -> core::result::Result<usize, tls_codec::Error> {
#discriminant_constants
match self {
#(#arms)*
if cfg!(feature = "std") {
quote! {
impl #impl_generics tls_codec::Serialize for #ident #ty_generics #where_clause {
fn tls_serialize<W: std::io::Write>(&self, writer: &mut W) -> core::result::Result<usize, tls_codec::Error> {
#discriminant_constants
match self {
#(#arms)*
}
}
}
}

impl #impl_generics tls_codec::Serialize for &#ident #ty_generics #where_clause {
#[cfg(feature = "std")]
fn tls_serialize<W: std::io::Write>(&self, writer: &mut W) -> core::result::Result<usize, tls_codec::Error> {
tls_codec::Serialize::tls_serialize(*self, writer)
impl #impl_generics tls_codec::Serialize for &#ident #ty_generics #where_clause {
fn tls_serialize<W: std::io::Write>(&self, writer: &mut W) -> core::result::Result<usize, tls_codec::Error> {
tls_codec::Serialize::tls_serialize(*self, writer)
}
}
}
} else {
quote! {
impl #impl_generics tls_codec::Serialize for #ident #ty_generics #where_clause {}
impl #impl_generics tls_codec::Serialize for &#ident #ty_generics #where_clause {}
}
}
}
SerializeVariant::Bytes => {
Expand Down Expand Up @@ -1011,16 +1021,21 @@ fn impl_deserialize(parsed_ast: TlsStruct) -> TokenStream2 {
#[cfg(feature = "conditional_deserialization")]
let (impl_generics, ty_generics) =
restrict_conditional_generic(impl_generics, ty_generics, true);
quote! {
impl #impl_generics tls_codec::Deserialize for #ident #ty_generics #where_clause {
#[cfg(feature = "std")]
fn tls_deserialize<R: std::io::Read>(bytes: &mut R) -> core::result::Result<Self, tls_codec::Error> {
Ok(Self {
#(#members: #prefixes::tls_deserialize(bytes)?,)*
#(#members_default: Default::default(),)*
})
if cfg!(feature = "std") {
quote! {
impl #impl_generics tls_codec::Deserialize for #ident #ty_generics #where_clause {
fn tls_deserialize<R: std::io::Read>(bytes: &mut R) -> core::result::Result<Self, tls_codec::Error> {
Ok(Self {
#(#members: #prefixes::tls_deserialize(bytes)?,)*
#(#members_default: Default::default(),)*
})
}
}
}
} else {
quote! {
impl #impl_generics tls_codec::Deserialize for #ident #ty_generics #where_clause {}
}
}
}
TlsStruct::Enum(Enum {
Expand Down Expand Up @@ -1050,20 +1065,25 @@ fn impl_deserialize(parsed_ast: TlsStruct) -> TokenStream2 {
})
.collect::<Vec<_>>();
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
quote! {
impl #impl_generics tls_codec::Deserialize for #ident #ty_generics #where_clause {
#[cfg(feature = "std")]
fn tls_deserialize<R: std::io::Read>(bytes: &mut R) -> core::result::Result<Self, tls_codec::Error> {
#discriminant_constants
let discriminant = <#repr as tls_codec::Deserialize>::tls_deserialize(bytes)?;
match discriminant {
#(#arms)*
_ => {
Err(tls_codec::Error::UnknownValue(discriminant.into()))
},
if cfg!(feature = "std") {
quote! {
impl #impl_generics tls_codec::Deserialize for #ident #ty_generics #where_clause {
fn tls_deserialize<R: std::io::Read>(bytes: &mut R) -> core::result::Result<Self, tls_codec::Error> {
#discriminant_constants
let discriminant = <#repr as tls_codec::Deserialize>::tls_deserialize(bytes)?;
match discriminant {
#(#arms)*
_ => {
Err(tls_codec::Error::UnknownValue(discriminant.into()))
},
}
}
}
}
} else {
quote! {
impl #impl_generics tls_codec::Deserialize for #ident #ty_generics #where_clause {}
}
}
}
}
Expand Down

0 comments on commit b495ba2

Please sign in to comment.