1{{!
2
3  Copyright (c) Facebook, Inc. and its affiliates.
4
5  Licensed under the Apache License, Version 2.0 (the "License");
6  you may not use this file except in compliance with the License.
7  You may obtain a copy of the License at
8
9      http://www.apache.org/licenses/LICENSE-2.0
10
11  Unless required by applicable law or agreed to in writing, software
12  distributed under the License is distributed on an "AS IS" BASIS,
13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  See the License for the specific language governing permissions and
15  limitations under the License.
16
17}}
18
19    use ref_cast::RefCast;
20
21    #[derive(RefCast)]
22    #[repr(transparent)]
23    pub(crate) struct LocalImpl<T>(T);
24
25    #[allow(unused)]
26    pub(crate) fn write<T, P>(value: &T, p: &mut P)
27    where
28        LocalImpl<T>: ::fbthrift::Serialize<P>,
29        P: ::fbthrift::ProtocolWriter,
30    {
31        ::fbthrift::Serialize::write(LocalImpl::ref_cast(value), p);
32    }
33
34    #[allow(unused)]
35    pub(crate) fn read<T, P>(p: &mut P) -> ::anyhow::Result<T>
36    where
37        LocalImpl<T>: ::fbthrift::Deserialize<P>,
38        P: ::fbthrift::ProtocolReader,
39    {
40        let value: LocalImpl<T> = ::fbthrift::Deserialize::read(p)?;
41        ::std::result::Result::Ok(value.0)
42    }{{!
43
44}}{{#program:nonstandardTypes}}{{!
45    }}{{#type:set?}}
46
47    impl<P> ::fbthrift::Serialize<P> for LocalImpl<{{>lib/rawtype}}>
48    where
49        P: ::fbthrift::ProtocolWriter,
50    {
51        fn write(&self, p: &mut P) {
52            p.write_set_begin(
53                <{{#type:set_elem_type}}{{>lib/type}}{{/type:set_elem_type}} as ::fbthrift::GetTType>::TTYPE,
54                self.0.len(),
55            );
56            for v in &self.0 {
57                p.write_set_value_begin();
58                {{#type:set_elem_type}}{{>lib/write}}{{/type:set_elem_type}}(v, p);
59            }
60            p.write_set_end();
61        }
62    }
63
64    impl<P> ::fbthrift::Deserialize<P> for LocalImpl<{{>lib/rawtype}}>
65    where
66        P: ::fbthrift::ProtocolReader,
67    {
68        fn read(p: &mut P) -> ::anyhow::Result<Self> {
69            let (_elem_ty, len) = p.read_set_begin()?;
70            let mut set = <{{>lib/type}}>::with_capacity(len.unwrap_or_default());
71
72            if let Some(0) = len {
73                return Ok(LocalImpl(set));
74            }
75
76            let mut idx = 0;
77            loop {
78                let more = p.read_set_value_begin()?;
79                if !more {
80                    break;
81                }
82                {{#type:set_elem_type}}let v: {{>lib/type}} = {{>lib/read}}(p){{/type:set_elem_type}}?;
83                p.read_set_value_end()?;
84                set.insert(v);
85
86                idx += 1;
87                if ::fbthrift::protocol::should_break(len, more, idx) {
88                    break;
89                }
90            }
91            p.read_set_end()?;
92            ::std::result::Result::Ok(LocalImpl(set))
93        }
94    }{{!
95    }}{{/type:set?}}{{!
96
97    }}{{#type:map?}}
98
99    impl<P> ::fbthrift::Serialize<P> for LocalImpl<{{>lib/rawtype}}>
100    where
101        P: ::fbthrift::ProtocolWriter,
102    {
103        fn write(&self, p: &mut P) {
104            p.write_map_begin(
105                <{{#type:key_type}}{{>lib/type}}{{/type:key_type}} as ::fbthrift::GetTType>::TTYPE,
106                <{{#type:value_type}}{{>lib/type}}{{/type:value_type}} as ::fbthrift::GetTType>::TTYPE,
107                self.0.len(),
108            );
109            for (k, v) in &self.0 {
110                p.write_map_key_begin();
111                {{#type:key_type}}{{>lib/write}}{{/type:key_type}}(k, p);
112                p.write_map_value_begin();
113                {{#type:value_type}}{{>lib/write}}{{/type:value_type}}(v, p);
114            }
115            p.write_map_end();
116        }
117    }
118
119    impl<P> ::fbthrift::Deserialize<P> for LocalImpl<{{>lib/rawtype}}>
120    where
121        P: ::fbthrift::ProtocolReader,
122    {
123        fn read(p: &mut P) -> ::anyhow::Result<Self> {
124            let (_key_ty, _val_ty, len) = p.read_map_begin()?;
125            let mut map = <{{>lib/type}}>::with_capacity(len.unwrap_or_default());
126
127            if let Some(0) = len {
128                return Ok(LocalImpl(map));
129            }
130
131            let mut idx = 0;
132            loop {
133                let more = p.read_map_key_begin()?;
134                if !more {
135                    break;
136                }
137                {{#type:key_type}}let k: {{>lib/type}} = {{>lib/read}}(p){{/type:key_type}}?;
138                p.read_map_value_begin()?;
139                {{#type:value_type}}let v: {{>lib/type}} = {{>lib/read}}(p){{/type:value_type}}?;
140                p.read_map_value_end()?;
141                map.insert(k, v);
142
143                idx += 1;
144                if ::fbthrift::protocol::should_break(len, more, idx) {
145                    break;
146                }
147            }
148            p.read_map_end()?;
149            ::std::result::Result::Ok(LocalImpl(map))
150        }
151    }{{!
152    }}{{/type:map?}}{{!
153
154    }}{{#type:binary?}}
155
156    impl<P> ::fbthrift::Serialize<P> for LocalImpl<{{>lib/rawtype}}>
157    where
158        P: ::fbthrift::ProtocolWriter,
159    {
160        fn write(&self, p: &mut P) {
161            self.0.as_slice().write(p)
162        }
163    }
164
165    impl<P> ::fbthrift::Deserialize<P> for LocalImpl<{{>lib/rawtype}}>
166    where
167        P: ::fbthrift::ProtocolReader,
168    {
169        fn read(p: &mut P) -> ::anyhow::Result<Self> {
170            p.read_binary()
171        }
172    }
173
174    impl ::fbthrift::binary_type::BinaryType for LocalImpl<{{>lib/rawtype}}> {
175        fn with_capacity(capacity: usize) -> Self {
176            LocalImpl(<{{>lib/rawtype}}>::with_capacity(capacity))
177        }
178        fn extend_from_slice(&mut self, other: &[u8]) {
179            self.0.extend_from_slice(other)
180        }
181        fn from_vec(vec: ::std::vec::Vec<u8>) -> Self {
182            LocalImpl(::std::convert::Into::into(vec))
183        }
184    }{{!
185    }}{{/type:binary?}}{{!
186}}{{/program:nonstandardTypes}}
187{{!newline}}
188