Debian Codegraph @

tests/test_custom_types.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
2
3 use pyo3rust-analyzer cargo pyo3 0.28.1 crate/::{
4 exceptionsrust-analyzer cargo pyo3 0.28.1 exceptions/::{PyIndexErrorrust-analyzer cargo pyo3 0.28.1 exceptions/PyIndexError#, PyKeyErrorrust-analyzer cargo pyo3 0.28.1 exceptions/PyKeyError#},
5 preluderust-analyzer cargo pyo3 0.28.1 prelude/::*,
6 typesrust-analyzer cargo pyo3 0.28.1 types/::{PyDictrust-analyzer cargo pyo3 0.28.1 types/dict/PyDict#, PyMappingrust-analyzer cargo pyo3 0.28.1 types/mapping/PyMapping#, PySequencerust-analyzer cargo pyo3 0.28.1 types/sequence/PySequence#, PyTuplerust-analyzer cargo pyo3 0.28.1 types/tuple/PyTuple#},
7 IntoPyObjectExtrust-analyzer cargo pyo3 0.28.1 conversion/IntoPyObjectExt#,
8 };
9 use pythonizerust-analyzer cargo pythonize 0.28.0 crate/::{
10 depythonizerust-analyzer cargo pythonize 0.28.0 de/depythonize()., pythonize_customrust-analyzer cargo pythonize 0.28.0 ser/pythonize_custom()., PythonizeListTyperust-analyzer cargo pythonize 0.28.0 ser/PythonizeListType#, PythonizeMappingTyperust-analyzer cargo pythonize 0.28.0 ser/PythonizeMappingType#,
11 PythonizeNamedMappingTyperust-analyzer cargo pythonize 0.28.0 ser/PythonizeNamedMappingType#, PythonizeTypesrust-analyzer cargo pythonize 0.28.0 ser/PythonizeTypes#, PythonizeUnnamedMappingAdapterrust-analyzer cargo pythonize 0.28.0 ser/PythonizeUnnamedMappingAdapter#, Pythonizerrust-analyzer cargo pythonize 0.28.0 ser/Pythonizer#,
12 };
13 use serderust-analyzer cargo serde 1.0.228 crate/::Serializerust-analyzer cargo serde_core 1.0.228 ser/Serialize#;
14 use serde_jsonrust-analyzer cargo serde_json 1.0.149 crate/::{jsonrust-analyzer cargo serde_json 1.0.149 macros/json!, Valuerust-analyzer cargo serde_json 1.0.149 value/Value#};
15
16 #[pyclassrust-analyzer cargo pyo3-macros 0.28.1 pyclass:(sequence)]
17 struct CustomListrust-analyzer cargo pythonize 0.28.0 CustomList# {
18 itemsrust-analyzer cargo pythonize 0.28.0 CustomList#items.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>>,
19 }
20
21 #[pymethodsrust-analyzer cargo pyo3-macros 0.28.1 pymethods:]
22 impl CustomListrust-analyzer cargo pythonize 0.28.0 CustomList# {
23 fn __len__rust-analyzer cargo pythonize 0.28.0 impl#[CustomList]__len__().(&self) -> usize {
24 self.itemsrust-analyzer cargo pythonize 0.28.0 CustomList#items..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()
25 }
26
27 fn __getitem__rust-analyzer cargo pythonize 0.28.0 impl#[CustomList]__getitem__().(&self, idx: isize) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>> {
28 self.itemsrust-analyzer cargo pythonize 0.28.0 CustomList#items.
29 .getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(idx as usize)
30 .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]cloned().()
31 .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| PyIndexErrorrust-analyzer cargo pyo3 0.28.1 exceptions/PyIndexError#::new_errrust-analyzer cargo pyo3 0.28.1 exceptions/impl#[PyIndexError]new_err().(idx))
32 }
33 }
34
35 impl PythonizeListTyperust-analyzer cargo pythonize 0.28.0 ser/PythonizeListType# for CustomListrust-analyzer cargo pythonize 0.28.0 CustomList# {
36 fn create_sequencerust-analyzer cargo pythonize 0.28.0 impl#[CustomList][PythonizeListType]create_sequence().<'py, T, U>(
37 py: Pythonrust-analyzer cargo pyo3 0.28.1 marker/Python#<'py>,
38 elements: impl IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = T, IntoIterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#IntoIter# = U>,
39 ) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<'py, PySequencerust-analyzer cargo pyo3 0.28.1 types/sequence/PySequence#>>
40 where
41 T: IntoPyObjectrust-analyzer cargo pyo3 0.28.1 conversion/IntoPyObject#<'py>,
42 U: ExactSizeIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/exact_size/ExactSizeIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = T>,
43 {
44 let sequence = Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#::newrust-analyzer cargo pyo3 0.28.1 instance/impl#[`Bound<'py, T>`]new().(
45 py,
46 CustomListrust-analyzer cargo pythonize 0.28.0 CustomList# {
47 itemsrust-analyzer cargo pythonize 0.28.0 CustomList#items.: elements
48 .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().()
49 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|item| item.into_py_anyrust-analyzer cargo pyo3 0.28.1 conversion/IntoPyObjectExt#into_py_any().(py))
50 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<_>>()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
51 },
52 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
53
54 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { sequence.cast_into_uncheckedrust-analyzer cargo pyo3 0.28.1 instance/impl#[`Bound<'py, T>`]cast_into_unchecked().() })
55 }
56 }
57
58 struct PythonizeCustomListrust-analyzer cargo pythonize 0.28.0 PythonizeCustomList#;
59 impl<'py> PythonizeTypesrust-analyzer cargo pythonize 0.28.0 ser/PythonizeTypes# for PythonizeCustomListrust-analyzer cargo pythonize 0.28.0 PythonizeCustomList# {
60 type Maprust-analyzer cargo pythonize 0.28.0 impl#[PythonizeCustomList][PythonizeTypes]Map# = PyDictrust-analyzer cargo pyo3 0.28.1 types/dict/PyDict#;
61 type NamedMaprust-analyzer cargo pythonize 0.28.0 impl#[PythonizeCustomList][PythonizeTypes]NamedMap# = PythonizeUnnamedMappingAdapterrust-analyzer cargo pythonize 0.28.0 ser/PythonizeUnnamedMappingAdapter#<PyDictrust-analyzer cargo pyo3 0.28.1 types/dict/PyDict#>;
62 type Listrust-analyzer cargo pythonize 0.28.0 impl#[PythonizeCustomList][PythonizeTypes]List# = CustomListrust-analyzer cargo pythonize 0.28.0 CustomList#;
63 }
64
65 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
66 fn test_custom_listrust-analyzer cargo pythonize 0.28.0 test_custom_list().() {
67 Pythonrust-analyzer cargo pyo3 0.28.1 marker/Python#::attachrust-analyzer cargo pyo3 0.28.1 marker/impl#[`Python<'_>`]attach().(|py| {
68 PySequencerust-analyzer cargo pyo3 0.28.1 types/sequence/PySequence#::registerrust-analyzer cargo pyo3 0.28.1 types/sequence/impl#[PySequence]register().::<CustomListrust-analyzer cargo pythonize 0.28.0 CustomList#>(py).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
69 let serialized = pythonize_customrust-analyzer cargo pythonize 0.28.0 ser/pythonize_custom().::<PythonizeCustomListrust-analyzer cargo pythonize 0.28.0 PythonizeCustomList#, _>(py, &jsonrust-analyzer cargo serde_json 1.0.149 macros/json!!([1, 2, 3])).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
70 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(serialized.is_instance_ofrust-analyzer cargo pyo3 0.28.1 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]is_instance_of().::<CustomListrust-analyzer cargo pythonize 0.28.0 CustomList#>());
71
72 let deserialized: Valuerust-analyzer cargo serde_json 1.0.149 value/Value# = depythonizerust-analyzer cargo pythonize 0.28.0 de/depythonize().(&serialized).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
73 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(deserialized, jsonrust-analyzer cargo serde_json 1.0.149 macros/json!!([1, 2, 3]));
74 })
75 }
76
77 #[pyclassrust-analyzer cargo pyo3-macros 0.28.1 pyclass:(mapping)]
78 struct CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict# {
79 itemsrust-analyzer cargo pythonize 0.28.0 CustomDict#items.: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>>,
80 }
81
82 #[pymethodsrust-analyzer cargo pyo3-macros 0.28.1 pymethods:]
83 impl CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict# {
84 fn __len__rust-analyzer cargo pythonize 0.28.0 impl#[CustomDict]__len__().(&self) -> usize {
85 self.itemsrust-analyzer cargo pythonize 0.28.0 CustomDict#items..lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]len().()
86 }
87
88 fn __getitem__rust-analyzer cargo pythonize 0.28.0 impl#[CustomDict]__getitem__().(&self, key: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>> {
89 self.itemsrust-analyzer cargo pythonize 0.28.0 CustomDict#items.
90 .getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&key)
91 .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]cloned().()
92 .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| PyKeyErrorrust-analyzer cargo pyo3 0.28.1 exceptions/PyKeyError#::new_errrust-analyzer cargo pyo3 0.28.1 exceptions/impl#[PyKeyError]new_err().(key))
93 }
94
95 fn __setitem__rust-analyzer cargo pythonize 0.28.0 impl#[CustomDict]__setitem__().(&mut self, key: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, value: Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>) {
96 self.itemsrust-analyzer cargo pythonize 0.28.0 CustomDict#items..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(key, value);
97 }
98
99 fn keysrust-analyzer cargo pythonize 0.28.0 impl#[CustomDict]keys().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
100 self.itemsrust-analyzer cargo pythonize 0.28.0 CustomDict#items..keysrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]keys().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
101 }
102
103 fn valuesrust-analyzer cargo pythonize 0.28.0 impl#[CustomDict]values().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>> {
104 self.itemsrust-analyzer cargo pythonize 0.28.0 CustomDict#items..valuesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]values().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
105 }
106 }
107
108 impl PythonizeMappingTyperust-analyzer cargo pythonize 0.28.0 ser/PythonizeMappingType# for CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict# {
109 type Builderrust-analyzer cargo pythonize 0.28.0 impl#[CustomDict][PythonizeMappingType]Builder#<'py> = Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<'py, CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#>;
110
111 fn builderrust-analyzer cargo pythonize 0.28.0 impl#[CustomDict][PythonizeMappingType]builder().<'py>(py: Pythonrust-analyzer cargo pyo3 0.28.1 marker/Python#<'py>, len: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<Selfrust-analyzer cargo pythonize 0.28.0 impl#[CustomDict][PythonizeMappingType]::Builderrust-analyzer cargo pythonize 0.28.0 ser/PythonizeMappingType#Builder#<'py>> {
112 Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#::newrust-analyzer cargo pyo3 0.28.1 instance/impl#[`Bound<'py, T>`]new().(
113 py,
114 CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict# {
115 itemsrust-analyzer cargo pythonize 0.28.0 CustomDict#items.: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::with_capacityrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]with_capacity().(len.unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(0)),
116 },
117 )
118 }
119
120 fn push_itemrust-analyzer cargo pythonize 0.28.0 impl#[CustomDict][PythonizeMappingType]push_item().<'py>(
121 builder: &mut Selfrust-analyzer cargo pythonize 0.28.0 impl#[CustomDict][PythonizeMappingType]::Builderrust-analyzer cargo pythonize 0.28.0 ser/PythonizeMappingType#Builder#<'py>,
122 key: Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<'py, PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>,
123 value: Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<'py, PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>,
124 ) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<()> {
125 unsafe { builder.cast_uncheckedrust-analyzer cargo pyo3 0.28.1 instance/impl#[`Bound<'py, T>`]cast_unchecked().::<PyMappingrust-analyzer cargo pyo3 0.28.1 types/mapping/PyMapping#>() }.set_itemrust-analyzer cargo pyo3 0.28.1 types/mapping/impl#[`Bound<'py, PyMapping>`][`PyMappingMethods<'py>`]set_item().(key, value)
126 }
127
128 fn finishrust-analyzer cargo pythonize 0.28.0 impl#[CustomDict][PythonizeMappingType]finish().<'py>(builder: Selfrust-analyzer cargo pythonize 0.28.0 impl#[CustomDict][PythonizeMappingType]::Builderrust-analyzer cargo pythonize 0.28.0 ser/PythonizeMappingType#Builder#<'py>) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<'py, PyMappingrust-analyzer cargo pyo3 0.28.1 types/mapping/PyMapping#>> {
129 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { builder.cast_into_uncheckedrust-analyzer cargo pyo3 0.28.1 instance/impl#[`Bound<'py, T>`]cast_into_unchecked().() })
130 }
131 }
132
133 struct PythonizeCustomDictrust-analyzer cargo pythonize 0.28.0 PythonizeCustomDict#;
134 impl<'py> PythonizeTypesrust-analyzer cargo pythonize 0.28.0 ser/PythonizeTypes# for PythonizeCustomDictrust-analyzer cargo pythonize 0.28.0 PythonizeCustomDict# {
135 type Maprust-analyzer cargo pythonize 0.28.0 impl#[PythonizeCustomDict][PythonizeTypes]Map# = CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#;
136 type NamedMaprust-analyzer cargo pythonize 0.28.0 impl#[PythonizeCustomDict][PythonizeTypes]NamedMap# = PythonizeUnnamedMappingAdapterrust-analyzer cargo pythonize 0.28.0 ser/PythonizeUnnamedMappingAdapter#<CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#>;
137 type Listrust-analyzer cargo pythonize 0.28.0 impl#[PythonizeCustomDict][PythonizeTypes]List# = PyTuplerust-analyzer cargo pyo3 0.28.1 types/tuple/PyTuple#;
138 }
139
140 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
141 fn test_custom_dictrust-analyzer cargo pythonize 0.28.0 test_custom_dict().() {
142 Pythonrust-analyzer cargo pyo3 0.28.1 marker/Python#::attachrust-analyzer cargo pyo3 0.28.1 marker/impl#[`Python<'_>`]attach().(|py| {
143 PyMappingrust-analyzer cargo pyo3 0.28.1 types/mapping/PyMapping#::registerrust-analyzer cargo pyo3 0.28.1 types/mapping/impl#[PyMapping]register().::<CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#>(py).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
144 let serialized =
145 pythonize_customrust-analyzer cargo pythonize 0.28.0 ser/pythonize_custom().::<PythonizeCustomDictrust-analyzer cargo pythonize 0.28.0 PythonizeCustomDict#, _>(py, &jsonrust-analyzer cargo serde_json 1.0.149 macros/json!!({ "hello": 1, "world": 2 }))
146 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
147 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(serialized.is_instance_ofrust-analyzer cargo pyo3 0.28.1 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]is_instance_of().::<CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#>());
148
149 let deserialized: Valuerust-analyzer cargo serde_json 1.0.149 value/Value# = depythonizerust-analyzer cargo pythonize 0.28.0 de/depythonize().(&serialized).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
150 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(deserialized, jsonrust-analyzer cargo serde_json 1.0.149 macros/json!!({ "hello": 1, "world": 2 }));
151 })
152 }
153
154 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
155 fn test_tuplerust-analyzer cargo pythonize 0.28.0 test_tuple().() {
156 Pythonrust-analyzer cargo pyo3 0.28.1 marker/Python#::attachrust-analyzer cargo pyo3 0.28.1 marker/impl#[`Python<'_>`]attach().(|py| {
157 PyMappingrust-analyzer cargo pyo3 0.28.1 types/mapping/PyMapping#::registerrust-analyzer cargo pyo3 0.28.1 types/mapping/impl#[PyMapping]register().::<CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#>(py).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
158 let serialized =
159 pythonize_customrust-analyzer cargo pythonize 0.28.0 ser/pythonize_custom().::<PythonizeCustomDictrust-analyzer cargo pythonize 0.28.0 PythonizeCustomDict#, _>(py, &jsonrust-analyzer cargo serde_json 1.0.149 macros/json!!([1, 2, 3, 4])).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
160 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(serialized.is_instance_ofrust-analyzer cargo pyo3 0.28.1 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]is_instance_of().::<PyTuplerust-analyzer cargo pyo3 0.28.1 types/tuple/PyTuple#>());
161
162 let deserialized: Valuerust-analyzer cargo serde_json 1.0.149 value/Value# = depythonizerust-analyzer cargo pythonize 0.28.0 de/depythonize().(&serialized).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
163 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(deserialized, jsonrust-analyzer cargo serde_json 1.0.149 macros/json!!([1, 2, 3, 4]));
164 })
165 }
166
167 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
168 fn test_pythonizer_can_be_createdrust-analyzer cargo pythonize 0.28.0 test_pythonizer_can_be_created().() {
169 // https://github.com/davidhewitt/pythonize/pull/56
170 Pythonrust-analyzer cargo pyo3 0.28.1 marker/Python#::attachrust-analyzer cargo pyo3 0.28.1 marker/impl#[`Python<'_>`]attach().(|py| {
171 let sample = jsonrust-analyzer cargo serde_json 1.0.149 macros/json!!({ "hello": 1, "world": 2 });
172 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(sample
173 .serializerust-analyzer cargo serde_json 1.0.149 value/ser/impl#[Value][Serialize]serialize().(Pythonizerrust-analyzer cargo pythonize 0.28.0 ser/Pythonizer#::newrust-analyzer cargo pythonize 0.28.0 ser/impl#[`Pythonizer<'py, PythonizeDefault>`]new().(py))
174 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
175 .is_instance_ofrust-analyzer cargo pyo3 0.28.1 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]is_instance_of().::<PyDictrust-analyzer cargo pyo3 0.28.1 types/dict/PyDict#>());
176
177 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(sample
178 .serializerust-analyzer cargo serde_json 1.0.149 value/ser/impl#[Value][Serialize]serialize().(Pythonizerrust-analyzer cargo pythonize 0.28.0 ser/Pythonizer#::customrust-analyzer cargo pythonize 0.28.0 ser/impl#[`Pythonizer<'py, PythonizeDefault>`]custom().::<PythonizeCustomDictrust-analyzer cargo pythonize 0.28.0 PythonizeCustomDict#>(py))
179 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
180 .is_instance_ofrust-analyzer cargo pyo3 0.28.1 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]is_instance_of().::<CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#>());
181 })
182 }
183
184 #[pyclassrust-analyzer cargo pyo3-macros 0.28.1 pyclass:(mapping)]
185 struct NamedCustomDictrust-analyzer cargo pythonize 0.28.0 NamedCustomDict# {
186 namerust-analyzer cargo pythonize 0.28.0 NamedCustomDict#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
187 itemsrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#items.: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>>,
188 }
189
190 #[pymethodsrust-analyzer cargo pyo3-macros 0.28.1 pymethods:]
191 impl NamedCustomDictrust-analyzer cargo pythonize 0.28.0 NamedCustomDict# {
192 fn __len__rust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict]__len__().(&self) -> usize {
193 self.itemsrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#items..lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]len().()
194 }
195
196 fn __getitem__rust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict]__getitem__().(&self, key: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>> {
197 self.itemsrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#items.
198 .getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&key)
199 .clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<&T>`]cloned().()
200 .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| PyKeyErrorrust-analyzer cargo pyo3 0.28.1 exceptions/PyKeyError#::new_errrust-analyzer cargo pyo3 0.28.1 exceptions/impl#[PyKeyError]new_err().(key))
201 }
202
203 fn __setitem__rust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict]__setitem__().(&mut self, key: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, value: Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>) {
204 self.itemsrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#items..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(key, value);
205 }
206
207 fn keysrust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict]keys().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
208 self.itemsrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#items..keysrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]keys().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
209 }
210
211 fn valuesrust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict]values().(&self) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Pyrust-analyzer cargo pyo3 0.28.1 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>> {
212 self.itemsrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#items..valuesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]values().().clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
213 }
214 }
215
216 impl PythonizeNamedMappingTyperust-analyzer cargo pythonize 0.28.0 ser/PythonizeNamedMappingType# for NamedCustomDictrust-analyzer cargo pythonize 0.28.0 NamedCustomDict# {
217 type Builderrust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict][PythonizeNamedMappingType]Builder#<'py> = Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<'py, NamedCustomDictrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#>;
218
219 fn builderrust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict][PythonizeNamedMappingType]builder().<'py>(
220 py: Pythonrust-analyzer cargo pyo3 0.28.1 marker/Python#<'py>,
221 len: usize,
222 name: &'static str,
223 ) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<Selfrust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict][PythonizeNamedMappingType]::Builderrust-analyzer cargo pythonize 0.28.0 ser/PythonizeNamedMappingType#Builder#<'py>> {
224 Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#::newrust-analyzer cargo pyo3 0.28.1 instance/impl#[`Bound<'py, T>`]new().(
225 py,
226 NamedCustomDictrust-analyzer cargo pythonize 0.28.0 NamedCustomDict# {
227 namerust-analyzer cargo pythonize 0.28.0 NamedCustomDict#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().(name),
228 itemsrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#items.: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::with_capacityrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]with_capacity().(len),
229 },
230 )
231 }
232
233 fn push_fieldrust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict][PythonizeNamedMappingType]push_field().<'py>(
234 builder: &mut Selfrust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict][PythonizeNamedMappingType]::Builderrust-analyzer cargo pythonize 0.28.0 ser/PythonizeNamedMappingType#Builder#<'py>,
235 name: Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<'py, pyo3rust-analyzer cargo pyo3 0.28.1 crate/::typesrust-analyzer cargo pyo3 0.28.1 types/::PyStringrust-analyzer cargo pyo3 0.28.1 types/string/PyString#>,
236 value: Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<'py, PyAnyrust-analyzer cargo pyo3 0.28.1 types/any/PyAny#>,
237 ) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<()> {
238 unsafe { builder.cast_uncheckedrust-analyzer cargo pyo3 0.28.1 instance/impl#[`Bound<'py, T>`]cast_unchecked().::<PyMappingrust-analyzer cargo pyo3 0.28.1 types/mapping/PyMapping#>() }.set_itemrust-analyzer cargo pyo3 0.28.1 types/mapping/impl#[`Bound<'py, PyMapping>`][`PyMappingMethods<'py>`]set_item().(name, value)
239 }
240
241 fn finishrust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict][PythonizeNamedMappingType]finish().<'py>(builder: Selfrust-analyzer cargo pythonize 0.28.0 impl#[NamedCustomDict][PythonizeNamedMappingType]::Builderrust-analyzer cargo pythonize 0.28.0 ser/PythonizeNamedMappingType#Builder#<'py>) -> PyResultrust-analyzer cargo pyo3 0.28.1 err/PyResult#<Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<'py, PyMappingrust-analyzer cargo pyo3 0.28.1 types/mapping/PyMapping#>> {
242 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { builder.cast_into_uncheckedrust-analyzer cargo pyo3 0.28.1 instance/impl#[`Bound<'py, T>`]cast_into_unchecked().() })
243 }
244 }
245
246 struct PythonizeNamedCustomDictrust-analyzer cargo pythonize 0.28.0 PythonizeNamedCustomDict#;
247 impl<'py> PythonizeTypesrust-analyzer cargo pythonize 0.28.0 ser/PythonizeTypes# for PythonizeNamedCustomDictrust-analyzer cargo pythonize 0.28.0 PythonizeNamedCustomDict# {
248 type Maprust-analyzer cargo pythonize 0.28.0 impl#[PythonizeNamedCustomDict][PythonizeTypes]Map# = CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#;
249 type NamedMaprust-analyzer cargo pythonize 0.28.0 impl#[PythonizeNamedCustomDict][PythonizeTypes]NamedMap# = NamedCustomDictrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#;
250 type Listrust-analyzer cargo pythonize 0.28.0 impl#[PythonizeNamedCustomDict][PythonizeTypes]List# = PyTuplerust-analyzer cargo pyo3 0.28.1 types/tuple/PyTuple#;
251 }
252
253 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Serializerust-analyzer cargo serde_derive 1.0.228 Serialize:)]
254 struct Structrust-analyzer cargo pythonize 0.28.0 Struct# {
255 hellorust-analyzer cargo pythonize 0.28.0 Struct#hello.: u8,
256 worldrust-analyzer cargo pythonize 0.28.0 Struct#world.: i8,
257 }
258
259 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
260 fn test_custom_unnamed_dictrust-analyzer cargo pythonize 0.28.0 test_custom_unnamed_dict().() {
261 Pythonrust-analyzer cargo pyo3 0.28.1 marker/Python#::attachrust-analyzer cargo pyo3 0.28.1 marker/impl#[`Python<'_>`]attach().(|py| {
262 PyMappingrust-analyzer cargo pyo3 0.28.1 types/mapping/PyMapping#::registerrust-analyzer cargo pyo3 0.28.1 types/mapping/impl#[PyMapping]register().::<CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#>(py).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
263 let serialized =
264 pythonize_customrust-analyzer cargo pythonize 0.28.0 ser/pythonize_custom().::<PythonizeCustomDictrust-analyzer cargo pythonize 0.28.0 PythonizeCustomDict#, _>(py, &Structrust-analyzer cargo pythonize 0.28.0 Struct# { hellorust-analyzer cargo pythonize 0.28.0 Struct#hello.: 1, worldrust-analyzer cargo pythonize 0.28.0 Struct#world.: 2 }).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
265 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(serialized.is_instance_ofrust-analyzer cargo pyo3 0.28.1 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]is_instance_of().::<CustomDictrust-analyzer cargo pythonize 0.28.0 CustomDict#>());
266
267 let deserialized: Valuerust-analyzer cargo serde_json 1.0.149 value/Value# = depythonizerust-analyzer cargo pythonize 0.28.0 de/depythonize().(&serialized).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
268 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(deserialized, jsonrust-analyzer cargo serde_json 1.0.149 macros/json!!({ "hello": 1, "world": 2 }));
269 })
270 }
271
272 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
273 fn test_custom_named_dictrust-analyzer cargo pythonize 0.28.0 test_custom_named_dict().() {
274 Pythonrust-analyzer cargo pyo3 0.28.1 marker/Python#::attachrust-analyzer cargo pyo3 0.28.1 marker/impl#[`Python<'_>`]attach().(|py| {
275 PyMappingrust-analyzer cargo pyo3 0.28.1 types/mapping/PyMapping#::registerrust-analyzer cargo pyo3 0.28.1 types/mapping/impl#[PyMapping]register().::<NamedCustomDictrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#>(py).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
276 let serialized =
277 pythonize_customrust-analyzer cargo pythonize 0.28.0 ser/pythonize_custom().::<PythonizeNamedCustomDictrust-analyzer cargo pythonize 0.28.0 PythonizeNamedCustomDict#, _>(py, &Structrust-analyzer cargo pythonize 0.28.0 Struct# { hellorust-analyzer cargo pythonize 0.28.0 Struct#hello.: 1, worldrust-analyzer cargo pythonize 0.28.0 Struct#world.: 2 })
278 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
279 let named: Boundrust-analyzer cargo pyo3 0.28.1 instance/Bound#<NamedCustomDictrust-analyzer cargo pythonize 0.28.0 NamedCustomDict#> = serialized.extractrust-analyzer cargo pyo3 0.28.1 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]extract().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
280 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(named.borrowrust-analyzer cargo pyo3 0.28.1 instance/impl#[`Bound<'py, T>`]borrow().().namerust-analyzer cargo pythonize 0.28.0 NamedCustomDict#name., "Struct");
281
282 let deserialized: Valuerust-analyzer cargo serde_json 1.0.149 value/Value# = depythonizerust-analyzer cargo pythonize 0.28.0 de/depythonize().(&serialized).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
283 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(deserialized, jsonrust-analyzer cargo serde_json 1.0.149 macros/json!!({ "hello": 1, "world": 2 }));
284 })
285 }