Skip to content

Commit a1cbefc

Browse files
committed
Update example with MAX_SIZE
1 parent 91476b6 commit a1cbefc

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ fn main() {
8686
..Default::default()
8787
};
8888
89+
// Maximum size of the message type on the wire, scaled to the next power of 2 due to heapless::Vec
90+
const CAPACITY: usize = example::Example::MAX_SIZE.unwrap().next_power_of_two();
91+
// For the example message above we can use a smaller capacity
92+
// const CAPACITY: usize = 32;
93+
8994
// Use heapless::Vec as the output stream and build an encoder around it
90-
let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, 32>::new());
95+
let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, CAPACITY>::new());
9196
9297
// Compute the size of the `Example` on the wire
9398
let size = example.compute_size();
@@ -420,7 +425,7 @@ use micropb::{PbEncoder, PbWrite, MessageEncode};
420425
use micropb::heapless::Vec;
421426
422427
// Use heapless::Vec as the output stream and build an encoder around it
423-
let mut encoder = PbEncoder::new(Vec::<u8, 10>::new());
428+
let mut encoder = PbEncoder::new(Vec::<u8, 16>::new());
424429
425430
// ProtoMessage was generated by micropb
426431
let mut message = ProtoMessage::default();

examples/basic/src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ fn main() {
1616
..Default::default()
1717
};
1818

19+
// Maximum size of the message type on the wire, scaled to the next power of 2 due to heapless::Vec
20+
const CAPACITY: usize = example::Example::MAX_SIZE.unwrap().next_power_of_two();
21+
// For the example message above we can use a smaller capacity
22+
// const CAPACITY: usize = 32;
23+
1924
// Use heapless::Vec as the output stream and build an encoder around it
20-
let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, 32>::new());
25+
let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, CAPACITY>::new());
2126

2227
// Compute the size of the `Example` on the wire
2328
let _size = example.compute_size();

micropb/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ fn main() {
8686
..Default::default()
8787
};
8888
89+
// Maximum size of the message type on the wire, scaled to the next power of 2 due to heapless::Vec
90+
const CAPACITY: usize = example::Example::MAX_SIZE.unwrap().next_power_of_two();
91+
// For the example message above we can use a smaller capacity
92+
// const CAPACITY: usize = 32;
93+
8994
// Use heapless::Vec as the output stream and build an encoder around it
90-
let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, 32>::new());
95+
let mut encoder = PbEncoder::new(micropb::heapless::Vec::<u8, CAPACITY>::new());
9196
9297
// Compute the size of the `Example` on the wire
9398
let size = example.compute_size();
@@ -420,7 +425,7 @@ use micropb::{PbEncoder, PbWrite, MessageEncode};
420425
use micropb::heapless::Vec;
421426
422427
// Use heapless::Vec as the output stream and build an encoder around it
423-
let mut encoder = PbEncoder::new(Vec::<u8, 10>::new());
428+
let mut encoder = PbEncoder::new(Vec::<u8, 16>::new());
424429
425430
// ProtoMessage was generated by micropb
426431
let mut message = ProtoMessage::default();

0 commit comments

Comments
 (0)