@@ -41,8 +41,7 @@ pub struct SetMorseBulkRequest {
4141
4242#[ cfg( feature = "bulk" ) ]
4343impl MaxSize for SetMorseBulkRequest {
44- const POSTCARD_MAX_SIZE : usize =
45- u8:: POSTCARD_MAX_SIZE + <Morse >:: POSTCARD_MAX_SIZE * BULK_SIZE + crate :: varint_max_size ( BULK_SIZE ) ;
44+ const POSTCARD_MAX_SIZE : usize = u8:: POSTCARD_MAX_SIZE + crate :: heapless_vec_max_size :: < Morse , BULK_SIZE > ( ) ;
4645}
4746
4847/// Bulk response for getting multiple morse configs at once.
@@ -54,15 +53,41 @@ pub struct GetMorseBulkResponse {
5453
5554#[ cfg( feature = "bulk" ) ]
5655impl MaxSize for GetMorseBulkResponse {
57- const POSTCARD_MAX_SIZE : usize = < Morse > :: POSTCARD_MAX_SIZE * BULK_SIZE + crate :: varint_max_size ( BULK_SIZE ) ;
56+ const POSTCARD_MAX_SIZE : usize = crate :: heapless_vec_max_size :: < Morse , BULK_SIZE > ( ) ;
5857}
5958
6059#[ cfg( test) ]
6160mod tests {
6261 use super :: * ;
6362 use crate :: action:: Action ;
63+ use crate :: constants:: MORSE_SIZE ;
64+ use crate :: keycode:: { HidKeyCode , KeyCode } ;
65+ use crate :: modifier:: ModifierCombination ;
6466 use crate :: morse:: { MorsePattern , MorseProfile } ;
65- use crate :: protocol:: rmk:: test_utils:: round_trip;
67+ use crate :: protocol:: rmk:: test_utils:: { assert_max_size_bound, round_trip} ;
68+
69+ /// Build a `Morse` whose `actions` `LinearMap` is filled to `MORSE_SIZE`
70+ /// distinct entries, each using a multi-field `Action` variant so both the
71+ /// entry count *and* the per-entry encoded size meaningfully exercise the
72+ /// manual `MaxSize` impl. `MorsePattern::from_u16(0)` panics (the empty
73+ /// pattern is `0b1`), so patterns start at 1.
74+ fn full_morse ( ) -> Morse {
75+ // `KeyWithModifier` carries a nested `KeyCode` enum + a `ModifierCombination`
76+ // bitfield, so it encodes to several bytes rather than the 1 byte of
77+ // `Action::No` — enough slack for `assert_max_size_bound` to catch a
78+ // per-element under-count.
79+ let action = Action :: KeyWithModifier ( KeyCode :: Hid ( HidKeyCode :: A ) , ModifierCombination :: new ( ) ) ;
80+ let mut m = Morse {
81+ profile : MorseProfile :: const_default ( ) ,
82+ actions : heapless:: LinearMap :: new ( ) ,
83+ } ;
84+ for i in 0 ..MORSE_SIZE {
85+ m. actions
86+ . insert ( MorsePattern :: from_u16 ( ( i + 1 ) as u16 ) , action)
87+ . unwrap ( ) ;
88+ }
89+ m
90+ }
6691
6792 #[ test]
6893 fn round_trip_morse ( ) {
@@ -84,4 +109,39 @@ mod tests {
84109 config : morse,
85110 } ) ;
86111 }
112+
113+ #[ test]
114+ fn round_trip_morse_max_capacity ( ) {
115+ let m = full_morse ( ) ;
116+ assert_eq ! ( m. actions. len( ) , MORSE_SIZE ) ;
117+ round_trip ( & m) ;
118+ assert_max_size_bound ( & m) ;
119+ }
120+
121+ #[ cfg( feature = "bulk" ) ]
122+ #[ test]
123+ fn round_trip_set_morse_bulk_request_max_capacity ( ) {
124+ let mut configs: Vec < Morse , BULK_SIZE > = Vec :: new ( ) ;
125+ for _ in 0 ..BULK_SIZE {
126+ configs. push ( full_morse ( ) ) . unwrap ( ) ;
127+ }
128+ let req = SetMorseBulkRequest {
129+ start_index : u8:: MAX ,
130+ configs,
131+ } ;
132+ round_trip ( & req) ;
133+ assert_max_size_bound ( & req) ;
134+ }
135+
136+ #[ cfg( feature = "bulk" ) ]
137+ #[ test]
138+ fn round_trip_get_morse_bulk_response_max_capacity ( ) {
139+ let mut configs: Vec < Morse , BULK_SIZE > = Vec :: new ( ) ;
140+ for _ in 0 ..BULK_SIZE {
141+ configs. push ( full_morse ( ) ) . unwrap ( ) ;
142+ }
143+ let resp = GetMorseBulkResponse { configs } ;
144+ round_trip ( & resp) ;
145+ assert_max_size_bound ( & resp) ;
146+ }
87147}
0 commit comments