Trait ion_rs::IonDataSource
source · pub trait IonDataSource: BufRead {
// Provided methods
fn skip_bytes(&mut self, number_of_bytes: usize) -> IonResult<()> { ... }
fn next_byte(&mut self) -> IonResult<Option<u8>> { ... }
fn read_next_byte_while<F>(
&mut self,
byte_processor: &mut F
) -> IonResult<usize>
where F: FnMut(u8) -> bool { ... }
fn read_slice<T, F>(
&mut self,
length: usize,
fallback_buffer: &mut Vec<u8>,
slice_processor: F
) -> IonResult<T>
where F: FnOnce(&[u8]) -> IonResult<T> { ... }
}
Expand description
Optimized read operations for parsing Ion.
The binary Ion spec calls for a number of reading patterns, including:
- Type descriptor octets (value headers) require that a single byte be read from input.
- Variable length integers (both signed and unsigned) require that a single byte at a time be read from the data source until some condition is met.
- Fixed length values require that
n
bytes be read from the data source and interpreted as a single value. - Skipping over values, partial or whole, requires that the next
n
bytes of the data source be ignored altogether.
The IonDataSource trait extends functionality offered by the BufRead trait by providing methods that are tailored to these use cases. They have been optimized to prefer operating on data that’s already in the input buffer in-place rather than copying it out to another byte array.
Provided Methods§
sourcefn skip_bytes(&mut self, number_of_bytes: usize) -> IonResult<()>
fn skip_bytes(&mut self, number_of_bytes: usize) -> IonResult<()>
Ignore the next number_of_bytes
bytes in the data source.
sourcefn next_byte(&mut self) -> IonResult<Option<u8>>
fn next_byte(&mut self) -> IonResult<Option<u8>>
Returns the next byte in the data source if available.
sourcefn read_next_byte_while<F>(
&mut self,
byte_processor: &mut F
) -> IonResult<usize>where
F: FnMut(u8) -> bool,
fn read_next_byte_while<F>( &mut self, byte_processor: &mut F ) -> IonResult<usize>where F: FnMut(u8) -> bool,
Calls byte_processor
on each byte in the data source until it returns false.
Returns the number of bytes that were read and processed.
sourcefn read_slice<T, F>(
&mut self,
length: usize,
fallback_buffer: &mut Vec<u8>,
slice_processor: F
) -> IonResult<T>where
F: FnOnce(&[u8]) -> IonResult<T>,
fn read_slice<T, F>( &mut self, length: usize, fallback_buffer: &mut Vec<u8>, slice_processor: F ) -> IonResult<T>where F: FnOnce(&[u8]) -> IonResult<T>,
Calls slice_processor
on a slice containing the next length
bytes from the
data source. If the required bytes are already in the input buffer, a reference to that
slice of the input buffer will be used. If they are not, the required bytes will be read
into fallback_buffer
and that will be used instead. If fallback_buffer
does not have
enough capacity to store the requested data, it will be resized. It will never be shrunk,
however–it is the caller’s responsibility to manage this memory.