Skip to content

Commit 35ece21

Browse files
committed
Merge branch 'develop'
Signed-off-by: Toralf Wittner <tw@dtex.org>
2 parents 567b8a1 + c2c2aa4 commit 35ece21

File tree

6 files changed

+102
-57
lines changed

6 files changed

+102
-57
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
# minicbor
99

10+
## `2.1.3`
11+
12+
- Depends on `minicbor-derive-0.18.3`.
13+
1014
## `2.1.2`
1115

1216
- Replaces `build.rs` with `target_has_atomic`. See pull request
@@ -433,6 +437,11 @@
433437

434438
# minicbor-derive
435439

440+
## `0.18.3`
441+
442+
- Fixes feature-handling, see pull request [#48](https://github.com/twittner/minicbor/pull/48) by
443+
@dtolnay for details.
444+
436445
## `0.18.2`
437446

438447
- Using `#[cbor(transparent)]` together with a custom codec (e.g. `#[cbor(with = "...")]`) will

minicbor-derive/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "minicbor-derive"
3-
version = "0.18.2"
3+
version = "0.18.3"
44
authors = ["Toralf Wittner <tw@dtex.org>"]
55
license = "BlueOak-1.0.0"
66
edition = "2024"
@@ -13,8 +13,8 @@ categories = ["encoding"]
1313
proc-macro = true
1414

1515
[features]
16-
alloc = []
17-
std = ["alloc"]
16+
alloc = [] # unused
17+
std = [] # unused
1818

1919
[dependencies]
2020
proc-macro2 = "1.0.79"

minicbor-derive/src/decode.rs

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,14 @@ fn gen_statements(fields: &Fields, encoding: Encoding, flat: bool) -> syn::Resul
330330
};
331331

332332
let value =
333-
if cfg!(any(feature = "alloc", feature = "std"))
334-
&& (field.attrs.borrow().is_some() || field.index.is_b())
333+
if (field.attrs.borrow().is_some() || field.index.is_b())
335334
&& is_cow(&field.typ, |t| is_str(t) || is_byte_slice(t))
336335
{
337-
if cfg!(feature = "std") {
338-
quote!(Some(std::borrow::Cow::Borrowed(__v777)))
339-
} else {
340-
quote!(Some(alloc::borrow::Cow::Borrowed(__v777)))
341-
}
336+
quote!(minicbor::__minicbor_cfg! {
337+
'std { Some(std::borrow::Cow::Borrowed(__v777)) }
338+
'alloc { Some(alloc::borrow::Cow::Borrowed(__v777)) }
339+
'otherwise { Some(__v777) }
340+
})
342341
} else {
343342
quote!(Some(__v777))
344343
};
@@ -443,42 +442,41 @@ fn make_transparent_impl
443442
.unwrap_or_else(|| default_decode_fn.clone());
444443

