serde/
impossible.rs

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
use std::{convert::Infallible, marker::PhantomData};

use super::ser::{BytesSerializer, MapSerializer, SequenceSerializer, Serialize, Serializer};

/// A helper struct to represent any type that cannot be serialized
pub struct Impossible<R, E> {
    void: Infallible,
    _marker: PhantomData<(R, E)>,
}

impl<R, E> Serializer for Impossible<R, E>
where
    E: std::error::Error,
{
    type Ok = R;
    type Err = E;
    type Bytes = Impossible<R, E>;
    type Seq = Impossible<R, E>;
    type Map = Impossible<R, E>;

    fn serialize_unit(self) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }

    fn serialize_i128(self, _value: i128) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }

    fn serialize_u128(self, _value: u128) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }

    fn serialize_f32(self, _value: f32) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }

    fn serialize_f64(self, _value: f64) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }

    fn serialize_bool(self, _value: bool) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }

    fn serialize_str(self, _value: &str) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }

    fn serialize_sequence(self) -> Result<Self::Seq, Self::Err> {
        match self.void {}
    }

    fn serialize_map(self) -> Result<Self::Map, Self::Err> {
        match self.void {}
    }

    fn serialize_none(self) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }

    fn serialize_slice<T: Serialize>(self, _value: &[T]) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }

    fn serialize_byte_seq(self) -> Result<Self::Bytes, Self::Err> {
        match self.void {}
    }
}

impl<R, E> MapSerializer for Impossible<R, E>
where
    E: std::error::Error,
{
    type Ok = R;
    type Err = E;

    fn serialize_entry<K: Serialize, V: Serialize>(
        &mut self,
        _key: &K,
        _value: &V,
    ) -> Result<(), Self::Err> {
        match self.void {}
    }

    fn end(self) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }
}

impl<R, E> SequenceSerializer for Impossible<R, E>
where
    E: std::error::Error,
{
    type Ok = R;
    type Err = E;

    fn serialize_element<T: Serialize>(&mut self, _value: &T) -> Result<(), Self::Err> {
        match self.void {}
    }

    fn end(self) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }
}

impl<R, E> BytesSerializer for Impossible<R, E>
where
    E: std::error::Error,
{
    type Ok = R;
    type Err = E;

    fn serialize_bytes<T: Serialize>(&mut self, _buf: &[u8]) -> Result<(), Self::Err> {
        match self.void {}
    }

    fn end(self) -> Result<Self::Ok, Self::Err> {
        match self.void {}
    }
}