Repository: knurling-rs/defmt
Pull request #227 (2020-11-04 12:41:52)

Fix encoding of usize/isize slices
Fixes https://github.com/knurling-rs/defmt/issues/209


Patch:
diff --git a/firmware/qemu/src/bin/log.out b/firmware/qemu/src/bin/log.out
index 9b70ee0..56cd299 100644
--- a/firmware/qemu/src/bin/log.out
+++ b/firmware/qemu/src/bin/log.out
@@ -38,50 +38,52 @@
 0.000037 INFO e7=Some(X { y: Y { z: 42 } })
 0.000038 INFO true Flags { a: true, b: false, c: true }
 0.000039 INFO [true, true, false]
-0.000040 INFO S { x: 42, y: 43 }
-0.000041 INFO S { x: 44, y: 45 }
-0.000042 INFO S { x: 46, y: Some(47) }
-0.000043 INFO S { x: Some(48), y: 49 }
-0.000044 INFO A
-0.000045 INFO B(42)
-0.000046 INFO C { y: 43 }
-0.000047 INFO A
-0.000048 INFO B(44)
-0.000049 INFO C { y: 45 }
-0.000050 INFO A
-0.000051 INFO B(Some(46))
-0.000052 INFO C { y: Ok(47) }
-0.000053 INFO A
-0.000054 INFO B(Some(48))
-0.000055 INFO C { y: 49 }
-0.000056 INFO [None, Some(42)]
-0.000057 INFO [Ok(42), Err(43)]
-0.000058 INFO [A, B(42)]
-0.000059 INFO [S { x: 42, y: None }, S { x: 43, y: Some(44) }]
-0.000060 INFO [None, Some(S { x: 42, y: 256 })]
-0.000061 INFO [None, Some([42, 43])]
-0.000062 INFO in nested 123
-0.000063 INFO after nested log: NestedStruct { a: 170, b: 305419896 }
-0.000064 INFO I can now print the @ symbol!
-0.000065 INFO @nd @lso vi@ interned strings: this is @n interned string
-0.000066 INFO empty tuple: ()
-0.000067 INFO tuple of ints: (1, 2, 3)
-0.000068 INFO nested tuple of ints: (1, 2, (3, 4, 5), (6, 7, 8))
-0.000069 INFO super nested tuples: (((((((())))))), (((((((), ())))))))
-0.000070 INFO slice of tuples: [(1, 2), (3, 4), (5, 6)]
-0.000071 INFO tuple of slices: ([1, 2, 3], [4, 5, 6])
-0.000072 INFO tuple of [u8;4]: ([1, 2, 3, 4], [5, 6, 7, 8])
-0.000073 INFO [u8;0]: []
-0.000074 INFO [u8;4]: [1, 2, 3, 4]
-0.000075 INFO [i8;4]: [-1, 2, 3, -4]
-0.000076 INFO [(u32,u32);4]: [(1, 2), (3, 4), (5, 6), (7, 8)]
-0.000077 INFO [u8;0]: []
-0.000078 INFO [u8;4]: [1, 2, 3, 4]
-0.000079 INFO [i8;4]: [-1, 2, 3, -4]
-0.000080 INFO [u32;4]: [1, 2, 3, 4]
-0.000081 INFO [i32;4]: [-1, 2, 3, -4]
-0.000082 INFO [[u32;4];4]: [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]
-0.000083 INFO [Option<u32>;4]: [Some(1), None, Some(3), None]
-0.000084 INFO [(u32,u32);4]: [(1, 2), (3, 4), (5, 6), (7, 8)]
-0.000085 INFO 1-variant enum: A { fld: 123 }
-0.000086 INFO wrapped: A(A { fld: 200 })
+0.000040 INFO usize slice: [1, 2, 3]
+0.000041 INFO isize slice: [-1, -2, -3]
+0.000042 INFO S { x: 42, y: 43 }
+0.000043 INFO S { x: 44, y: 45 }
+0.000044 INFO S { x: 46, y: Some(47) }
+0.000045 INFO S { x: Some(48), y: 49 }
+0.000046 INFO A
+0.000047 INFO B(42)
+0.000048 INFO C { y: 43 }
+0.000049 INFO A
+0.000050 INFO B(44)
+0.000051 INFO C { y: 45 }
+0.000052 INFO A
+0.000053 INFO B(Some(46))
+0.000054 INFO C { y: Ok(47) }
+0.000055 INFO A
+0.000056 INFO B(Some(48))
+0.000057 INFO C { y: 49 }
+0.000058 INFO [None, Some(42)]
+0.000059 INFO [Ok(42), Err(43)]
+0.000060 INFO [A, B(42)]
+0.000061 INFO [S { x: 42, y: None }, S { x: 43, y: Some(44) }]
+0.000062 INFO [None, Some(S { x: 42, y: 256 })]
+0.000063 INFO [None, Some([42, 43])]
+0.000064 INFO in nested 123
+0.000065 INFO after nested log: NestedStruct { a: 170, b: 305419896 }
+0.000066 INFO I can now print the @ symbol!
+0.000067 INFO @nd @lso vi@ interned strings: this is @n interned string
+0.000068 INFO empty tuple: ()
+0.000069 INFO tuple of ints: (1, 2, 3)
+0.000070 INFO nested tuple of ints: (1, 2, (3, 4, 5), (6, 7, 8))
+0.000071 INFO super nested tuples: (((((((())))))), (((((((), ())))))))
+0.000072 INFO slice of tuples: [(1, 2), (3, 4), (5, 6)]
+0.000073 INFO tuple of slices: ([1, 2, 3], [4, 5, 6])
+0.000074 INFO tuple of [u8;4]: ([1, 2, 3, 4], [5, 6, 7, 8])
+0.000075 INFO [u8;0]: []
+0.000076 INFO [u8;4]: [1, 2, 3, 4]
+0.000077 INFO [i8;4]: [-1, 2, 3, -4]
+0.000078 INFO [(u32,u32);4]: [(1, 2), (3, 4), (5, 6), (7, 8)]
+0.000079 INFO [u8;0]: []
+0.000080 INFO [u8;4]: [1, 2, 3, 4]
+0.000081 INFO [i8;4]: [-1, 2, 3, -4]
+0.000082 INFO [u32;4]: [1, 2, 3, 4]
+0.000083 INFO [i32;4]: [-1, 2, 3, -4]
+0.000084 INFO [[u32;4];4]: [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]
+0.000085 INFO [Option<u32>;4]: [Some(1), None, Some(3), None]
+0.000086 INFO [(u32,u32);4]: [(1, 2), (3, 4), (5, 6), (7, 8)]
+0.000087 INFO 1-variant enum: A { fld: 123 }
+0.000088 INFO wrapped: A(A { fld: 200 })
diff --git a/firmware/qemu/src/bin/log.release.out b/firmware/qemu/src/bin/log.release.out
index e54fe3c..9949e32 100644
--- a/firmware/qemu/src/bin/log.release.out
+++ b/firmware/qemu/src/bin/log.release.out
@@ -36,50 +36,52 @@
 0.000035 INFO e7=Some(X { y: Y { z: 42 } })
 0.000036 INFO true Flags { a: true, b: false, c: true }
 0.000037 INFO [true, true, false]
