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): better encode str fields #312

Merged
merged 3 commits into from
Dec 17, 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
3 changes: 2 additions & 1 deletion firmware/qemu/src/bin/log.out
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@
0.000102 INFO Hello 💜 & 🍕
0.000103 INFO EnumLarge::A051
0.000104 INFO EnumLarge::A269
0.000105 INFO QEMU test finished!
0.000105 INFO S { x: hi }
0.000106 INFO QEMU test finished!
3 changes: 2 additions & 1 deletion firmware/qemu/src/bin/log.release.out
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@
0.000100 INFO Hello 💜 & 🍕
0.000101 INFO EnumLarge::A051
0.000102 INFO EnumLarge::A269
0.000103 INFO QEMU test finished!
0.000103 INFO S { x: hi }
0.000104 INFO QEMU test finished!
9 changes: 9 additions & 0 deletions firmware/qemu/src/bin/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ fn main() -> ! {
defmt::info!("EnumLarge::{:?}", EnumLarge::A269);
}

{
#[derive(Format)]
struct S {
x: &'static str,
}

defmt::info!("{:?}", S { x: "hi" });
}

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

loop {
Expand Down
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ fn as_native_type(ty: &Type) -> Option<String> {
let s = ident.to_string();
match &*s {
"u8" | "u16" | "u32" | "usize" | "i8" | "i16" | "i32" | "isize" | "f32"
| "bool" => Some(s),
| "bool" | "str" => Some(s),
_ => None,
}
}
Expand Down
91 changes: 36 additions & 55 deletions tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ fn inc(index: u8, n: u8) -> u8 {

fn check_format_implementation(val: &(impl Format + ?Sized), expected_encoding: &[u8]) {
let mut f = InternalFormatter::new();
let g = Formatter {
inner: &mut f,
};
let g = Formatter { inner: &mut f };
val.format(g);
f.finalize();
assert_eq!(f.bytes(), expected_encoding);
Expand All @@ -58,9 +56,7 @@ fn check_format_implementation(val: &(impl Format + ?Sized), expected_encoding:
fn write() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(g, "The answer is {:u8}", 42);
assert_eq!(
Expand All @@ -72,9 +68,7 @@ fn write() {
);

let ref mut f2 = InternalFormatter::new();
let g2 = Formatter {
inner: f2,
};
let g2 = Formatter { inner: f2 };
write!(g2, "The answer is {:?}", 42u8);
assert_eq!(
f2.bytes(),
Expand All @@ -90,9 +84,7 @@ fn write() {
fn booleans_max_num_bool_flags() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(
g,
Expand All @@ -112,9 +104,7 @@ fn booleans_max_num_bool_flags() {
fn booleans_less_than_max_num_bool_flags() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(
g,
Expand All @@ -134,9 +124,7 @@ fn booleans_less_than_max_num_bool_flags() {
fn booleans_more_than_max_num_bool_flags() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(g, "encode 9 bools {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool}",
false, true, true, false, true, false, true, true, false, true);
Expand All @@ -154,9 +142,7 @@ fn booleans_more_than_max_num_bool_flags() {
fn booleans_mixed() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(
g,
Expand All @@ -177,9 +163,7 @@ fn booleans_mixed() {
fn booleans_mixed_no_trailing_bool() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(g, "encode mixed bools {:bool} {:u8}", false, 42);
assert_eq!(
Expand All @@ -195,9 +179,7 @@ fn booleans_mixed_no_trailing_bool() {
fn bitfields_mixed() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(
g,
Expand All @@ -219,9 +201,7 @@ fn bitfields_mixed() {
fn bitfields_across_octets() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(g, "bitfields {0:0..7} {0:9..14}", 0b0110_0011_1101_0010u16);
assert_eq!(
Expand All @@ -238,9 +218,7 @@ fn bitfields_across_octets() {
fn bitfields_truncate_lower() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(
g,
Expand All @@ -260,9 +238,7 @@ fn bitfields_truncate_lower() {
fn bitfields_assert_range_exclusive() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(g, "bitfields {0:6..8}", 0b1010_0101u8,);
assert_eq!(
Expand Down Expand Up @@ -302,9 +278,7 @@ fn boolean_struct_mixed() {

let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};
let g = Formatter { inner: f };

write!(
g,
Expand Down Expand Up @@ -934,9 +908,8 @@ fn derive_with_bounds() {
check_format_implementation(
&S2 { a: &1, b: &2 },
&[
index, // "S2 { a: {:u8}, b: {:u8} }}"
1,
2,
index, // "S2 { a: {:u8}, b: {:u8} }}"
1, 2,
],
);
}
Expand Down Expand Up @@ -990,13 +963,12 @@ fn issue_208() {

#[test]
fn enum_variants() {

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

#[rustfmt::skip]
Expand All @@ -1023,23 +995,32 @@ fn enum_variants() {
let e = EnumSmall::A2;

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

let e = EnumLarge::A269;

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

#[test]
fn derive_str() {
#[derive(Format)]
struct S {
x: &'static str,
}

let s = S { x: "hi" };

let index = fetch_string_index();
check_format_implementation(
&e,
&s,
&[
index,
13,
1,
index, // "S {{ s: {:str} }}" (NOTE: `s` field is not {:?})
// so no extra format string index here
2, // s.x.len()
104, // b'h'
105, // b'i'
],
);
}