Deserializer

Struct Deserializer 

Source
pub struct Deserializer<'de> { /* private fields */ }
Expand description

The struct that handles deserializing VDF into Rust structs

This typically doesn’t need to be invoked directly when from_str() and from_str_with_key() can be used instead

Implementations§

Source§

impl<'de> Deserializer<'de>

Source

pub fn new_with_key(vdf: Vdf<'de>) -> Result<(Self, Key<'de>)>

Attempts to create a new VDF deserializer along with returning the top level VDF key

Source

pub fn is_empty(&mut self) -> bool

Returns if the internal tokenstream is empty

Source

pub fn peek_is_value(&mut self) -> bool

Returns if the next token is a value type (str, object, or sequence)

Source

pub fn next_key_or_str(&mut self) -> Option<Cow<'de, str>>

Returns the next key or str if available

Source

pub fn next_key_or_str_else_eof(&mut self) -> Result<Cow<'de, str>>

Returns the next key or str or returns an appropriate error

Source

pub fn next_finite_float_else_eof(&mut self) -> Result<f32>

Returns the next finite float or returns an appropriate error

Methods from Deref<Target = Peekable<IntoIter<Token<'de>>>>§

1.0.0 · Source

pub fn peek(&mut self) -> Option<&<I as Iterator>::Item>

Returns a reference to the next() value without advancing the iterator.

Like next, if there is a value, it is wrapped in a Some(T). But if the iteration is over, None is returned.

Because peek() returns a reference, and many iterators iterate over references, there can be a possibly confusing situation where the return value is a double reference. You can see this effect in the examples below.

§Examples

Basic usage:

let xs = [1, 2, 3];

let mut iter = xs.iter().peekable();

// peek() lets us see into the future
assert_eq!(iter.peek(), Some(&&1));
assert_eq!(iter.next(), Some(&1));

assert_eq!(iter.next(), Some(&2));

// The iterator does not advance even if we `peek` multiple times
assert_eq!(iter.peek(), Some(&&3));
assert_eq!(iter.peek(), Some(&&3));

assert_eq!(iter.next(), Some(&3));

// After the iterator is finished, so is `peek()`
assert_eq!(iter.peek(), None);
assert_eq!(iter.next(), None);
1.53.0 · Source

pub fn peek_mut(&mut self) -> Option<&mut <I as Iterator>::Item>

Returns a mutable reference to the next() value without advancing the iterator.

Like next, if there is a value, it is wrapped in a Some(T). But if the iteration is over, None is returned.

Because peek_mut() returns a reference, and many iterators iterate over references, there can be a possibly confusing situation where the return value is a double reference. You can see this effect in the examples below.

§Examples

Basic usage:

let mut iter = [1, 2, 3].iter().peekable();

// Like with `peek()`, we can see into the future without advancing the iterator.
assert_eq!(iter.peek_mut(), Some(&mut &1));
assert_eq!(iter.peek_mut(), Some(&mut &1));
assert_eq!(iter.next(), Some(&1));

// Peek into the iterator and set the value behind the mutable reference.
if let Some(p) = iter.peek_mut() {
    assert_eq!(*p, &2);
    *p = &5;
}

// The value we put in reappears as the iterator continues.
assert_eq!(iter.collect::<Vec<_>>(), vec![&5, &3]);
1.51.0 · Source

pub fn next_if( &mut self, func: impl FnOnce(&<I as Iterator>::Item) -> bool, ) -> Option<<I as Iterator>::Item>

Consume and return the next value of this iterator if a condition is true.

If func returns true for the next value of this iterator, consume and return it. Otherwise, return None.

§Examples

Consume a number if it’s equal to 0.

let mut iter = (0..5).peekable();
// The first item of the iterator is 0; consume it.
assert_eq!(iter.next_if(|&x| x == 0), Some(0));
// The next item returned is now 1, so `next_if` will return `None`.
assert_eq!(iter.next_if(|&x| x == 0), None);
// `next_if` retains the next item if the predicate evaluates to `false` for it.
assert_eq!(iter.next(), Some(1));

Consume any number less than 10.

