@@ -370,14 +370,14 @@ package struct HTTP3StreamStateMachine: ~Copyable {
370370 }
371371
372372 enum WriteAction {
373- /// Bytes are ready to be written out .
374- case writeBytes ( ByteBuffer )
373+ /// The frame's bytes were appended to the provided buffer .
374+ case wroteBytes
375375 /// We need this header to be encoder.
376376 case encodeHeaders( [ HTTPField ] )
377377 }
378378
379- /// Write a frame out.
380- mutating func write( frame: HTTP3Frame ) -> WriteAction {
379+ /// Write a frame out by appending its encoded bytes to `buffer` .
380+ mutating func write( frame: HTTP3Frame , into buffer : inout ByteBuffer ) -> WriteAction {
381381 switch consume self. state {
382382 case . idle( let idleState) :
383383 let maybePartial = MaybePartialFrame ( frame)
@@ -394,24 +394,24 @@ package struct HTTP3StreamStateMachine: ~Copyable {
394394 // So it won't get this far.
395395 fatalError ( " Tried to write a push promise, which is not supported " )
396396 case . partial( let partial) :
397- var buffer = ByteBuffer ( )
398397 buffer. writeHTTP3PartialFrame ( partial, preferHuffmanEncoding: idleState. preferHuffmanEncoding)
399398 self = . init( state: . idle( idleState) )
400- return . writeBytes ( buffer )
399+ return . wroteBytes
401400 }
402401 case . waitingForEncode:
403402 fatalError ( " Cannot call write whilst waiting for a QPACK encode result " )
404403 }
405404 }
406405
407406 enum HeaderEncodeResultAction {
408- /// Bytes are ready to be written out .
409- case writeBytes ( ByteBuffer )
407+ /// The header's bytes were appended to the provided buffer .
408+ case wroteBytes
410409 }
411410
412411 mutating func gotHeaderEncodeResult(
413412 _ result: HTTP3PartialFrame . Headers ,
414- from: [ HTTPField ]
413+ from: [ HTTPField ] ,
414+ into buffer: inout ByteBuffer
415415 ) -> HeaderEncodeResultAction {
416416 switch consume self. state {
417417 case . idle:
@@ -420,13 +420,12 @@ package struct HTTP3StreamStateMachine: ~Copyable {
420420 guard from == waitingState. fields else {
421421 fatalError ( " Unexpected encode result " )
422422 }
423- var buffer = ByteBuffer ( )
424423 buffer. writeHTTP3PartialFrame (
425424 . headers( result) ,
426425 preferHuffmanEncoding: waitingState. preferHuffmanEncoding
427426 )
428427 self = . init( state: . idle( . init( preferHuffmanEncoding: waitingState. preferHuffmanEncoding) ) )
429- return . writeBytes ( buffer )
428+ return . wroteBytes
430429 }
431430 }
432431 }
@@ -466,8 +465,8 @@ package struct HTTP3StreamStateMachine: ~Copyable {
466465 }
467466
468467 package enum WriteFrameAction {
469- /// You should write out the following bytes to the wire .
470- case returnBytes ( ByteBuffer )
468+ /// The frame's bytes were appended to the buffer you provided .
469+ case wroteBytes
471470 /// You should encode the given headers and call back with the result.
472471 case encodeHeaders( [ HTTPField ] )
473472 /// The frame can't be written, because doing so would be a stream error.
@@ -480,8 +479,8 @@ package struct HTTP3StreamStateMachine: ~Copyable {
480479 case previousError
481480 }
482481
483- /// Write out a frame.
484- package mutating func writeFrame( frame: HTTP3Frame ) -> WriteFrameAction {
482+ /// Write out a frame by appending its encoded bytes to `buffer` .
483+ package mutating func writeFrame( frame: HTTP3Frame , into buffer : inout ByteBuffer ) -> WriteFrameAction {
485484 switch self . state {
486485 case . idle( var idleState) :
487486 guard idleState. readState. checkCanWrite ( ) else {
@@ -491,11 +490,11 @@ package struct HTTP3StreamStateMachine: ~Copyable {
491490 let validationResult = idleState. validator. processOutboundFrame ( frame)
492491 switch validationResult {
493492 case . forwardFrame( let validatedFrame) :
494- let writeAction = idleState. writeState. write ( frame: validatedFrame)
493+ let writeAction = idleState. writeState. write ( frame: validatedFrame, into : & buffer )
495494 switch writeAction {
496- case . writeBytes ( let bytes ) :
495+ case . wroteBytes :
497496 self = . init( state: . idle( idleState) )
498- return . returnBytes ( bytes )
497+ return . wroteBytes
499498 case . encodeHeaders( let fields) :
500499 self = . init( state: . idle( idleState) )
501500 return . encodeHeaders( fields)
@@ -520,8 +519,8 @@ package struct HTTP3StreamStateMachine: ~Copyable {
520519 }
521520
522521 package enum HeaderEncodeResultAction {
523- /// You should write out the following bytes to the wire .
524- case returnBytes ( ByteBuffer )
522+ /// The header's bytes were appended to the buffer you provided .
523+ case wroteBytes
525524 /// This header can't be encoded because the stream is already in an error state.
526525 case previousError( HTTP3Error )
527526 /// You should fail the current write because the stream is already closed
@@ -530,15 +529,16 @@ package struct HTTP3StreamStateMachine: ~Copyable {
530529
531530 package mutating func gotHeaderEncodeResult(
532531 _ result: HTTP3PartialFrame . Headers ,
533- from: [ HTTPField ]
532+ from: [ HTTPField ] ,
533+ into buffer: inout ByteBuffer
534534 ) -> HeaderEncodeResultAction {
535535 switch self . state {
536536 case . idle( var idleState) :
537- let writeAction = idleState. writeState. gotHeaderEncodeResult ( result, from: from)
537+ let writeAction = idleState. writeState. gotHeaderEncodeResult ( result, from: from, into : & buffer )
538538 self = . init( state: . idle( idleState) )
539539 switch writeAction {
540- case . writeBytes ( let bytes ) :
541- return . returnBytes ( bytes )
540+ case . wroteBytes :
541+ return . wroteBytes
542542 }
543543 case . finished:
544544 // We shouldn't get a header decode result on a finished stream.
0 commit comments