pub fn write(&mut self, buf: RangeBuf) -> Result<()> {
        if buf.max_off() > self.max_data() {
            return Err(Error::FlowControl);
        }

        if let Some(fin_off) = self.fin_off {
            // Stream's size is known, forbid data beyond that point.
            if buf.max_off() > fin_off {
                return Err(Error::FinalSize);
            }

            // Stream's size is already known, forbid changing it.
            if buf.fin() && fin_off != buf.max_off() {
                return Err(Error::FinalSize);
            }
        }

        // Stream's known size is lower than data already received.
        if buf.fin() && buf.max_off() < self.len {
            return Err(Error::FinalSize);
        }

        // We already saved the final offset, so there's nothing else we
        // need to keep from the RangeBuf if it's empty.
        if self.fin_off.is_some() && buf.is_empty() {
            return Ok(());
        }

        // No need to process an empty buffer with the fin flag, if we already
        // know the final size.
        if buf.fin() && buf.is_empty() && self.fin_off.is_some() {
            return Ok(());
        }

        if buf.fin() {
            self.fin_off = Some(buf.max_off());
        }

        // No need to store empty buffer that doesn't carry the fin flag.
        if !buf.fin() && buf.is_empty() {
            return Ok(());
        }

        // Check if data is fully duplicate, that is the buffer's max offset is
        // lower or equal to the offset already stored in the recv buffer.
        if self.off >= buf.max_off() {
            // An exception is applied to empty range buffers, because an empty
            // buffer's max offset matches the max offset of the recv buffer.
            //
            // By this point all spurious empty buffers should have already been
            // discarded, so allowing empty buffers here should be safe.
            if !buf.is_empty() {
                return Ok(());
            }
        }

        let mut tmp_bufs = VecDeque::with_capacity(2);
        tmp_bufs.push_back(buf);

        'tmp: while let Some(mut buf) = tmp_bufs.pop_front() {
            // Discard incoming data below current stream offset. Bytes up to
            // `self.off` have already been received so we should not buffer
            // them again. This is also important to make sure `ready()` doesn't
            // get stuck when a buffer with lower offset than the stream's is
            // buffered.
            if self.off_front() > buf.off() {
                buf = buf.split_off((self.off_front() - buf.off()) as usize);
            }

            // Handle overlapping data. If the incoming data's starting offset
            // is above the previous maximum received offset, there is clearly
            // no overlap so this logic can be skipped. However do still try to
            // merge an empty final buffer (i.e. an empty buffer with the fin
            // flag set, which is the only kind of empty buffer that should
            // reach this point).
            if buf.off() < self.max_off() || buf.is_empty() {
                for b in &self.data {
                    // New buffer is fully contained in existing buffer.
                    if buf.off() >= b.off() && buf.max_off() <= b.max_off() {
                        continue 'tmp;
                    }

                    // New buffer's start overlaps existing buffer.
                    if buf.off() >= b.off() && buf.off() < b.max_off() {
                        buf = buf.split_off((b.max_off() - buf.off()) as usize);
                    }

                    // New buffer's end overlaps existing buffer.
                    if buf.off() < b.off() && buf.max_off() > b.off() {
                        tmp_bufs.push_back(
                            buf.split_off((b.off() - buf.off()) as usize),
                        );
                    }
                }
            }

            self.len = cmp::max(self.len, buf.max_off());

            if !self.drain {
                self.data.push(buf);
            }
        }

        Ok(())
    }
/// Inserts the given chunk of data in the buffer.
///
/// This also takes care of enforcing stream flow control limits, as well
/// as handling incoming data that overlaps data that is already in the
/// buffer.
Inserts the given chunk of data in the buffer.
This also takes care of enforcing stream flow control limits, as well
as handling incoming data that overlaps data that is already in the
buffer.
Inserts the given chunk of data in the buffer . This also takes care of enforcing stream flow control limits as well as handling incoming data that overlaps data that is already in the buffer .
pub fn write(&mut self, buf: RangeBuf) -> Result<()> {
        if buf.max_off() > self.max_data() {
            return Err(Error::FlowControl);
        }
        if let Some(fin_off) = self.fin_off {
            // Stream's size is known, forbid data beyond that point.
            if buf.max_off() > fin_off {
                return Err(Error::FinalSize);
            }
            // Stream's size is already known, forbid changing it.
            if buf.fin() && fin_off != buf.max_off() {
                return Err(Error::FinalSize);
            }
        }
        // Stream's known size is lower than data already received.
        if buf.fin() && buf.max_off() < self.len {
            return Err(Error::FinalSize);
        }
        // We already saved the final offset, so there's nothing else we
        // need to keep from the RangeBuf if it's empty.
        if self.fin_off.is_some() && buf.is_empty() {
            return Ok(());
        }
        // No need to process an empty buffer with the fin flag, if we already
        // know the final size.
        if buf.fin() && buf.is_empty() && self.fin_off.is_some() {
            return Ok(());
        }
        if buf.fin() {
            self.fin_off = Some(buf.max_off());
        }
        // No need to store empty buffer that doesn't carry the fin flag.
        if !buf.fin() && buf.is_empty() {
            return Ok(());
        }
        // Check if data is fully duplicate, that is the buffer's max offset is
        // lower or equal to the offset already stored in the recv buffer.
        if self.off >= buf.max_off() {
            // An exception is applied to empty range buffers, because an empty
            // buffer's max offset matches the max offset of the recv buffer.
            //
            // By this point all spurious empty buffers should have already been
            // discarded, so allowing empty buffers here should be safe.
            if !buf.is_empty() {
                return Ok(());
            }
        }
        let mut tmp_bufs = VecDeque::with_capacity(2);
        tmp_bufs.push_back(buf);
        'tmp: while let Some(mut buf) = tmp_bufs.pop_front() {
            // Discard incoming data below current stream offset. Bytes up to
            // `self.off` have already been received so we should not buffer
            // them again. This is also important to make sure `ready()` doesn't
            // get stuck when a buffer with lower offset than the stream's is
            // buffered.
            if self.off_front() > buf.off() {
                buf = buf.split_off((self.off_front() - buf.off()) as usize);
            }
            // Handle overlapping data. If the incoming data's starting offset
            // is above the previous maximum received offset, there is clearly
            // no overlap so this logic can be skipped. However do still try to
            // merge an empty final buffer (i.e. an empty buffer with the fin
            // flag set, which is the only kind of empty buffer that should
            // reach this point).
            if buf.off() < self.max_off() || buf.is_empty() {
                for b in &self.data {
                    // New buffer is fully contained in existing buffer.
                    if buf.off() >= b.off() && buf.max_off() <= b.max_off() {
                        continue 'tmp;
                    }
                    // New buffer's start overlaps existing buffer.
                    if buf.off() >= b.off() && buf.off() < b.max_off() {
                        buf = buf.split_off((b.max_off() - buf.off()) as usize);
                    }
                    // New buffer's end overlaps existing buffer.
                    if buf.off() < b.off() && buf.max_off() > b.off() {
                        tmp_bufs.push_back(
                            buf.split_off((b.off() - buf.off()) as usize),
                        );
                    }
                }
            }
            self.len = cmp::max(self.len, buf.max_off());
            if !self.drain {
                self.data.push(buf);
            }
        }
        Ok(())
    }
