Fold

Struct Fold 

Source
pub struct Fold<T, A, F = Id> { /* private fields */ }
Expand description

Sequence adapter to fold over its items when deserializing

This adapter causes a sequence to be deserialized such that its items are deserialized with F and folded over with A::add and A::default() as the initial accumulator.

This avoids collecting when only the result of the fold is needed.

§Example

use serde::Deserialize;
use serde_json::json;

#[derive(Deserialize)]
struct Foo(#[serde(with = "serdapt::Fold::<i32, i32>")] i32);

let Foo(sum) = serde_json::from_value::<Foo>(json!([1, 2, 3])).unwrap();
assert_eq!(sum, 1 + 2 + 3);

Implementations§

Source§

impl<T, A, F> Fold<T, A, F>

Source

pub fn deserialize<'de, U, D>(deserializer: D) -> Result<U, D::Error>
where D: Deserializer<'de>, Self: DeserializeWith<'de, U>,

Deserializes value with adapter

Trait Implementations§

Source§

impl<'de, T, A, F> DeserializeWith<'de, A> for Fold<T, A, F>
where F: DeserializeWith<'de, T>, A: Default + Add<T, Output = A>,

Source§

fn deserialize_with<D>(deserializer: D) -> Result<A, D::Error>
where D: Deserializer<'de>,

Deserializes a value using deserializer

Auto Trait Implementations§

§

impl<T, A, F> Freeze for Fold<T, A, F>

§

impl<T, A, F> RefUnwindSafe for Fold<T, A, F>
where F: RefUnwindSafe,

§

impl<T, A, F> Send for Fold<T, A, F>
where F: Send,

§

impl<T, A, F> Sync for Fold<T, A, F>
where F: Sync,

§

impl<T, A, F> Unpin for Fold<T, A, F>
where F: Unpin,

§

impl<T, A, F> UnwindSafe for Fold<T, A, F>
where F: UnwindSafe,

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 = 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.