AddRef

Struct AddRef 

Source
pub struct AddRef<F>(/* private fields */);
Expand description

Adapter to serialize values of type T with an adapter F that expects &T

§Example

use serde::Serialize;
use serde_json::json;

struct StrNumber(String);

impl TryFrom<&StrNumber> for i32 {
    type Error = std::num::ParseIntError;

    fn try_from(value: &StrNumber) -> Result<Self, Self::Error> {
        value.0.parse()
    }
}

#[derive(Serialize)]
struct Foo {
    #[serde(with = "serdapt::AddRef::<serdapt::TryInto<i32>>")]
    n: StrNumber,
}

let v = serde_json::to_value(Foo {
    n: StrNumber("33".into()),
})
.unwrap();
assert_eq!(v, json!({ "n": 33 }));

Implementations§

Source§

impl<F> AddRef<F>

Source

pub fn serialize<T, S>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where T: ?Sized, S: Serializer, Self: SerializeWith<T>,

Serializes value with adapter

Trait Implementations§

Source§

impl<F, T> SerializeWith<T> for AddRef<F>
where F: for<'a> SerializeWith<&'a T>, T: ?Sized,

Source§

fn serialize_with<S: Serializer>( value: &T, serializer: S, ) -> Result<S::Ok, S::Error>

Serializes value using serializer

Auto Trait Implementations§

§

impl<F> Freeze for AddRef<F>

§

impl<F> RefUnwindSafe for AddRef<F>
where F: RefUnwindSafe,

§

impl<F> Send for AddRef<F>
where F: Send,

§

impl<F> Sync for AddRef<F>
where F: Sync,

§

impl<F> Unpin for AddRef<F>
where F: Unpin,

§

impl<F> UnwindSafe for AddRef<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.