[go: up one dir, main page]

Struct serde_with::As[][src]

pub struct As<T: ?Sized>(_);
Expand description

Adapter to convert from serde_as to the serde traits.

The As type adapter allows to use types implementing DeserializeAs or SerializeAs in place of serde’s with-annotation. The with-annotation allows to run custom code when de/serializing, however it is quite inflexible. The traits DeserializeAs/SerializeAs are more flexible, as they allow composition and nesting of types to create more complex de/serialization behavior. However, they are not directly compatible with serde, as they are not provided by serde. The As type adapter makes them compatible, by forwarding the function calls to serialize/deserialize to the corresponding functions serialize_as and deserialize_as.

It is not required to use this type directly. Instead, it is highly encouraged to use the #[serde_as] attribute since it includes further usability improvements. If the use of the use of the proc-macro is not acceptable, then As can be used directly with serde.

#[derive(Deserialize, Serialize)]
// Serialize numbers as sequence of strings, using Display and FromStr
#[serde(with = "As::<Vec<DisplayFromStr>>")]
field: Vec<u8>,

If the normal Deserialize/Serialize traits should be used, the placeholder type Same can be used. It implements DeserializeAs/SerializeAs, when the underlying type implements Deserialize/Serialize.

#[derive(Deserialize, Serialize)]
// Serialize map, turn keys into strings but keep type of value
#[serde(with = "As::<BTreeMap<DisplayFromStr, Same>>")]
field: BTreeMap<u8, i32>,

Implementations

impl<T: ?Sized> As<T>[src]

pub fn serialize<S, I>(value: &I, serializer: S) -> Result<S::Ok, S::Error> where
    S: Serializer,
    T: SerializeAs<I>,
    I: ?Sized
[src]

Serialize type T using SerializeAs

The function signature is compatible with serde’s with-annotation.

pub fn deserialize<'de, D, I>(deserializer: D) -> Result<I, D::Error> where
    T: DeserializeAs<'de, I>,
    D: Deserializer<'de>, 
[src]

Deserialize type T using DeserializeAs

The function signature is compatible with serde’s with-annotation.

Trait Implementations

impl<T: Clone + ?Sized> Clone for As<T>[src]

fn clone(&self) -> As<T>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Debug + ?Sized> Debug for As<T>[src]

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

Formats the value using the given formatter. Read more

impl<T: Default + ?Sized> Default for As<T>[src]

fn default() -> As<T>[src]

Returns the “default value” for a type. Read more

impl<T: Copy + ?Sized> Copy for As<T>[src]

Auto Trait Implementations

impl<T: ?Sized> RefUnwindSafe for As<T> where
    T: RefUnwindSafe

impl<T: ?Sized> Send for As<T> where
    T: Send

impl<T: ?Sized> Sync for As<T> where
    T: Sync

impl<T: ?Sized> Unpin for As<T> where
    T: Unpin

impl<T: ?Sized> UnwindSafe for As<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

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

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.