let mut iter = (1..20).peekable();
// Consume all numbers less than 10
while iter.next_if(|&x| x < 10).is_some() {}
// The next value returned will be 10
assert_eq!(iter.next(), Some(10));
1.51.0 · Source

pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<<I as Iterator>::Item>
where <I as Iterator>::Item: PartialEq<T>, T: ?Sized,

Consume and return the next item if it is equal to expected.

§Example

Consume a number if it’s equal to 0.

let mut iter = (0..5).peekable();
// The first item of the iterator is 0; consume it.
assert_eq!(iter.next_if_eq(&0), Some(0));
// The next item returned is now 1, so `next_if_eq` will return `None`.
assert_eq!(iter.next_if_eq(&0), None);
// `next_if_eq` retains the next item if it was not equal to `expected`.
assert_eq!(iter.next(), Some(1));
Source

pub fn next_if_map<R>( &mut self, f: impl FnOnce(<I as Iterator>::Item) -> Result<R, <I as Iterator>::Item>, ) -> Option<R>

🔬This is a nightly-only experimental API. (peekable_next_if_map)

Consumes the next value of this iterator and applies a function f on it, returning the result if the closure returns Ok.

Otherwise if the closure returns Err the value is put back for the next iteration.

The content of the Err variant is typically the original value of the closure, but this is not required. If a different value is returned, the next peek() or next() call will result in this new value. This is similar to modifying the output of peek_mut().

If the closure panics, the next value will always be consumed and dropped even if the panic is caught, because the closure never returned an Err value to put back.

See also: next_if_map_mut.

§Examples

Parse the leading decimal number from an iterator of characters.

#![feature(peekable_next_if_map)]
let mut iter = "125 GOTO 10".chars().peekable();
let mut line_num = 0_u32;
while let Some(digit) = iter.next_if_map(|c| c.to_digit(10).ok_or(c)) {
    line_num = line_num * 10 + digit;
}
assert_eq!(line_num, 125);
assert_eq!(iter.collect::<String>(), " GOTO 10");

Matching custom types.

#![feature(peekable_next_if_map)]

#[derive(Debug, PartialEq, Eq)]
enum Node {
    Comment(String),
    Red(String),
    Green(String),
    Blue(String),
}

/// Combines all consecutive `Comment` nodes into a single one.
fn combine_comments(nodes: Vec<Node>) -> Vec<Node> {
    let mut result = Vec::with_capacity(nodes.len());
    let mut iter = nodes.into_iter().peekable();
    let mut comment_text = None::<String>;
    loop {
        // Typically the closure in .next_if_map() matches on the input,
        //  extracts the desired pattern into an `Ok`,
        //  and puts the rest into an `Err`.
        while let Some(text) = iter.next_if_map(|node| match node {
            Node::Comment(text) => Ok(text),
            other => Err(other),
        }) {
            comment_text.get_or_insert_default().push_str(&text);
        }

        if let Some(text) = comment_text.take() {
            result.push(Node::Comment(text));
        }
        if let Some(node) = iter.next() {
            result.push(node);
        } else {
            break;
        }
    }
    result
}
Source

pub fn next_if_map_mut<R>( &mut self, f: impl FnOnce(&mut <I as Iterator>::Item) -> Option<R>, ) -> Option<R>

🔬This is a nightly-only experimental API. (peekable_next_if_map)

Gives a mutable reference to the next value of the iterator and applies a function f to it, returning the result and advancing the iterator if f returns Some.

Otherwise, if f returns None, the next value is kept for the next iteration.

If f panics, the item that is consumed from the iterator as if Some was returned from f. The value will be dropped.

This is similar to next_if_map, except ownership of the item is not given to f. This can be preferable if f would copy the item anyway.

§Examples

Parse the leading decimal number from an iterator of characters.

#![feature(peekable_next_if_map)]
let mut iter = "125 GOTO 10".chars().peekable();
let mut line_num = 0_u32;
while let Some(digit) = iter.next_if_map_mut(|c| c.to_digit(10)) {
    line_num = line_num * 10 + digit;
}
assert_eq!(line_num, 125);
assert_eq!(iter.collect::<String>(), " GOTO 10");

