| 1 | // Copyright (c) 2020 Nikita Pekin and the lazycell contributors |
| 2 | // See the README.md file at the top-level directory of this distribution. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 5 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 7 | // option. This file may not be copied, modified, or distributed |
| 8 | // except according to those terms.use serde::ser::{Serialize, Serializer}; |
| 9 | use serde::ser::{Serialize, Serializer}; |
| 10 | use serde::de::{self, Deserialize, Deserializer, Visitor}; |
| 11 | |
| 12 | use std::fmt; |
| 13 | use std::marker::PhantomData; |
| 14 | |
| 15 | use super::{LazyCell, AtomicLazyCell}; |
| 16 | |
| 17 | impl<T: Serialize> Serialize for LazyCell<T> { |
| 18 | fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 19 | match self.borrow() { |
| 20 | Some(val) => serializer.serialize_some(val), |
| 21 | None => serializer.serialize_none() |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | |
| 27 | impl<T: Serialize> Serialize for AtomicLazyCell<T> { |
| 28 | fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 29 | match self.borrow() { |
| 30 | Some(val) => serializer.serialize_some(val), |
| 31 | None => serializer.serialize_none() |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | struct LazyCellVisitor<T>(PhantomData<*const T>); |
| 37 | impl<'de, T: Deserialize<'de>> Visitor<'de> for LazyCellVisitor<T> { |
| 38 | type Value = LazyCell<T>; |
| 39 | |
| 40 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 41 | formatter.write_str("a LazyCell") |
| 42 | } |
| 43 | |
| 44 | fn visit_some<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> { |
| 45 | let mut cell = LazyCell::new(); |
| 46 | cell.replace(T::deserialize(deserializer)?); |
| 47 | Ok(cell) |
| 48 | } |
| 49 | |
| 50 | fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> { |
| 51 | Ok(LazyCell::new()) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl<'de, T: Deserialize<'de>> Deserialize<'de> for LazyCell<T> { |
| 56 | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 57 | deserializer.deserialize_option(LazyCellVisitor(PhantomData)) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | struct AtomicLazyCellVisitor<T>(PhantomData<*const T>); |
| 63 | impl<'de, T: Deserialize<'de>> Visitor<'de> for AtomicLazyCellVisitor<T> { |
| 64 | type Value = AtomicLazyCell<T>; |
| 65 | |
| 66 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 67 | formatter.write_str("an AtomicLazyCell") |
| 68 | } |
| 69 | |
| 70 | fn visit_some<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> { |
| 71 | let mut cell = AtomicLazyCell::new(); |
| 72 | cell.replace(T::deserialize(deserializer)?); |
| 73 | Ok(cell) |
| 74 | } |
| 75 | |
| 76 | fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> { |
| 77 | Ok(AtomicLazyCell::new()) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | impl<'de, T: Deserialize<'de>> Deserialize<'de> for AtomicLazyCell<T> { |
| 83 | fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 84 | deserializer.deserialize_option(AtomicLazyCellVisitor(PhantomData)) |
| 85 | } |
| 86 | } |