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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::{ArcSwapAny, RefCnt, Strategy};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

impl<T, S> Serialize for ArcSwapAny<T, S>
where
    T: RefCnt + Serialize,
    S: Strategy<T>,
{
    fn serialize<Ser: Serializer>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error> {
        self.load().serialize(serializer)
    }
}

impl<'de, T, S> Deserialize<'de> for ArcSwapAny<T, S>
where
    T: RefCnt + Deserialize<'de>,
    S: Strategy<T> + Default,
{
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Ok(Self::from(T::deserialize(deserializer)?))
    }
}

#[cfg(test)]
mod tests {
    use crate::{ArcSwap, ArcSwapAny, ArcSwapOption, RefCnt};
    use serde_derive::{Deserialize, Serialize};
    use serde_test::{assert_tokens, Token};
    use std::sync::Arc;

    #[derive(Debug, Serialize, Deserialize)]
    #[serde(transparent)]
    struct ArcSwapAnyEq<T: RefCnt>(ArcSwapAny<T>);
    impl<T: RefCnt + PartialEq> PartialEq for ArcSwapAnyEq<T> {
        fn eq(&self, other: &Self) -> bool {
            self.0.load().eq(&other.0.load())
        }
    }
    impl<T: RefCnt + PartialEq> Eq for ArcSwapAnyEq<T> {}

    #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
    struct Foo {
        field0: u64,
        field1: String,
    }

    #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
    struct Bar {
        field0: ArcSwapAnyEq<Arc<u64>>,
        field1: ArcSwapAnyEq<Option<Arc<String>>>,
    }

    #[test]
    fn test_serialize_deserialize() {
        let field0 = u64::MAX;
        let field1 = "FOO_-0123456789";

        let data_orig = Foo {
            field0,
            field1: field1.to_string(),
        };
        let data = ArcSwapAnyEq(ArcSwap::from_pointee(data_orig));
        assert_tokens(
            &data,
            &[
                Token::Struct {
                    name: "Foo",
                    len: 2,
                },
                Token::Str("field0"),
                Token::U64(u64::MAX),
                Token::Str("field1"),
                Token::String(field1),
                Token::StructEnd,
            ],
        );

        let data = Bar {
            field0: ArcSwapAnyEq(ArcSwap::from_pointee(field0)),
            field1: ArcSwapAnyEq(ArcSwapOption::from_pointee(field1.to_string())),
        };
        assert_tokens(
            &data,
            &[
                Token::Struct {
                    name: "Bar",
                    len: 2,
                },
                Token::Str("field0"),
                Token::U64(u64::MAX),
                Token::Str("field1"),
                Token::Some,
                Token::String(field1),
                Token::StructEnd,
            ],
        );
    }

    #[test]
    fn test_serialize_deserialize_option() {
        let field0 = u64::MAX;
        let field1 = "FOO_-0123456789";

        let data_orig = Foo {
            field0,
            field1: field1.to_string(),
        };
        let data = ArcSwapAnyEq(ArcSwapOption::from_pointee(data_orig));
        assert_tokens(
            &data,
            &[
                Token::Some,
                Token::Struct {
                    name: "Foo",
                    len: 2,
                },
                Token::Str("field0"),
                Token::U64(u64::MAX),
                Token::Str("field1"),
                Token::String(field1),
                Token::StructEnd,
            ],
        );
    }

    #[test]
    fn test_serialize_deserialize_option_none() {
        let data = ArcSwapAnyEq(ArcSwapOption::<Foo>::from_pointee(None));

        assert_tokens(&data, &[Token::None]);
    }
}