summaryrefslogtreecommitdiffstats
path: root/vendor/lazycell/src/serde_impl.rs
blob: 8f08f7b03ef771172aae3cc41e9f68b19c117da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2020 Nikita Pekin and the lazycell contributors
// See the README.md file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.use serde::ser::{Serialize, Serializer};
use serde::ser::{Serialize, Serializer};
use serde::de::{self, Deserialize, Deserializer, Visitor};

use std::fmt;
use std::marker::PhantomData;

use super::{LazyCell, AtomicLazyCell};

impl<T: Serialize> Serialize for LazyCell<T> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self.borrow() {
            Some(val) => serializer.serialize_some(val),
            None => serializer.serialize_none()
        }
    }
}


impl<T: Serialize> Serialize for AtomicLazyCell<T> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self.borrow() {
            Some(val) => serializer.serialize_some(val),
            None => serializer.serialize_none()
        }
    }
}

struct LazyCellVisitor<T>(PhantomData<*const T>);
impl<'de, T: Deserialize<'de>> Visitor<'de> for LazyCellVisitor<T> {
    type Value = LazyCell<T>;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a LazyCell")
    }

    fn visit_some<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
        let mut cell = LazyCell::new();
        cell.replace(T::deserialize(deserializer)?);
        Ok(cell)
    }

    fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
        Ok(LazyCell::new())
    }
}

impl<'de, T: Deserialize<'de>> Deserialize<'de> for LazyCell<T> {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_option(LazyCellVisitor(PhantomData))
    }
}


struct AtomicLazyCellVisitor<T>(PhantomData<*const T>);
impl<'de, T: Deserialize<'de>> Visitor<'de> for AtomicLazyCellVisitor<T> {
    type Value = AtomicLazyCell<T>;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("an AtomicLazyCell")
    }

    fn visit_some<D: Deserializer<'de>>(self, deserializer: D) -> Result<Self::Value, D::Error> {
        let mut cell = AtomicLazyCell::new();
        cell.replace(T::deserialize(deserializer)?);
        Ok(cell)
    }

    fn visit_none<E: de::Error>(self) -> Result<Self::Value, E> {
        Ok(AtomicLazyCell::new())
    }
}


impl<'de, T: Deserialize<'de>> Deserialize<'de> for AtomicLazyCell<T> {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_option(AtomicLazyCellVisitor(PhantomData))
    }
}