@@ -90,61 +90,30 @@ impl Fragmenter {
9090 /// In `SendOutput::Packet(buf)`, `out` is borrowed as the returned fragment, filled with
9191 /// packet contents.
9292 ///
93+ /// Calls to `fragment_vectored()` and `fragment()` should not be mixed.
94+ /// (If you do, the vector has to hold exactly one buffer that is
95+ /// equal to the one passed to `fragment()`.)
96+ ///
9397 /// `out` must be at least as large as the specified `mtu`.
9498 pub fn fragment < ' f > (
9599 & mut self ,
96100 payload : & [ u8 ] ,
97101 out : & ' f mut [ u8 ] ,
98102 ) -> SendOutput < ' f > {
99- if self . header . eom {
100- return SendOutput :: success ( self ) ;
101- }
102-
103- // Require at least MTU buffer size, to ensure that all non-end
104- // fragments are the same size per the spec.
105- if out. len ( ) < self . mtu {
106- debug ! ( "small out buffer" ) ;
107- return SendOutput :: failure ( Error :: BadArgument , self ) ;
108- }
109-
110- // Reserve header space, the remaining buffer keeps being
111- // updated in `rest`
112- let max_total = out. len ( ) . min ( self . mtu ) ;
113- let ( h, mut rest) = out[ ..max_total] . split_at_mut ( MctpHeader :: LEN ) ;
114-
115- // Append type byte
116- if self . header . som {
117- rest[ 0 ] = mctp:: encode_type_ic ( self . typ , self . ic ) ;
118- rest = & mut rest[ 1 ..] ;
119- }
120-
121- if payload. len ( ) < self . payload_used {
122- // Caller is passing varying payload buffers
123- debug ! ( "varying payload" ) ;
124- return SendOutput :: failure ( Error :: BadArgument , self ) ;
125- }
126-
127- // Copy as much as is available in input or output
128- let p = & payload[ self . payload_used ..] ;
129- let l = p. len ( ) . min ( rest. len ( ) ) ;
130- let ( d, rest) = rest. split_at_mut ( l) ;
131- self . payload_used += l;
132- d. copy_from_slice ( & p[ ..l] ) ;
133-
134- // Add the header
135- if self . payload_used == payload. len ( ) {
136- self . header . eom = true ;
137- }
138- // OK unwrap: seq and tag are valid.
139- h. copy_from_slice ( & self . header . encode ( ) . unwrap ( ) ) ;
140-
141- self . header . som = false ;
142- self . header . seq = ( self . header . seq + 1 ) & mctp:: MCTP_SEQ_MASK ;
143-
144- let used = max_total - rest. len ( ) ;
145- SendOutput :: Packet ( & mut out[ ..used] )
103+ self . fragment_vectored ( & [ payload] , out)
146104 }
147105
106+ /// Returns fragments for the MCTP payload
107+ ///
108+ /// The same input message `payload` should be passed to each `fragment_vectored()` call.
109+ /// In `SendOutput::Packet(buf)`, `out` is borrowed as the returned fragment, filled with
110+ /// packet contents.
111+ ///
112+ /// Calls to `fragment_vectored()` and `fragment()` should not be mixed.
113+ /// (If you do, the vector has to hold exactly one buffer that is
114+ /// equal to the one passed to `fragment()`.)
115+ ///
116+ /// `out` must be at least as large as the specified `mtu`.
148117 pub fn fragment_vectored < ' f > (
149118 & mut self ,
150119 payload : & [ & [ u8 ] ] ,
0 commit comments