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

derive(Format): Support more than 256 variants #302

Merged
merged 1 commit into from
Dec 16, 2020
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
25 changes: 22 additions & 3 deletions decoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, doc(cfg(unstable)))]

use core::convert::{TryFrom, TryInto};
use core::fmt::{self, Write as _};
use core::ops::Range;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -566,13 +567,31 @@ impl<'t, 'b> Decoder<'t, 'b> {

fn get_variant(&mut self, format: &'t str) -> Result<&'t str, DecodeError> {
assert!(format.contains("|"));
let discriminant = self.bytes.read_u8()?;

// NOTE nesting of enums, like "A|B(C|D)" is not possible; indirection is
// required: "A|B({:?})" where "{:?}" -> "C|D"
let num_variants = format.chars().filter(|c| *c == '|').count();

let discriminant: usize = if u8::try_from(num_variants).is_ok() {
self.bytes.read_u8()?.into()
} else if u16::try_from(num_variants).is_ok() {
self.bytes.read_u16::<LE>()?.into()
} else if u32::try_from(num_variants).is_ok() {
self.bytes
.read_u32::<LE>()?
.try_into()
.map_err(|_| DecodeError::Malformed)?
} else if u64::try_from(num_variants).is_ok() {
self.bytes
.read_u64::<LE>()?
.try_into()
.map_err(|_| DecodeError::Malformed)?
} else {
return Err(DecodeError::Malformed);
};

format
.split('|')
.nth(usize::from(discriminant))
.nth(discriminant)
.ok_or(DecodeError::Malformed)
}

Expand Down
4 changes: 3 additions & 1 deletion firmware/qemu/src/bin/log.out
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,6 @@
0.000100 INFO -170141183460469
0.000101 INFO Hello 💜
0.000102 INFO Hello 💜 & 🍕
0.000103 INFO QEMU test finished!
0.000103 INFO EnumLarge::A051
0.000104 INFO EnumLarge::A269
0.000105 INFO QEMU test finished!
4 changes: 3 additions & 1 deletion firmware/qemu/src/bin/log.release.out
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,6 @@
0.000098 INFO -170141183460469
0.000099 INFO Hello 💜
0.000100 INFO Hello 💜 & 🍕
0.000101 INFO QEMU test finished!
0.000101 INFO EnumLarge::A051
0.000102 INFO EnumLarge::A269
0.000103 INFO QEMU test finished!
26 changes: 26 additions & 0 deletions firmware/qemu/src/bin/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,32 @@ fn main() -> ! {
defmt::info!("Hello {:char}", '💜');
defmt::info!("Hello {:char} & {:?}", '💜', '🍕');

{
#[rustfmt::skip]
#[allow(dead_code)]
#[derive(Format)]
enum EnumLarge {
A000, A001, A002, A003, A004, A005, A006, A007, A008, A009, A010, A011, A012, A013, A014, A015, A016, A017,
A018, A019, A020, A021, A022, A023, A024, A025, A026, A027, A028, A029, A030, A031, A032, A033, A034, A035,
A036, A037, A038, A039, A040, A041, A042, A043, A044, A045, A046, A047, A048, A049, A050, A051, A052, A053,
A054, A055, A056, A057, A058, A059, A060, A061, A062, A063, A064, A065, A066, A067, A068, A069, A070, A071,
A072, A073, A074, A075, A076, A077, A078, A079, A080, A081, A082, A083, A084, A085, A086, A087, A088, A089,
A090, A091, A092, A093, A094, A095, A096, A097, A098, A099, A100, A101, A102, A103, A104, A105, A106, A107,
A108, A109, A110, A111, A112, A113, A114, A115, A116, A117, A118, A119, A120, A121, A122, A123, A124, A125,
A126, A127, A128, A129, A130, A131, A132, A133, A134, A135, A136, A137, A138, A139, A140, A141, A142, A143,
A144, A145, A146, A147, A148, A149, A150, A151, A152, A153, A154, A155, A156, A157, A158, A159, A160, A161,
A162, A163, A164, A165, A166, A167, A168, A169, A170, A171, A172, A173, A174, A175, A176, A177, A178, A179,
A180, A181, A182, A183, A184, A185, A186, A187, A188, A189, A190, A191, A192, A193, A194, A195, A196, A197,
A198, A199, A200, A201, A202, A203, A204, A205, A206, A207, A208, A209, A210, A211, A212, A213, A214, A215,
A216, A217, A218, A219, A220, A221, A222, A223, A224, A225, A226, A227, A228, A229, A230, A231, A232, A233,
A234, A235, A236, A237, A238, A239, A240, A241, A242, A243, A244, A245, A246, A247, A248, A249, A250, A251,
A252, A253, A254, A255, A256, A257, A258, A259, A260, A261, A262, A263, A264, A265, A266, A267, A268, A269,
}

defmt::info!("EnumLarge::{:?}", EnumLarge::A051);
defmt::info!("EnumLarge::{:?}", EnumLarge::A269);
}

defmt::info!("QEMU test finished!");

loop {
Expand Down
37 changes: 25 additions & 12 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! INTERNAL; DO NOT USE. Please use the `defmt` crate to access the functionality implemented here
mod symbol;

use core::convert::TryFrom;
use core::fmt::Write as _;
use proc_macro::TokenStream;

Expand Down Expand Up @@ -249,22 +250,13 @@ pub fn format(ts: TokenStream) -> TokenStream {
let mut exprs = vec![];
match input.data {
Data::Enum(de) => {
if de.variants.len() > 256 {
return parse::Error::new(
span,
"`#[derive(Format)]` does not support enums with more than 256 variants",
)
.to_compile_error()
.into();
}

if de.variants.is_empty() {
// For zero-variant enums, this is unreachable code.
exprs.push(quote!(match *self {}));
} else {
let mut arms = vec![];
let mut first = true;
for (var, i) in de.variants.iter().zip(0u8..) {
for (i, var) in de.variants.iter().enumerate() {
let vident = &var.ident;

if first {
Expand All @@ -278,13 +270,34 @@ pub fn format(ts: TokenStream) -> TokenStream {
let exprs = fields(&var.fields, &mut fs, &mut field_types, &mut pats);
let pats = quote!( { #(#pats),* } );

let encode_discriminant = if de.variants.len() == 1 {
let len = de.variants.len();
let encode_discriminant = if len == 1 {
// For single-variant enums, there is no need to encode the discriminant.
quote!()
} else {
} else if let (Ok(_), Ok(i)) = (u8::try_from(len), u8::try_from(i)) {
quote!(
f.inner.u8(&#i);
)
} else if let (Ok(_), Ok(i)) = (u16::try_from(len), u16::try_from(i)) {
quote!(
f.inner.u16(&#i);
)
} else if let (Ok(_), Ok(i)) = (u32::try_from(len), u32::try_from(i)) {
quote!(
f.inner.u32(&#i);
)
} else if let (Ok(_), Ok(i)) = (u64::try_from(len), u64::try_from(i)) {
quote!(
f.inner.u64(&#i);
)
} else {
// u128 case is omitted with the assumption, that usize is never greater than u64
return parse::Error::new(
span,
format!("`#[derive(Format)]` does not support enums with more than {} variants", u64::MAX),
)
.to_compile_error()
.into();
};

arms.push(quote!(
Expand Down
56 changes: 56 additions & 0 deletions tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,3 +987,59 @@ fn issue_208() {
],
);
}

#[test]
fn enum_variants() {

#[allow(dead_code)]
#[derive(Format)]
enum EnumSmall {
A0,
A1,
A2
}

#[rustfmt::skip]
#[allow(dead_code)]
#[derive(Format)]
enum EnumLarge {
A000, A001, A002, A003, A004, A005, A006, A007, A008, A009, A010, A011, A012, A013, A014, A015, A016, A017,
A018, A019, A020, A021, A022, A023, A024, A025, A026, A027, A028, A029, A030, A031, A032, A033, A034, A035,
A036, A037, A038, A039, A040, A041, A042, A043, A044, A045, A046, A047, A048, A049, A050, A051, A052, A053,
A054, A055, A056, A057, A058, A059, A060, A061, A062, A063, A064, A065, A066, A067, A068, A069, A070, A071,
A072, A073, A074, A075, A076, A077, A078, A079, A080, A081, A082, A083, A084, A085, A086, A087, A088, A089,
A090, A091, A092, A093, A094, A095, A096, A097, A098, A099, A100, A101, A102, A103, A104, A105, A106, A107,
A108, A109, A110, A111, A112, A113, A114, A115, A116, A117, A118, A119, A120, A121, A122, A123, A124, A125,
A126, A127, A128, A129, A130, A131, A132, A133, A134, A135, A136, A137, A138, A139, A140, A141, A142, A143,
A144, A145, A146, A147, A148, A149, A150, A151, A152, A153, A154, A155, A156, A157, A158, A159, A160, A161,
A162, A163, A164, A165, A166, A167, A168, A169, A170, A171, A172, A173, A174, A175, A176, A177, A178, A179,
A180, A181, A182, A183, A184, A185, A186, A187, A188, A189, A190, A191, A192, A193, A194, A195, A196, A197,
A198, A199, A200, A201, A202, A203, A204, A205, A206, A207, A208, A209, A210, A211, A212, A213, A214, A215,
A216, A217, A218, A219, A220, A221, A222, A223, A224, A225, A226, A227, A228, A229, A230, A231, A232, A233,
A234, A235, A236, A237, A238, A239, A240, A241, A242, A243, A244, A245, A246, A247, A248, A249, A250, A251,
A252, A253, A254, A255, A256, A257, A258, A259, A260, A261, A262, A263, A264, A265, A266, A267, A268, A269,
}

let e = EnumSmall::A2;

let index = fetch_string_index();
check_format_implementation(
&e,
&[
index,
2,
],
);

let e = EnumLarge::A269;

let index = fetch_string_index();
check_format_implementation(
&e,
&[
index,
13,
1,
],
);
}