code: func (d *defrag) WriteTo(dst io.Writer) (written int64, err error) {
  	defer close(d.done)
  
  	// Early exit if previously canceled.
  	if d.cancellation != nil {
  		return d.written, d.cancellation
  	}
  
  	for {
  		// Exit if all fragments have finished.
  		//
  		// A fragment ordinal of zero is indeterminate and is
  		// specifically excluded from being able to satisfy
  		// that criteria.
  		if d.lastWritten >= d.lastOrdinal && d.lastOrdinal > 0 {
  			break
  		}
  
  		select {
  		case frag := <-d.registerNotify:
  			// Got fragment.
  			//
  			// Figure out whether to write it now or not.
  			next := d.lastWritten + 1
  			if frag.ord == next {
  				// This fragment completes the next
  				// contiguous swathe of bytes to be
  				// written out.
  				n, err := d.writeConsecutive(dst, frag)
  				d.written += n
  				if err != nil {
  					return d.written, err
  				}
  			} else if frag.ord > next {
  				// Got a fragment that can't be
  				// emitted yet: store it for now.
  				d.future[frag.ord] = frag
  			} else {
  				return d.written, assertErrf(
  					"Unexpected retrograde fragment %v, "+
  						"expected at least %v",
  					frag.ord, next)
  			}
  
  		case d.cancellation = <-d.cancelNotify:
  			// Cancel and exit immediately with the
  			// injected cancellation.
  			d.future = nil
  			return d.written, d.cancellation
  
  		case d.lastOrdinal = <-d.lastOrdinalNotify:
  			// Re-check the exit conditions as a
  			// lastOrdinal has been set.
  			continue
  		}
  	}
  
  	return d.written, nil
  }
docs: // Write the contents of the defragmenter out to the io.Writer dst.