Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 // Copyright 2022 Damir Jelić
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #![doc = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!("../README.md")]
16 #![deny(
17 clippy::mem_forget,
18 clippy::unwrap_used,
19 dead_code,
20 missing_docs,
21 trivial_casts,
22 trivial_numeric_casts,
23 unsafe_code,
24 unsafe_op_in_unsafe_fn,
25 unused_extern_crates,
26 unused_import_braces,
27 unused_qualifications,
28 rust_2018_idioms
29 )]
30 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
31
32 use proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#;
33 use proc_macro2rust-analyzer cargo proc-macro2 1.0.103 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.103 TokenStream# as TokenStream2rust-analyzer cargo proc-macro2 1.0.103 TokenStream#;
34 use proc_macro_craterust-analyzer cargo proc-macro-crate 3.4.0 crate/::{crate_namerust-analyzer cargo proc-macro-crate 3.4.0 crate_name()., FoundCraterust-analyzer cargo proc-macro-crate 3.4.0 FoundCrate#};
35 use proc_macro_error2rust-analyzer cargo proc-macro-error2 2.0.1 crate/::{abort_call_siterust-analyzer cargo proc-macro-error2 2.0.1 macros/abort_call_site!, proc_macro_errorrust-analyzer cargo proc-macro-error-attr2 2.0.0 proc_macro_error:};
36 use quoterust-analyzer cargo quote 1.0.41 crate/::{format_identrust-analyzer cargo quote 1.0.41 format/format_ident!, quoterust-analyzer cargo quote 1.0.41 quote!};
37 use synrust-analyzer cargo syn 2.0.108 crate/::{
38 parse_macro_inputrust-analyzer cargo syn 2.0.108 parse_macro_input/, parse_quoterust-analyzer cargo syn 2.0.108 parse_quote/, punctuatedrust-analyzer cargo syn 2.0.108 punctuated/::Punctuatedrust-analyzer cargo syn 2.0.108 punctuated/Punctuated#, tokenrust-analyzer cargo syn 2.0.108 token/::Commarust-analyzer cargo syn 2.0.108 token/Comma#, Datarust-analyzer cargo syn 2.0.108 derive/Data#, DataEnumrust-analyzer cargo syn 2.0.108 derive/DataEnum#,
39 DataStructrust-analyzer cargo syn 2.0.108 derive/DataStruct#, DeriveInputrust-analyzer cargo syn 2.0.108 derive/DeriveInput#, Fieldrust-analyzer cargo syn 2.0.108 data/Field#, Fieldsrust-analyzer cargo syn 2.0.108 data/Fields#, FieldsNamedrust-analyzer cargo syn 2.0.108 data/FieldsNamed#, FieldsUnnamedrust-analyzer cargo syn 2.0.108 data/FieldsUnnamed#, GenericParamrust-analyzer cargo syn 2.0.108 generics/GenericParam#, Typerust-analyzer cargo syn 2.0.108 ty/Type#,
40 };
41
42 fn use_matrix_picklerust-analyzer cargo matrix-pickle-derive 0.2.2 use_matrix_pickle().() -> TokenStream2rust-analyzer cargo proc-macro2 1.0.103 TokenStream# {
43 if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(FoundCraterust-analyzer cargo proc-macro-crate 3.4.0 FoundCrate#::Namerust-analyzer cargo proc-macro-crate 3.4.0 FoundCrate#Name#(name)) = crate_namerust-analyzer cargo proc-macro-crate 3.4.0 crate_name().("matrix-pickle") {
44 let import = format_identrust-analyzer cargo quote 1.0.41 format/format_ident!!("{}", name);
45 quoterust-analyzer cargo quote 1.0.41 quote!! { ::#import }
46 } else {
47 quoterust-analyzer cargo quote 1.0.41 quote!! { ::matrix_pickle }
48 }
49 }
50
51 /// Derive an `Encode` implementation for a struct or enum.
52 #[proc_macro_errorrust-analyzer cargo proc-macro-error-attr2 2.0.0 proc_macro_error:]
53 #[proc_macro_derive(Encode)]
54 pub fn derive_encoderust-analyzer cargo matrix-pickle-derive 0.2.2 Encode:(input: TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# {
55 let mut input = parse_macro_inputrust-analyzer cargo syn 2.0.108 parse_macro_input/parse_macro_input!!(input as DeriveInputrust-analyzer cargo syn 2.0.108 derive/DeriveInput#);
56 let name = &input.identrust-analyzer cargo syn 2.0.108 derive/DeriveInput#ident.;
57
58 let matrix_pickle = use_matrix_picklerust-analyzer cargo matrix-pickle-derive 0.2.2 use_matrix_pickle().();
59
60 for param in &mut input.genericsrust-analyzer cargo syn 2.0.108 derive/DeriveInput#generics..paramsrust-analyzer cargo syn 2.0.108 generics/Generics#params. {
61 if let GenericParamrust-analyzer cargo syn 2.0.108 generics/GenericParam#::Typerust-analyzer cargo syn 2.0.108 generics/GenericParam#Type#(ref mut type_param) = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().param {
62 type_param.boundsrust-analyzer cargo syn 2.0.108 generics/TypeParam#bounds..pushrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]push().(parse_quoterust-analyzer cargo syn 2.0.108 parse_quote/parse_quote!!(#matrix_pickle::Encode));
63 }
64 }
65
66 let generics = input.genericsrust-analyzer cargo syn 2.0.108 derive/DeriveInput#generics.;
67 let (impl_generics, ty_generics, where_clause) = generics.split_for_implrust-analyzer cargo syn 2.0.108 generics/impl#[Generics]split_for_impl().();
68
69 match input.datarust-analyzer cargo syn 2.0.108 derive/DeriveInput#data. {
70 Datarust-analyzer cargo syn 2.0.108 derive/Data#::Structrust-analyzer cargo syn 2.0.108 derive/Data#Struct#(DataStructrust-analyzer cargo syn 2.0.108 derive/DataStruct# {
71 fieldsrust-analyzer cargo syn 2.0.108 derive/DataStruct#fields.: Fieldsrust-analyzer cargo syn 2.0.108 data/Fields#::Namedrust-analyzer cargo syn 2.0.108 data/Fields#Named#(FieldsNamedrust-analyzer cargo syn 2.0.108 data/FieldsNamed# { namedrust-analyzer cargo syn 2.0.108 data/FieldsNamed#named., .. }),
72 ..
73 }) => {
74 let idents = named.iterrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| &f.identrust-analyzer cargo syn 2.0.108 data/Field#ident.);
75
76 quoterust-analyzer cargo quote 1.0.41 quote!! {
77 impl #impl_generics #matrix_pickle::Encode for #name #ty_generics #where_clause {
78 fn encode(&self, writer: &mut impl std::io::Write) -> Result<usize, #matrix_pickle::EncodeError> {
79 let mut ret = 0;
80
81 #(ret += self.#idents.encode(writer)?;)*
82
83 Ok(ret)
84 }
85 }
86 }
87 }
88 Datarust-analyzer cargo syn 2.0.108 derive/Data#::Structrust-analyzer cargo syn 2.0.108 derive/Data#Struct#(DataStructrust-analyzer cargo syn 2.0.108 derive/DataStruct# {
89 fieldsrust-analyzer cargo syn 2.0.108 derive/DataStruct#fields.: Fieldsrust-analyzer cargo syn 2.0.108 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.108 data/Fields#Unnamed#(FieldsUnnamedrust-analyzer cargo syn 2.0.108 data/FieldsUnnamed# { unnamedrust-analyzer cargo syn 2.0.108 data/FieldsUnnamed#unnamed., .. }),
90 ..
91 }) => {
92 let i = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#unnamed.lenrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]len().()).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(synrust-analyzer cargo syn 2.0.108 crate/::Indexrust-analyzer cargo syn 2.0.108 expr/Index#::fromrust-analyzer cargo syn 2.0.108 expr/impl#[Index][`From<usize>`]from().);
93
94 quoterust-analyzer cargo quote 1.0.41 quote!! {
95 impl #impl_generics #matrix_pickle::Encode for #name #ty_generics #where_clause {
96 fn encode(&self, writer: &mut impl std::io::Write) -> Result<usize, #matrix_pickle::EncodeError> {
97 let mut ret = 0;
98
99 #(ret += self.#i.encode(writer)?;)*
100
101 Ok(ret)
102 }
103 }
104 }
105 }
106 Datarust-analyzer cargo syn 2.0.108 derive/Data#::Enumrust-analyzer cargo syn 2.0.108 derive/Data#Enum#(DataEnumrust-analyzer cargo syn 2.0.108 derive/DataEnum# { variantsrust-analyzer cargo syn 2.0.108 derive/DataEnum#variants., .. }) => {
107 let names = variants.iterrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|v| &v.identrust-analyzer cargo syn 2.0.108 data/Variant#ident.);
108 let numbers = 0u8..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#variants.lenrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]len().().try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Only enums with up to 256 elements are supported");
109
110 quoterust-analyzer cargo quote 1.0.41 quote!! {
111 impl #impl_generics #matrix_pickle::Encode for #name #ty_generics #where_clause {
112 fn encode(&self, writer: &mut impl std::io::Write) -> Result<usize, #matrix_pickle::EncodeError> {
113 let mut ret = 0;
114
115 match self {
116 #(#name::#names(v) => {
117 ret += #numbers.encode(writer)?;
118 ret += v.encode(writer)?;
119 }),*
120 }
121
122 Ok(ret)
123 }
124 }
125 }
126 }
127
128 _ => abort_call_siterust-analyzer cargo proc-macro-error2 2.0.1 macros/abort_call_site!!("`#[derive(Encode)` only supports non-tuple structs"),
129 }.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
130 }
131
132 fn check_if_boxedrust-analyzer cargo matrix-pickle-derive 0.2.2 check_if_boxed().(fields: &Punctuatedrust-analyzer cargo syn 2.0.108 punctuated/Punctuated#<Fieldrust-analyzer cargo syn 2.0.108 data/Field#, Commarust-analyzer cargo syn 2.0.108 token/Comma#>) {
133 for field in fields {
134 for attribute in &field.attrsrust-analyzer cargo syn 2.0.108 data/Field#attrs. {
135 if attribute.pathrust-analyzer cargo syn 2.0.108 attr/impl#[Attribute]path().().is_identrust-analyzer cargo syn 2.0.108 path/impl#[Path]is_ident().("secret") {
136 match &field.tyrust-analyzer cargo syn 2.0.108 data/Field#ty. {
137 Typerust-analyzer cargo syn 2.0.108 ty/Type#::Arrayrust-analyzer cargo syn 2.0.108 ty/Type#Array#(_) => abort_call_siterust-analyzer cargo proc-macro-error2 2.0.1 macros/abort_call_site!!(
138 "Arrays need to be boxed to avoid unintended copies of the secret"
139 ),
140 Typerust-analyzer cargo syn 2.0.108 ty/Type#::Pathrust-analyzer cargo syn 2.0.108 ty/Type#Path#(_) => {}
141 _ => {
142 abort_call_siterust-analyzer cargo proc-macro-error2 2.0.1 macros/abort_call_site!!("Type {} does not support being decoded as a secret value")
143 }
144 }
145 }
146 }
147 }
148 }
149
150 /// Derive an `Decode` implementation for a struct or enum.
151 #[proc_macro_errorrust-analyzer cargo proc-macro-error-attr2 2.0.0 proc_macro_error:]
152 #[proc_macro_derive(Decode, attributes(secret))]
153 pub fn derive_decoderust-analyzer cargo matrix-pickle-derive 0.2.2 Decode:(input: TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#) -> TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# {
154 let mut input = parse_macro_inputrust-analyzer cargo syn 2.0.108 parse_macro_input/parse_macro_input!!(input as DeriveInputrust-analyzer cargo syn 2.0.108 derive/DeriveInput#);
155 let name = &input.identrust-analyzer cargo syn 2.0.108 derive/DeriveInput#ident.;
156
157 let matrix_pickle = use_matrix_picklerust-analyzer cargo matrix-pickle-derive 0.2.2 use_matrix_pickle().();
158
159 for param in &mut input.genericsrust-analyzer cargo syn 2.0.108 derive/DeriveInput#generics..paramsrust-analyzer cargo syn 2.0.108 generics/Generics#params. {
160 if let GenericParamrust-analyzer cargo syn 2.0.108 generics/GenericParam#::Typerust-analyzer cargo syn 2.0.108 generics/GenericParam#Type#(ref mut type_param) = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().param {
161 type_param.boundsrust-analyzer cargo syn 2.0.108 generics/TypeParam#bounds..pushrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]push().(parse_quoterust-analyzer cargo syn 2.0.108 parse_quote/parse_quote!!(#matrix_pickle::Encode));
162 }
163 }
164
165 let generics = input.genericsrust-analyzer cargo syn 2.0.108 derive/DeriveInput#generics.;
166 let (impl_generics, ty_generics, where_clause) = generics.split_for_implrust-analyzer cargo syn 2.0.108 generics/impl#[Generics]split_for_impl().();
167
168 match input.datarust-analyzer cargo syn 2.0.108 derive/DeriveInput#data. {
169 Datarust-analyzer cargo syn 2.0.108 derive/Data#::Structrust-analyzer cargo syn 2.0.108 derive/Data#Struct#(DataStructrust-analyzer cargo syn 2.0.108 derive/DataStruct# {
170 fieldsrust-analyzer cargo syn 2.0.108 derive/DataStruct#fields.: Fieldsrust-analyzer cargo syn 2.0.108 data/Fields#::Namedrust-analyzer cargo syn 2.0.108 data/Fields#Named#(FieldsNamedrust-analyzer cargo syn 2.0.108 data/FieldsNamed# { namedrust-analyzer cargo syn 2.0.108 data/FieldsNamed#named., .. }),
171 ..
172 }) => {
173 check_if_boxedrust-analyzer cargo matrix-pickle-derive 0.2.2 check_if_boxed().(&named);
174
175 let names = named.iterrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| &f.identrust-analyzer cargo syn 2.0.108 data/Field#ident.);
176 let field_types = named.iterrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| &f.tyrust-analyzer cargo syn 2.0.108 data/Field#ty.);
177
178 quoterust-analyzer cargo quote 1.0.41 quote!! {
179 impl #impl_generics #matrix_pickle::Decode for #name #ty_generics #where_clause {
180 fn decode(reader: &mut impl std::io::Read) -> Result<Self, #matrix_pickle::DecodeError> {
181 Ok(Self {
182 #(#names: <#field_types>::decode(reader)?),*
183 })
184 }
185 }
186 }
187 }
188 Datarust-analyzer cargo syn 2.0.108 derive/Data#::Structrust-analyzer cargo syn 2.0.108 derive/Data#Struct#(DataStructrust-analyzer cargo syn 2.0.108 derive/DataStruct# {
189 fieldsrust-analyzer cargo syn 2.0.108 derive/DataStruct#fields.: Fieldsrust-analyzer cargo syn 2.0.108 data/Fields#::Unnamedrust-analyzer cargo syn 2.0.108 data/Fields#Unnamed#(FieldsUnnamedrust-analyzer cargo syn 2.0.108 data/FieldsUnnamed# { unnamedrust-analyzer cargo syn 2.0.108 data/FieldsUnnamed#unnamed., .. }),
190 ..
191 }) => {
192 check_if_boxedrust-analyzer cargo matrix-pickle-derive 0.2.2 check_if_boxed().(&unnamed);
193
194 let field_types = unnamed.iterrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|f| &f.tyrust-analyzer cargo syn 2.0.108 data/Field#ty.);
195
196 quoterust-analyzer cargo quote 1.0.41 quote!! {
197 impl #impl_generics #matrix_pickle::Decode for #name #ty_generics #where_clause {
198 fn decode(reader: &mut impl std::io::Read) -> Result<Self, #matrix_pickle::DecodeError> {
199 Ok(Self (
200 #(<#field_types>::decode(reader)?),*
201 ))
202 }
203 }
204 }
205 }
206 Datarust-analyzer cargo syn 2.0.108 derive/Data#::Enumrust-analyzer cargo syn 2.0.108 derive/Data#Enum#(DataEnumrust-analyzer cargo syn 2.0.108 derive/DataEnum# { variantsrust-analyzer cargo syn 2.0.108 derive/DataEnum#variants., .. }) => {
207 let names = variants.iterrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|v| &v.identrust-analyzer cargo syn 2.0.108 data/Variant#ident.);
208 let numbers = 0u8..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#variants.lenrust-analyzer cargo syn 2.0.108 punctuated/impl#[`Punctuated<T, P>`]len().().try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Only enums with up to 256 elements are supported");
209
210 quoterust-analyzer cargo quote 1.0.41 quote!! {
211 impl #impl_generics #matrix_pickle::Decode for #name #ty_generics #where_clause {
212 fn decode(reader: &mut impl std::io::Read) -> Result<Self, #matrix_pickle::DecodeError> {
213 let variant = u8::decode(reader)?;
214
215 match variant {
216 #(#numbers => {
217 let x = Decode::decode(reader)?;
218 Ok(Self::#names(x))
219 }),*
220
221 _ => Err(#matrix_pickle::DecodeError::UnknownEnumVariant(variant))
222 }
223 }
224 }
225 }
226 }
227 _ => abort_call_siterust-analyzer cargo proc-macro-error2 2.0.1 macros/abort_call_site!!("`#[derive(Encode)` only supports non-tuple structs"),
228 }.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()
229 }