Trait protobuf_native::io::ZeroCopyInputStream

source ·
pub trait ZeroCopyInputStream: Sealed {
    // Provided methods
    fn next(self: Pin<&mut Self>) -> Result<&[u8], OperationFailedError> { ... }
    fn back_up(self: Pin<&mut Self>, count: usize) { ... }
    fn skip(
        self: Pin<&mut Self>,
        count: usize
    ) -> Result<(), OperationFailedError> { ... }
    fn byte_count(&self) -> i64 { ... }
}
Expand description

Abstract interface similar to an input stream but designed to minimize copying.

§Examples

Read in a file and print its contents to stdout:

use std::fs::File;
use std::io::{self, Write};
use protobuf_native::io::{ReaderStream, ZeroCopyInputStream};

let mut f = File::open("myfile")?;
let mut input = ReaderStream::new(&mut f);
while let Ok(buf) = input.as_mut().next() {
    io::stdout().write_all(buf)?;
}

Provided Methods§

source

fn next(self: Pin<&mut Self>) -> Result<&[u8], OperationFailedError>

Obtains a chunk of data from the stream.

If the function returns an error, either there is no more data to return or an I/O error occurred. All errors are permanent.

It is legal for the returned buffer to have zero size, as long as repeatedly calling next eventually yields a buffer with non-zero size.

source

fn back_up(self: Pin<&mut Self>, count: usize)

Backs up a number of bytes, so that the next call to next returns data again that was already returned by the last call to next.

This is useful when writing procedures that are only supposed to read up to a certain point in the input, then return. If next returns a buffer that goes beyond what you wanted to read, you can use back_up to return to the point where you intended to finish.

The last method called must have been next. The count parameter must be less than or equal to the size of the last buffer returned by next.

source

fn skip(self: Pin<&mut Self>, count: usize) -> Result<(), OperationFailedError>

Skips count bytes.

Returns an error if the end of stream is reached or an I/O error occurred. In the end-of-stream case, the stream is advanced to its end, so byte_count will return the total size of the stream.

source

fn byte_count(&self) -> i64

Returns the total number of bytes read since this stream was created.

Implementors§