Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tls_codec: Don't require std feature for consumers when deriving #1262

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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