Skip to content

Commit

Permalink
Merge #298
Browse files Browse the repository at this point in the history
298: Add example of implementing DeserializeAs for remote types. r=jonasbb a=jonasbb

bors r+

Co-authored-by: Jonas Bushart <[email protected]>
  • Loading branch information
bors[bot] and jonasbb authored Apr 13, 2021
2 parents 2b98ed7 + 730801c commit 3156eee
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/guide/serde_as.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is an alternative to serde's with-annotation.
It is more flexible and composable but work with fewer types.

The scheme is based on two new traits, [`SerializeAs`] and [`DeserializeAs`], which need to be implemented by all types which want to be compatible with `serde_as`.
The proc macro attribute [`#[serde_as]`][crate::serde_as] exists as a usability boost for users.
The proc-macro attribute [`#[serde_as]`][crate::serde_as] exists as a usability boost for users.

This site contains some general advice how to use this crate and then lists the implemented conversions for `serde_as`.
The basic design of the system was done by [@markazmierczak](https://github.com/markazmierczak).
Expand Down Expand Up @@ -63,7 +63,7 @@ struct A {
}
```

The main advantage is that you can compose `serde_as` stuff, which is not possible with the with-annotation.
The main advantage is that you can compose `serde_as` stuff, which is impossible with the with-annotation.
For example, the `mime` field from above could be nested in one or more data structures:

```rust
Expand Down Expand Up @@ -151,6 +151,15 @@ impl SerializeAs<RemoteType> for LocalType {
MODULE::serialize(value, serializer)
}
}

impl<'de> DeserializeAs<'de, RemoteType> for LocalType {
fn deserialize_as<D>(deserializer: D) -> Result<RemoteType, D::Error>
where
D: Deserializer<'de>,
{
MODULE::deserialize(deserializer)
}
}
# }
```

Expand All @@ -173,7 +182,7 @@ struct Data {
### Using `#[serde_as]` with serde's remote derives

A special case of the above section is using it on remote derives.
This is a special functionality of serde where it derives the de-/serialization code for a type from another crate if all fields are `pub`.
This is a special functionality of serde where it derives the de/serialization code for a type from another crate if all fields are `pub`.
You can find all the details in the [official serde documentation](https://serde.rs/remote-derive.html).

```rust
Expand Down Expand Up @@ -205,7 +214,7 @@ struct DurationDef {
```

Our goal is it now to use `Duration` within `serde_as`.
We make use of the existing `DurationDef` type and its `serialize` and `deserialize` functions.
We use the existing `DurationDef` type and its `serialize` and `deserialize` functions.
We can write this implementation.
The implementation for `DeserializeAs` works analogue.

Expand Down Expand Up @@ -280,7 +289,7 @@ pub use serde_with;
pub use some_other_lib_derive::define_some_type;
```

And the procedural macro can be used by other crates without any additional imports:
The procedural macro can be used by other crates without any additional imports:

```rust,ignore
// consuming_crate/src/main.rs
Expand Down

0 comments on commit 3156eee

Please sign in to comment.