Skip to main content

Reader

Struct Reader 

Source
pub struct Reader<R, D: StreamDeserializer> { /* private fields */ }
Available on crate feature io only.
Expand description

Reads values from a Read.

The values are split and deserialized with a StreamDeserializer (for instance deser_json::StreamDeserializer, which the configuration of the format creates with config.reader(input)). The reader buffers the input so it does not need to be buffered.

Readers implement Deserializer: every call to drive reads the next value. Values read this way cannot borrow from the reader, use read_borrowed for that.

Implementations§

Source§

impl<R: Read, D: StreamDeserializer> Reader<R, D>

Source

pub fn new(reader: R, deserializer: D) -> Reader<R, D>

Creates a reader.

To continue a stream whose context is known (for instance the names of the columns of a CSV file), create the stream deserializer with that context.

Source

pub fn set_context(&mut self, context: Context)

Sets the context the values are deserialized in.

This replaces the context of the stream deserializer (see StreamDeserializer::context), which is the one of the configuration it was created with. The values of the context are the defaults of the extension values of the state (see Context). A context set by the callback of read_with takes precedence.

Source

pub fn context(&self) -> &Context

Returns the context the values are deserialized in.

Source

pub fn read<T: DeserializeOwned>(&mut self) -> Result<Option<T>, Error>

Reads the next value.

Returns None if there are no more values. If the format supports it (see StreamDeserializer::supports_partial), the value is deserialized while the input is read which means that only incomplete tokens are buffered. Otherwise the complete value is buffered first. Whether reading can continue after an error depends on the format (for instance with JSON Lines it continues with the next line).

Source

pub fn read_with<T, F>(&mut self, setup: F) -> Result<Option<T>, Error>
where T: DeserializeOwned, F: FnOnce(&mut DeserializeDriver<'_, '_>),

Reads the next value with a configured driver.

The callback is invoked with the driver before the value is deserialized, for instance to add Layers.

Source

pub fn read_borrowed<'a, T: Deserialize<'a>>( &'a mut self, ) -> Result<Option<T>, Error>

Reads the next value which can borrow from the reader’s buffer.

The complete value is buffered first.

use deser::io::Reader;

// `Lines` is the stream deserializer of a format with a string per
// line
let mut reader = Reader::new(&b"hello\nworld\n"[..], Lines);
let value: &str = reader.read_borrowed().unwrap().unwrap();
assert_eq!(value, "hello");
Source

pub fn read_next<T, E>(&mut self) -> Result<Option<Part<E, T>>, Error>
where T: DeserializeOwned + 'static, E: Send + 'static,

Reads the next element of the Streamed sequence of a value or the value.

T is the type of the value and E the type of the elements of a Streamed<E> sequence within it. The elements are handed out as they are read (Part::Element), the value once it’s complete (Part::Done). The next call continues with the next value. Returns None if there are no more values. See Streamed for an example.

Until the value is complete, the reader can only be used to read the value with the same types.

Source

pub fn is_end(&mut self) -> Result<bool, Error>

Returns true if there are no more values.

This reads until the start of the next value or the end of the stream. If the format cannot find the start of a value on its own (see StreamDeserializer::peek), the next value is buffered completely. This is useful to read values with the reader’s Deserializer implementation:

use deser::de::Deserializer;
use deser::io::Reader;

// `Lines` is the stream deserializer of a format with a string per
// line
let mut reader = Reader::new(&b"hello\nworld\n"[..], Lines);
let mut values = Vec::new();
while !reader.is_end().unwrap() {
    values.push(reader.deserialize::<String>().unwrap());
}
assert_eq!(values, ["hello", "world"]);
Source

pub fn end(&mut self) -> Result<(), Error>

Checks that there are no more values.

Fails if another value follows (or if the data that follows is not valid).

Source

pub fn iter<T: DeserializeOwned>(&mut self) -> Iter<'_, R, D, T> ⓘ

Returns an iterator over the remaining values.

The iterator stops after the first error.

Source

pub fn deserializer(&self) -> &D

Returns the stream deserializer.

This gives access to what the stream established so far, for instance the names of the columns of a CSV file.

Source

pub fn get_ref(&self) -> &R

Returns a reference to the underlying reader.

Source

pub fn get_mut(&mut self) -> &mut R

Returns a mutable reference to the underlying reader.

Reading from it directly is likely to corrupt the stream as the reader buffers data.

Source

pub fn into_inner(self) -> R

Returns the underlying reader.

Data that was read into the buffer but not deserialized yet is lost.

Source

pub fn into_parts(self) -> (R, D)

Returns the underlying reader and the stream deserializer.

Data that was read into the buffer but not deserialized yet is lost.

Trait Implementations§

Source§

impl<'de, R: Read, D: StreamDeserializer> Deserializer<'de> for Reader<R, D>

Reads the next value.

Values cannot borrow from the reader: borrowed data is passed on like data that is only valid for the call (see DeserializeDriver::transient). If there are no more values this fails (see Reader::is_end).

Source§

fn drive( &mut self, driver: &mut DeserializeDriver<'_, 'de>, ) -> Result<(), Error>

Parses the input and feeds the events of a value into the driver.
Source§

fn deserialize<T: Deserialize<'de>>(&mut self) -> Result<T, Error>
where Self: Sized,

Deserializes a value.
Source§

fn deserialize_with<T, F>(&mut self, setup: F) -> Result<T, Error>
where T: Deserialize<'de>, F: FnOnce(&mut DeserializeDriver<'_, 'de>), Self: Sized,

Deserializes a value with a configured driver. Read more
Source§

fn update<T: Deserialize<'de>>(&mut self, value: &mut T) -> Result<(), Error>
where Self: Sized,

Updates an existing value with the next value. Read more
Source§

fn update_with<T, F>(&mut self, value: &mut T, setup: F) -> Result<(), Error>
where T: Deserialize<'de>, F: FnOnce(&mut DeserializeDriver<'_, 'de>), Self: Sized,

Updates an existing value with the next value after setting up the driver. Read more

Auto Trait Implementations§

§

impl<R, D> !RefUnwindSafe for Reader<R, D>

§

impl<R, D> !Sync for Reader<R, D>

§

impl<R, D> !UnwindSafe for Reader<R, D>

§

impl<R, D> Freeze for Reader<R, D>
where R: Freeze, InputBuffer<D>: Freeze,

§

impl<R, D> Send for Reader<R, D>
where R: Send, InputBuffer<D>: Send,

§

impl<R, D> Unpin for Reader<R, D>
where R: Unpin, InputBuffer<D>: Unpin,

§

impl<R, D> UnsafeUnpin for Reader<R, D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.