445444
let decode_call =
446-
if cfg!(any(feature = "alloc", feature = "std"))
447-
&& (field.attrs.borrow().is_some() || field.index.is_b())
445+
if (field.attrs.borrow().is_some() || field.index.is_b())
448446
&& is_cow(&field.typ, |t| is_str(t) || is_byte_slice(t))
449447
{
450-
let cow =
451-
if cfg!(feature = "std") {
452-
quote!(std::borrow::Cow::Borrowed(v))
453-
} else {
454-
quote!(alloc::borrow::Cow::Borrowed(v))
455-
};
456-
if field.is_name {
457-
let id = &field.ident;
458-
quote! {
459-
Ok(#name {
460-
#id: match #decode_fn(__d777, __ctx777) {
461-
Ok(v) => #cow,
462-
Err(e) => return Err(e)
463-
}
464-
})
448+
quote!(minicbor::__minicbor_cfg! {
449+
'std {
450+
match #decode_fn(__d777, __ctx777) {
451+
Ok(v) => std::borrow::Cow::Borrowed(v),
452+
Err(e) => return Err(e)
453+
}
465454
}
466-
} else {
467-
quote! {
468-
Ok(#name(match #decode_fn(__d777, __ctx777) {
469-
Ok(v) => #cow,
455+
'alloc {
456+
match #decode_fn(__d777, __ctx777) {
457+
Ok(v) => alloc::borrow::Cow::Borrowed(v),
470458
Err(e) => return Err(e)
471-
}))
459+
}
460+
}
461+
'otherwise {
462+
#decode_fn(__d777, __ctx777)?
472463
}
464+
})
465+
} else {
466+
quote! {
467+
#decode_fn(__d777, __ctx777)?
473468
}
474-
} else if field.is_name {
469+
};
470+
471+
let decode_body =
472+
if field.is_name {
475473
let id = &field.ident;
476474
quote! {
477-
Ok(#name { #id: #decode_fn(__d777, __ctx777)? })
475+
Ok(#name { #id: #decode_call })
478476
}
479477
} else {
480478
quote! {
481-
Ok(#name(#decode_fn(__d777, __ctx777)?))
479+
Ok(#name(#decode_call))
482480
}
483481
};
484482

@@ -521,7 +519,7 @@ fn make_transparent_impl
521519
Ok(quote! {
522520
impl #impl_generics minicbor::Decode<'bytes, Ctx> for #name #typ_generics #where_clause {
523521
fn decode(__d777: &mut minicbor::Decoder<'bytes>, __ctx777: &mut Ctx) -> core::result::Result<#name #typ_generics, minicbor::decode::Error> {
524-
#decode_call
522+
#decode_body
525523
}
526524

527525
#nil_impl
@@ -576,27 +574,25 @@ fn nil(f: &Field) -> proc_macro2::TokenStream {
576574

577575
fn decode_tag(a: &Attributes) -> proc_macro2::TokenStream {
578576
if let Some(t) = a.tag() {
579-
let err =
580-
if cfg!(feature = "std") {
581-
quote! {
582-
minicbor::decode::Error::tag_mismatch(__t777)
583-
.with_message(format!("expected tag {}", #t))
584-
.at(__p777)
585-
}
586-
} else if cfg!(feature = "alloc") {
587-
quote! {
588-
minicbor::decode::Error::tag_mismatch(__t777)
589-
.with_message(alloc::format!("expected tag {}", #t))
590-
.at(__p777)
591-
}
592-
} else {
593-
quote!(minicbor::decode::Error::tag_mismatch(__t777).at(__p777))
594-
};
595577
quote! {
596578
let __p777 = __d777.position();
597579
let __t777 = __d777.tag()?;
598580
if #t != __t777.as_u64() {
599-
return Err(#err)
581+
return Err(minicbor::__minicbor_cfg! {
582+
'std {
583+
minicbor::decode::Error::tag_mismatch(__t777)
584+
.with_message(format!("expected tag {}", #t))
585+
.at(__p777)
586+
}
587+
'alloc {
588+
minicbor::decode::Error::tag_mismatch(__t777)
589+
.with_message(alloc::format!("expected tag {}", #t))
590+
.at(__p777)
591+
}
592+
'otherwise {
593+
minicbor::decode::Error::tag_mismatch(__t777).at(__p777)
594+
}
595+
});
600596
}
601597
}
602598
} else {

minicbor/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "minicbor"
3-
version = "2.1.2"
3+
version = "2.1.3"
44
authors = ["Toralf Wittner <tw@dtex.org>"]
55
license = "BlueOak-1.0.0"
66
edition = "2024"
@@ -14,12 +14,12 @@ features = ["std", "derive", "half"]
1414

1515
[features]
1616
full = ["std", "derive", "half"]
17-
alloc = ["minicbor-derive?/alloc"]
18-
std = ["alloc", "minicbor-derive?/std"]
17+
alloc = []
18+
std = ["alloc"]
1919
derive = ["minicbor-derive"]
2020

2121
[dependencies]
22-
minicbor-derive = { version = "0.18.2", path = "../minicbor-derive", optional = true }
22+
minicbor-derive = { version = "0.18.3", path = "../minicbor-derive", optional = true }
2323
half = { version = "2.4.0", default-features = false, optional = true }
2424

2525
[dev-dependencies]

minicbor/src/derive.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#[cfg(feature = "std")]
2+
#[doc(hidden)]
3+
#[macro_export]
4+
macro_rules! __minicbor_cfg {
5+
(
6+
'std {$($std:tt)*}
7+
'alloc {$($alloc:tt)*}
8+
'otherwise {$($otherwise:tt)*}
9+
) => {
10+
$($std)*
11+
};
12+
}
13+
14+
#[cfg(all(feature = "alloc", not(feature = "std")))]
15+
#[doc(hidden)]
16+
#[macro_export]
17+
macro_rules! __minicbor_cfg {
18+
(
19+
'std {$($std:tt)*}
20+
'alloc {$($alloc:tt)*}
21+
'otherwise {$($otherwise:tt)*}
22+
) => {
23+
$($alloc)*
24+
};
25+
}
26+
27+
#[cfg(all(not(feature = "alloc"), not(feature = "std")))]
28+
#[doc(hidden)]
29+
#[macro_export]
30+
macro_rules! __minicbor_cfg {
31+
(
32+
'std {$($std:tt)*}
33+
'alloc {$($alloc:tt)*}
34+
'otherwise {$($otherwise:tt)*}
35+
) => {
36+
$($otherwise)*
37+
};
38+
}

minicbor/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ pub use encode::{Encode, Encoder, CborLen};
160160

161161
#[cfg(feature = "derive")]
162162
pub use minicbor_derive::*;
163+
#[cfg(feature = "derive")]
164+
mod derive;
163165

164166
#[cfg(feature = "alloc")]
165167
use core::convert::Infallible;

0 commit comments

Comments
 (0)