Trait Implementations§

Source§

impl<'de> Debug for Deserializer<'de>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deref for Deserializer<'de>

Source§

type Target = Peekable<IntoIter<Token<'de>>>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for Deserializer<'_>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'de> Deserializer<'de> for &mut Deserializer<'de>

Source§

type Error = Error

The error type that can be returned if some error occurs during deserialization.
Source§

fn deserialize_any<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Require the Deserializer to figure out how to drive the visitor based on what data type is in the input. Read more
Source§

fn deserialize_bool<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a bool value.
Source§

fn deserialize_i8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting an i8 value.
Source§

fn deserialize_i16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting an i16 value.
Source§

fn deserialize_i32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting an i32 value.
Source§

fn deserialize_i64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting an i64 value.
Source§

fn deserialize_i128<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting an i128 value. Read more
Source§

fn deserialize_u8<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a u8 value.
Source§

fn deserialize_u16<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a u16 value.
Source§

fn deserialize_u32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a u32 value.
Source§

fn deserialize_u64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a u64 value.
Source§

fn deserialize_u128<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting an u128 value. Read more
Source§

fn deserialize_f32<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a f32 value.
Source§

fn deserialize_f64<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a f64 value.
Source§

fn deserialize_char<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a char value.
Source§

fn deserialize_str<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a string value and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_string<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a string value and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_bytes<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a byte array and does not benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_byte_buf<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a byte array and would benefit from taking ownership of buffered data owned by the Deserializer. Read more
Source§

fn deserialize_option<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting an optional value. Read more
Source§

fn deserialize_unit<V: Visitor<'de>>(self, _visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a unit value.
Source§

fn deserialize_unit_struct<V: Visitor<'de>>( self, _name: &'static str, _visitor: V, ) -> Result<V::Value>

Hint that the Deserialize type is expecting a unit struct with a particular name.
Source§

fn deserialize_newtype_struct<V: Visitor<'de>>( self, _name: &'static str, visitor: V, ) -> Result<V::Value>

Hint that the Deserialize type is expecting a newtype struct with a particular name.
Source§

fn deserialize_seq<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a sequence of values.
Source§

fn deserialize_tuple<V: Visitor<'de>>( self, len: usize, visitor: V, ) -> Result<V::Value>

Hint that the Deserialize type is expecting a sequence of values and knows how many values there are without looking at the serialized data.
Source§

fn deserialize_tuple_struct<V: Visitor<'de>>( self, _name: &'static str, len: usize, visitor: V, ) -> Result<V::Value>

Hint that the Deserialize type is expecting a tuple struct with a particular name and number of fields.
Source§

fn deserialize_map<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting a map of key-value pairs.
Source§

fn deserialize_struct<V: Visitor<'de>>( self, _name: &'static str, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value>

Hint that the Deserialize type is expecting a struct with a particular name and fields.
Source§

fn deserialize_enum<V: Visitor<'de>>( self, _name: &'static str, _variants: &'static [&'static str], visitor: V, ) -> Result<V::Value>

Hint that the Deserialize type is expecting an enum value with a particular name and possible variants.
Source§

fn deserialize_identifier<V: Visitor<'de>>(self, visitor: V) -> Result<V::Value>

Hint that the Deserialize type is expecting the name of a struct field or the discriminant of an enum variant.
Source§

fn deserialize_ignored_any<V: Visitor<'de>>( self, visitor: V, ) -> Result<V::Value>

Hint that the Deserialize type needs to deserialize a value whose type doesn’t matter because it is ignored. Read more
Source§

fn is_human_readable(&self) -> bool

Determine whether Deserialize implementations should expect to deserialize their human-readable form. Read more

Auto Trait Implementations§

§

impl<'de> Freeze for Deserializer<'de>

§

impl<'de> RefUnwindSafe for Deserializer<'de>

§

impl<'de> Send for Deserializer<'de>

§

impl<'de> Sync for Deserializer<'de>

§

impl<'de> Unpin for Deserializer<'de>

§

impl<'de> UnwindSafe for Deserializer<'de>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.