pub fn write ( & mut self , buf : RangeBuf ) -> Result < ( ) > { if buf . max_off ( ) > self . max_data ( ) { return Err ( Error :: FlowControl ) ; } if let Some ( fin_off ) = self . fin_off { // Stream's size is known, forbid data beyond that point. if buf . max_off ( ) > fin_off { return Err ( Error :: FinalSize ) ; } // Stream's size is already known, forbid changing it. if buf . fin ( ) && fin_off != buf . max_off ( ) { return Err ( Error :: FinalSize ) ; } } // Stream's known size is lower than data already received. if buf . fin ( ) && buf . max_off ( ) < self . len { return Err ( Error :: FinalSize ) ; } // We already saved the final offset, so there's nothing else we // need to keep from the RangeBuf if it's empty. if self . fin_off . is_some ( ) && buf . is_empty ( ) { return Ok ( ( ) ) ; } // No need to process an empty buffer with the fin flag, if we already // know the final size. if buf . fin ( ) && buf . is_empty ( ) && self . fin_off . is_some ( ) { return Ok ( ( ) ) ; } if buf . fin ( ) { self . fin_off = Some ( buf . max_off ( ) ) ; } // No need to store empty buffer that doesn't carry the fin flag. if ! buf . fin ( ) && buf . is_empty ( ) { return Ok ( ( ) ) ; } // Check if data is fully duplicate, that is the buffer's max offset is // lower or equal to the offset already stored in the recv buffer. if self . off >= buf . max_off ( ) { // An exception is applied to empty range buffers, because an empty // buffer's max offset matches the max offset of the recv buffer. // // By this point all spurious empty buffers should have already been // discarded, so allowing empty buffers here should be safe. if ! buf . is_empty ( ) { return Ok ( ( ) ) ; } } let mut tmp_bufs = VecDeque :: with_capacity ( 2 ) ; tmp_bufs . push_back ( buf ) ; ' tmp : while let Some ( mut buf ) = tmp_bufs . pop_front ( ) { // Discard incoming data below current stream offset. Bytes up to // `self.off` have already been received so we should not buffer // them again. This is also important to make sure `ready()` doesn't // get stuck when a buffer with lower offset than the stream's is // buffered. if self . off_front ( ) > buf . off ( ) { buf = buf . split_off ( ( self . off_front ( ) - buf . off ( ) ) as usize ) ; } // Handle overlapping data. If the incoming data's starting offset // is above the previous maximum received offset, there is clearly // no overlap so this logic can be skipped. However do still try to // merge an empty final buffer (i.e. an empty buffer with the fin // flag set, which is the only kind of empty buffer that should // reach this point). if buf . off ( ) < self . max_off ( ) || buf . is_empty ( ) { for b in & self . data { // New buffer is fully contained in existing buffer. if buf . off ( ) >= b . off ( ) && buf . max_off ( ) <= b . max_off ( ) { continue ' tmp ; } // New buffer's start overlaps existing buffer. if buf . off ( ) >= b . off ( ) && buf . off ( ) < b . max_off ( ) { buf = buf . split_off ( ( b . max_off ( ) - buf . off ( ) ) as usize ) ; } // New buffer's end overlaps existing buffer. if buf . off ( ) < b . off ( ) && buf . max_off ( ) > b . off ( ) { tmp_bufs . push_back ( buf . split_off ( ( b . off ( ) - buf . off ( ) ) as usize ) , ) ; } } } self . len = cmp :: max ( self . len , buf . max_off ( ) ) ; if ! self . drain { self . data . push ( buf ) ; } } Ok ( ( ) ) }
Inserts the given chunk of data in the buffer.
Inserts the given chunk of data in the buffer .
&mut self
buf
RangeBuf
&mut self
buf
RangeBuf