Skip to content

Commit

Permalink
[breaking change] Add no_std support for tls_codec_derive (#1135)
Browse files Browse the repository at this point in the history
Co-authored-by: Franziskus Kiefer <[email protected]>
  • Loading branch information
imor and franziskuskiefer authored Aug 10, 2023
1 parent 90a4324 commit 6e60929
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tls_codec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-features std,default,derive,serde,arbitrary
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-features std,default,serde,arbitrary

minimal-versions:
uses: RustCrypto/actions/.github/workflows/minimal-versions.yml@master
Expand Down Expand Up @@ -70,4 +70,4 @@ jobs:
- run: ${{ matrix.deps }}
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo hack test --feature-powerset
- run: cargo hack test -p tls_codec_derive --test encode\* --test decode\*
- run: cargo hack test -p tls_codec_derive --feature-powerset --test encode\* --test decode\*
4 changes: 2 additions & 2 deletions tls_codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ regex = "1.8"
[features]
default = [ "std" ]
arbitrary = [ "std", "dep:arbitrary" ]
derive = [ "std", "tls_codec_derive" ]
derive = [ "tls_codec_derive" ]
serde = [ "std", "dep:serde" ]
mls = [] # In MLS variable length vectors are limited compared to QUIC.
std = []
std = [ "tls_codec_derive?/std" ]

[[bench]]
name = "tls_vec"
Expand Down
4 changes: 4 additions & 0 deletions tls_codec/derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ proc-macro2 = "1.0"
[dev-dependencies]
tls_codec = { path = "../" }
trybuild = "1"

[features]
default = [ "std" ]
std = []
8 changes: 7 additions & 1 deletion tls_codec/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ fn impl_tls_size(parsed_ast: TlsStruct) -> TokenStream2 {
let field_len = match self {
#(#field_arms)*
};
std::mem::size_of::<#repr>() + field_len
core::mem::size_of::<#repr>() + field_len
}
}

Expand Down Expand Up @@ -740,6 +740,7 @@ fn impl_serialize(parsed_ast: TlsStruct, svariant: SerializeVariant) -> TokenStr
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;
#(
Expand All @@ -760,6 +761,7 @@ fn impl_serialize(parsed_ast: TlsStruct, svariant: SerializeVariant) -> TokenStr
}

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)
}
Expand Down Expand Up @@ -850,6 +852,7 @@ fn impl_serialize(parsed_ast: TlsStruct, svariant: SerializeVariant) -> TokenStr
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 {
Expand All @@ -859,6 +862,7 @@ fn impl_serialize(parsed_ast: TlsStruct, svariant: SerializeVariant) -> TokenStr
}

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)
}
Expand Down Expand Up @@ -909,6 +913,7 @@ fn impl_deserialize(parsed_ast: TlsStruct) -> TokenStream2 {
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> {
Ok(Self {
#(#members: #prefixes::tls_deserialize(bytes)?,)*
Expand Down Expand Up @@ -948,6 +953,7 @@ fn impl_deserialize(parsed_ast: TlsStruct) -> TokenStream2 {
quote! {
impl #impl_generics tls_codec::Deserialize for #ident #ty_generics #where_clause {
#[allow(non_upper_case_globals)]
#[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)?;
Expand Down
1 change: 1 addition & 0 deletions tls_codec/derive/tests/decode.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "std")]
use tls_codec::{
Deserialize, Error, Serialize, Size, TlsSliceU16, TlsVecU16, TlsVecU32, TlsVecU8, VLBytes,
};
Expand Down
Loading

0 comments on commit 6e60929

Please sign in to comment.