-0.000038 INFO S { x: 42, y: 43 }
-0.000039 INFO S { x: 44, y: 45 }
-0.000040 INFO S { x: 46, y: Some(47) }
-0.000041 INFO S { x: Some(48), y: 49 }
-0.000042 INFO A
-0.000043 INFO B(42)
-0.000044 INFO C { y: 43 }
-0.000045 INFO A
-0.000046 INFO B(44)
-0.000047 INFO C { y: 45 }
-0.000048 INFO A
-0.000049 INFO B(Some(46))
-0.000050 INFO C { y: Ok(47) }
-0.000051 INFO A
-0.000052 INFO B(Some(48))
-0.000053 INFO C { y: 49 }
-0.000054 INFO [None, Some(42)]
-0.000055 INFO [Ok(42), Err(43)]
-0.000056 INFO [A, B(42)]
-0.000057 INFO [S { x: 42, y: None }, S { x: 43, y: Some(44) }]
-0.000058 INFO [None, Some(S { x: 42, y: 256 })]
-0.000059 INFO [None, Some([42, 43])]
-0.000060 INFO in nested 123
-0.000061 INFO after nested log: NestedStruct { a: 170, b: 305419896 }
-0.000062 INFO I can now print the @ symbol!
-0.000063 INFO @nd @lso vi@ interned strings: this is @n interned string
-0.000064 INFO empty tuple: ()
-0.000065 INFO tuple of ints: (1, 2, 3)
-0.000066 INFO nested tuple of ints: (1, 2, (3, 4, 5), (6, 7, 8))
-0.000067 INFO super nested tuples: (((((((())))))), (((((((), ())))))))
-0.000068 INFO slice of tuples: [(1, 2), (3, 4), (5, 6)]
-0.000069 INFO tuple of slices: ([1, 2, 3], [4, 5, 6])
-0.000070 INFO tuple of [u8;4]: ([1, 2, 3, 4], [5, 6, 7, 8])
-0.000071 INFO [u8;0]: []
-0.000072 INFO [u8;4]: [1, 2, 3, 4]
-0.000073 INFO [i8;4]: [-1, 2, 3, -4]
-0.000074 INFO [(u32,u32);4]: [(1, 2), (3, 4), (5, 6), (7, 8)]
-0.000075 INFO [u8;0]: []
-0.000076 INFO [u8;4]: [1, 2, 3, 4]
-0.000077 INFO [i8;4]: [-1, 2, 3, -4]
-0.000078 INFO [u32;4]: [1, 2, 3, 4]
-0.000079 INFO [i32;4]: [-1, 2, 3, -4]
-0.000080 INFO [[u32;4];4]: [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]
-0.000081 INFO [Option<u32>;4]: [Some(1), None, Some(3), None]
-0.000082 INFO [(u32,u32);4]: [(1, 2), (3, 4), (5, 6), (7, 8)]
-0.000083 INFO 1-variant enum: A { fld: 123 }
-0.000084 INFO wrapped: A(A { fld: 200 })
+0.000038 INFO usize slice: [1, 2, 3]
+0.000039 INFO isize slice: [-1, -2, -3]
+0.000040 INFO S { x: 42, y: 43 }
+0.000041 INFO S { x: 44, y: 45 }
+0.000042 INFO S { x: 46, y: Some(47) }
+0.000043 INFO S { x: Some(48), y: 49 }
+0.000044 INFO A
+0.000045 INFO B(42)
+0.000046 INFO C { y: 43 }
+0.000047 INFO A
+0.000048 INFO B(44)
+0.000049 INFO C { y: 45 }
+0.000050 INFO A
+0.000051 INFO B(Some(46))
+0.000052 INFO C { y: Ok(47) }
+0.000053 INFO A
+0.000054 INFO B(Some(48))
+0.000055 INFO C { y: 49 }
+0.000056 INFO [None, Some(42)]
+0.000057 INFO [Ok(42), Err(43)]
+0.000058 INFO [A, B(42)]
+0.000059 INFO [S { x: 42, y: None }, S { x: 43, y: Some(44) }]
+0.000060 INFO [None, Some(S { x: 42, y: 256 })]
+0.000061 INFO [None, Some([42, 43])]
+0.000062 INFO in nested 123
+0.000063 INFO after nested log: NestedStruct { a: 170, b: 305419896 }
+0.000064 INFO I can now print the @ symbol!
+0.000065 INFO @nd @lso vi@ interned strings: this is @n interned string
+0.000066 INFO empty tuple: ()
+0.000067 INFO tuple of ints: (1, 2, 3)
+0.000068 INFO nested tuple of ints: (1, 2, (3, 4, 5), (6, 7, 8))
+0.000069 INFO super nested tuples: (((((((())))))), (((((((), ())))))))
+0.000070 INFO slice of tuples: [(1, 2), (3, 4), (5, 6)]
+0.000071 INFO tuple of slices: ([1, 2, 3], [4, 5, 6])
+0.000072 INFO tuple of [u8;4]: ([1, 2, 3, 4], [5, 6, 7, 8])
+0.000073 INFO [u8;0]: []
+0.000074 INFO [u8;4]: [1, 2, 3, 4]
+0.000075 INFO [i8;4]: [-1, 2, 3, -4]
+0.000076 INFO [(u32,u32);4]: [(1, 2), (3, 4), (5, 6), (7, 8)]
+0.000077 INFO [u8;0]: []
+0.000078 INFO [u8;4]: [1, 2, 3, 4]
+0.000079 INFO [i8;4]: [-1, 2, 3, -4]
+0.000080 INFO [u32;4]: [1, 2, 3, 4]
+0.000081 INFO [i32;4]: [-1, 2, 3, -4]
+0.000082 INFO [[u32;4];4]: [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]]
+0.000083 INFO [Option<u32>;4]: [Some(1), None, Some(3), None]
+0.000084 INFO [(u32,u32);4]: [(1, 2), (3, 4), (5, 6), (7, 8)]
+0.000085 INFO 1-variant enum: A { fld: 123 }
+0.000086 INFO wrapped: A(A { fld: 200 })
diff --git a/firmware/qemu/src/bin/log.rs b/firmware/qemu/src/bin/log.rs
index 2f0f64b..8ff6cb2 100644
--- a/firmware/qemu/src/bin/log.rs
+++ b/firmware/qemu/src/bin/log.rs
@@ -151,6 +151,10 @@ fn main() -> ! {
     // issue #111
     defmt::info!("{:[?]}", [true, true, false]);
 
+    // issue #209
+    defmt::info!("usize slice: {:?}", &[1usize, 2, 3][..]);
+    defmt::info!("isize slice: {:?}", &[-1isize, -2, -3][..]);
+
     /* issue #124 (start) */
     // plain generic struct
     {
diff --git a/src/impls.rs b/src/impls.rs
index b30cd5a..e3562b6 100644
--- a/src/impls.rs
+++ b/src/impls.rs
@@ -46,8 +46,10 @@ impl Format for i64 {
 
 impl Format for isize {
     fn format(&self, fmt: &mut Formatter) {
-        let t = internp!("{:isize}");
-        fmt.u8(&t);
+        if fmt.needs_tag() {
+            let t = internp!("{:isize}");
+            fmt.u8(&t);
+        }
         fmt.isize(self);
     }
 }
@@ -94,8 +96,10 @@ impl Format for u64 {
 
 impl Format for usize {
     fn format(&self, fmt: &mut Formatter) {
-        let t = internp!("{:usize}");
-        fmt.u8(&t);
+        if fmt.needs_tag() {
+            let t = internp!("{:usize}");
+            fmt.u8(&t);
+        }
         fmt.usize(self);
     }
 }
@@ -188,7 +192,7 @@ macro_rules! arrays {
     )+ };
 }
 
-arrays!{
+arrays! {
     0 "{:[?;0]}",
     1 "{:[?;1]}",
     2 "{:[?;2]}",


Test patch:
diff --git a/tests/encode.rs b/tests/encode.rs
index bb43d19..84bbe1a 100644
--- a/tests/encode.rs
+++ b/tests/encode.rs
@@ -525,9 +525,25 @@ fn slice() {
     check_format_implementation(
         val,
         &[
-            index,           // "{:?}"
+            index,           // "{:[?]}"
             val.len() as u8, // length
-            inc(index, 1),   // "{:[?]}"
+            inc(index, 1),   // "{:u8}"
+            23,              // val[0]
+            42,              // val[1]
+        ],
+    )
+}
+
+#[test]
+fn slice_of_usize() {
+    let index = fetch_string_index();
+    let val: &[usize] = &[23usize, 42];
+    check_format_implementation(
+        val,
+        &[
+            index,           // "{:[?]}"
+            val.len() as u8, // length
+            inc(index, 1),   // "{:usize}"
             23,              // val[0]
             42,              // val[1]
         ],