pub fn append(&mut self, data: &[u8]) -> Result<bool, PingpongBufferError> {
        // get the active buffer
        let buff = if self.active_toggle {
            &mut self.buffer_a
        } else {
            &mut self.buffer_b
        };

        // determine how many bytes we can append to the buffer
        let capacity = buff.len() - self.active_index;

        // iterate through the data, to add it to our buffer at the index
        let start = self.active_index;
        for i in 0..data.len() {
            if i >= capacity {
                break; // cannot fill anymore into this buffer
            }
            buff[start + i] = data[i];
            self.active_index += 1;
        }

        // number of bytes appended to the buffer
        let transferred = if capacity > data.len() {
            data.len()
        } else {
            capacity
        };

        // We are at the end of the buffer
        if self.active_index == buff.len() {
            if self.is_reserve_full {
                // We are attempting to switch the reserve->active buffer,
                // but the reserve buffer is still full of data that has not be read
                return Err(PingpongBufferError::ReserveFull);
            }
            // Toggle that the reserve buffer is full, and can be read
            self.is_reserve_full = true;
            // Reset the active index for writing
            self.active_index = 0;
            // Toggle the buffer
            self.active_toggle = !self.active_toggle;
            // There is left over data that needs to be transferred
            if transferred != data.len() {
                let buff = if self.active_toggle {
                    &mut self.buffer_a
                } else {
                    &mut self.buffer_b
                };

                // copy the remainder into the other buffer
                let remainder = data.len() - transferred;
                if remainder > N {
                    return Result::Err(PingpongBufferError::Overflow);
                }
                for i in 0..remainder {
                    buff[i] = data[transferred + i];
                    self.active_index += 1;
                }
            }
            // The buffers have been toggled
            return Result::Ok(true);
        }
        // The buffers have not been toggled
        return Result::Ok(false);
    }
/// Append data to the active buffer
/// If the active buffer fills to maximum capacity, then the active and reserve buffers
/// are switched, allowing the remainding data to be written to the reserve (now active) buffer
/// This switch can only happen if the data in the reserve buffer has been successfully read
Append data to the active buffer
If the active buffer fills to maximum capacity, then the active and reserve buffers
are switched, allowing the remainding data to be written to the reserve (now active) buffer
This switch can only happen if the data in the reserve buffer has been successfully read
Append data to the active buffer If the active buffer fills to maximum capacity then the active and reserve buffers are switched allowing the remainding data to be written to the reserve ( now active ) buffer This switch can only happen if the data in the reserve buffer has been successfully read
pub fn append(&mut self, data: &[u8]) -> Result<bool, PingpongBufferError> {
        // get the active buffer
        let buff = if self.active_toggle {
            &mut self.buffer_a
        } else {
            &mut self.buffer_b
        };
        // determine how many bytes we can append to the buffer
        let capacity = buff.len() - self.active_index;
        // iterate through the data, to add it to our buffer at the index
        let start = self.active_index;
        for i in 0..data.len() {
            if i >= capacity {
                break; // cannot fill anymore into this buffer
            }
            buff[start + i] = data[i];
            self.active_index += 1;
        }
        // number of bytes appended to the buffer
        let transferred = if capacity > data.len() {
            data.len()
        } else {
            capacity
        };
        // We are at the end of the buffer
        if self.active_index == buff.len() {
            if self.is_reserve_full {
                // We are attempting to switch the reserve->active buffer,
                // but the reserve buffer is still full of data that has not be read
                return Err(PingpongBufferError::ReserveFull);
            }
            // Toggle that the reserve buffer is full, and can be read
            self.is_reserve_full = true;
            // Reset the active index for writing
            self.active_index = 0;
            // Toggle the buffer
            self.active_toggle = !self.active_toggle;
            // There is left over data that needs to be transferred
            if transferred != data.len() {
                let buff = if self.active_toggle {
                    &mut self.buffer_a
                } else {
                    &mut self.buffer_b
                };
                // copy the remainder into the other buffer
                let remainder = data.len() - transferred;
                if remainder > N {
                    return Result::Err(PingpongBufferError::Overflow);
                }
                for i in 0..remainder {
                    buff[i] = data[transferred + i];
                    self.active_index += 1;
                }
            }
            // The buffers have been toggled
            return Result::Ok(true);
        }
        // The buffers have not been toggled
        return Result::Ok(false);
    }
pub fn append ( & mut self , data : & [ u8 ] ) -> Result < bool , PingpongBufferError > { // get the active buffer let buff = if self . active_toggle { & mut self . buffer_a } else { & mut self . buffer_b } ; // determine how many bytes we can append to the buffer let capacity = buff . len ( ) - self . active_index ; // iterate through the data, to add it to our buffer at the index let start = self . active_index ; for i in 0 .. data . len ( ) { if i >= capacity { break ; // cannot fill anymore into this buffer } buff [ start + i ] = data [ i ] ; self . active_index += 1 ; } // number of bytes appended to the buffer let transferred = if capacity > data . len ( ) { data . len ( ) } else { capacity } ; // We are at the end of the buffer if self . active_index == buff . len ( ) { if self . is_reserve_full { // We are attempting to switch the reserve->active buffer, // but the reserve buffer is still full of data that has not be read return Err ( PingpongBufferError :: ReserveFull ) ; } // Toggle that the reserve buffer is full, and can be read self . is_reserve_full = true ; // Reset the active index for writing self . active_index = 0 ; // Toggle the buffer self . active_toggle = ! self . active_toggle ; // There is left over data that needs to be transferred if transferred != data . len ( ) { let buff = if self . active_toggle { & mut self . buffer_a } else { & mut self . buffer_b } ; // copy the remainder into the other buffer let remainder = data . len ( ) - transferred ; if remainder > N { return Result :: Err ( PingpongBufferError :: Overflow ) ; } for i in 0 .. remainder { buff [ i ] = data [ transferred + i ] ; self . active_index += 1 ; } } // The buffers have been toggled return Result :: Ok ( true ) ; } // The buffers have not been toggled return Result :: Ok ( false ) ; }
Append data to the active buffer
If the active buffer fills to maximum capacity, then the active and reserve buffers
are switched, allowing the remainding data to be written to the reserve (now active) buffer
This switch can only happen if the data in the reserve buffer has been successfully read
Append data to the active buffer If the active buffer fills to maximum capacity then the active and reserve buffers are switched allowing the remainding data to be written to the reserve ( now active ) buffer This switch can only happen if the data in the reserve buffer has been successfully read
&mut self
data
&[u8]
&mut self
data
&[u8]