on master help sign in
Add optional support for `serde` for `Cached<R>`
commit45c1f03f945f3ec1bd951eacdaba5064292a3d6a authorNick Fitzgerald <fitzgen@gmail.com> dateMon 20 May 2019, 10:49 (7 years ago) parenta9389c3
4 files changed+37
Cargo.lock+1 Cargo.toml+1 README.md+9 src/cached.rs+26
Cargo.lock+1
@@ -218,6 +218,7 @@ dependencies = [
218218 "js-sys 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
219219 "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
220220 "longest-increasing-subsequence 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
221+ "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)",
221222 "wasm-bindgen 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
222223 "wasm-bindgen-futures 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
223224 "wasm-bindgen-test 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
Cargo.toml+1
@@ -52,6 +52,7 @@ fxhash = "0.2.1"
5252log = { version = "0.4.6", optional = true }
5353bitflags = "1.0.5"
5454longest-increasing-subsequence = "0.1.0"
55+serde = { version = "1.0.91", optional = true }
5556
5657[dependencies.web-sys]
5758version = "0.3.19"
README.md+9
@@ -57,6 +57,15 @@ directory](https://github.com/fitzgen/dodrio/tree/master/examples), including:
5757* [`js-component`](https://github.com/fitzgen/dodrio/tree/master/examples/js-component):
5858 Defines a rendering component in JavaScript with the `dodrio-js-api` crate.
5959
60+## Cargo Features
61+
62+* `log` &mdash; enable debugging-oriented log messages with the `log` crate's
63+ facade. You still have to initialize a logger for the messages to go anywhere,
64+ such as [`console_log`](https://github.com/iamcodemaker/console_log).
65+
66+* `serde` &mdash; enable `serde::{Serialize, Deserialize}` implementations for
67+ `Cached<R>` where `R` is serializable and deserializable.
68+
6069## Design
6170
6271### Bump Allocation
src/cached.rs+26
@@ -195,3 +195,29 @@ where
195195 cached.into()
196196 }
197197}
198+
199+#[cfg(feature = "serde")]
200+impl<R> serde::Serialize for Cached<R>
201+where
202+ R: Default + serde::Serialize,
203+{
204+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
205+ where
206+ S: serde::Serializer,
207+ {
208+ self.inner.serialize(serializer)
209+ }
210+}
211+
212+#[cfg(feature = "serde")]
213+impl<'de, R> serde::Deserialize<'de> for Cached<R>
214+where
215+ R: Default + serde::Deserialize<'de>,
216+{
217+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
218+ where
219+ D: serde::Deserializer<'de>,
220+ {
221+ R::deserialize(deserializer).map(Cached::new)
222+ }
223+}