Skip to content

Commit

Permalink
Fixes write macro to not require the first arg to be mutable.
Browse files Browse the repository at this point in the history
Fixes impl formatter; moves from_raw from Formatter to InternalFormatter,
depending on use of unstable-test feature.
Fixes compile errors in test/encode.rs file.
  • Loading branch information
BriocheBerlin committed Dec 15, 2020
1 parent a163062 commit 859b78c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ pub fn write(ts: TokenStream) -> TokenStream {
let fmt = &write.fmt;
// FIXME: Introduce a new `"write"` tag and decode it in a loop (breaking change).
let sym = mksym(&ls, "fmt", false);
quote!(match (&mut #fmt.inner, #(&(#args)),*) {
quote!(match (#fmt.inner, #(&(#args)),*) {
(_fmt_, #(#pats),*) => {
_fmt_.write_macro_start();
// HACK conditional should not be here; see FIXME in `format`
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ mod test_only {
use super::Write;

#[doc(hidden)]
impl super::Formatter<'_> {
impl super::InternalFormatter {
/// Implementation detail
pub unsafe fn from_raw(_: NonNull<dyn Write>) -> Self {
unreachable!()
Expand Down
64 changes: 50 additions & 14 deletions tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ 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,
};

write!(f, "The answer is {:u8}", 42);
write!(g, "The answer is {:u8}", 42);
assert_eq!(
f.bytes(),
&[
Expand All @@ -68,10 +71,13 @@ fn write() {
]
);

let ref mut f = InternalFormatter::new();
write!(f, "The answer is {:?}", 42u8);
let ref mut f2 = InternalFormatter::new();
let g2 = Formatter {
inner: f2,
};
write!(g2, "The answer is {:?}", 42u8);
assert_eq!(
f.bytes(),
f2.bytes(),
&[
inc(index, 1), // "The answer is {:?}"
inc(index, 2), // "{:u8}" / impl Format for u8
Expand All @@ -84,9 +90,12 @@ fn write() {
fn booleans_max_num_bool_flags() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};

write!(
f,
g,
"encode 8 bools {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool}",
false, true, true, false, true, false, true, true
);
Expand All @@ -103,9 +112,12 @@ 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,
};

write!(
f,
g,
"encode 3 bools {:bool} {:bool} {:bool}",
false, true, true
);
Expand All @@ -122,8 +134,11 @@ 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,
};

write!(f, "encode 9 bools {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool} {:bool}",
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);
assert_eq!(
f.bytes(),
Expand All @@ -139,9 +154,12 @@ 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,
};

write!(
f,
g,
"encode mixed bools {:bool} {:bool} {:u8} {:bool}",
true, false, 42, true
);
Expand All @@ -159,8 +177,11 @@ 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,
};

write!(f, "encode mixed bools {:bool} {:u8}", false, 42);
write!(g, "encode mixed bools {:bool} {:u8}", false, 42);
assert_eq!(
f.bytes(),
&[
Expand All @@ -174,9 +195,12 @@ 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,
};

write!(
f,
g,
"bitfields {0:7..12}, {1:0..5}",
0b1110_0101_1111_0000u16, 0b1111_0000u8
);
Expand All @@ -195,8 +219,11 @@ fn bitfields_mixed() {
fn bitfields_across_octets() {
let index = fetch_string_index();
let ref mut f = InternalFormatter::new();
let g = Formatter {
inner: f,
};

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

write!(
f,
g,
"bitfields {0:9..14}",
0b0000_0000_0000_1111_0110_0011_1101_0010u32
);
Expand All @@ -230,8 +260,11 @@ 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,
};

write!(f, "bitfields {0:6..8}", 0b1010_0101u8,);
write!(g, "bitfields {0:6..8}", 0b1010_0101u8,);
assert_eq!(
f.bytes(),
&[
Expand Down Expand Up @@ -269,9 +302,12 @@ fn boolean_struct_mixed() {

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

write!(
f,
g,
"mixed formats {:bool} {:?}",
true,
X { y: false, z: true }
Expand Down

0 comments on commit 859b78c

Please sign in to comment.