Skip to content

Commit 9b56559

Browse files
committed
did I gat it right?
1 parent 61135b7 commit 9b56559

3 files changed

Lines changed: 61 additions & 56 deletions

File tree

src/ser.rs

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::error::{PythonizeError, Result};
1313
/// Trait for types which can represent a Python mapping
1414
pub trait PythonizeMappingType {
1515
/// Builder type for Python mappings
16-
type Builder<'py>;
16+
type Builder<'py>: 'py;
1717

1818
/// Create a builder for a Python mapping
1919
fn builder<'py>(py: Python<'py>, len: Option<usize>) -> PyResult<Self::Builder<'py>>;
@@ -31,22 +31,26 @@ pub trait PythonizeMappingType {
3131

3232
// TODO: move 'py lifetime into builder once GATs are available in MSRV
3333
/// Trait for types which can represent a Python mapping and have a name
34-
pub trait PythonizeNamedMappingType<'py> {
34+
pub trait PythonizeNamedMappingType {
3535
/// Builder type for Python mappings with a name
36-
type Builder;
36+
type Builder<'py>: 'py;
3737

3838
/// Create a builder for a Python mapping with a name
39-
fn builder(py: Python<'py>, len: usize, name: &'static str) -> PyResult<Self::Builder>;
39+
fn builder<'py>(
40+
py: Python<'py>,
41+
len: usize,
42+
name: &'static str,
43+
) -> PyResult<Self::Builder<'py>>;
4044

4145
/// Adds the field to the named mapping being built
42-
fn push_field(
43-
builder: &mut Self::Builder,
46+
fn push_field<'py>(
47+
builder: &mut Self::Builder<'py>,
4448
name: Bound<'py, PyString>,
4549
value: Bound<'py, PyAny>,
4650
) -> PyResult<()>;
4751

4852
/// Build the Python mapping
49-
fn finish(builder: Self::Builder) -> PyResult<Bound<'py, PyMapping>>;
53+
fn finish<'py>(builder: Self::Builder<'py>) -> PyResult<Bound<'py, PyMapping>>;
5054
}
5155

5256
/// Trait for types which can represent a Python sequence
@@ -63,11 +67,11 @@ pub trait PythonizeListType: Sized {
6367

6468
// TODO: remove 'py lifetime once GATs are available in MSRV
6569
/// Custom types for serialization
66-
pub trait PythonizeTypes<'py> {
70+
pub trait PythonizeTypes {
6771
/// Python map type (should be representable as python mapping)
6872
type Map: PythonizeMappingType;
6973
/// Python (struct-like) named map type (should be representable as python mapping)
70-
type NamedMap: PythonizeNamedMappingType<'py>;
74+
type NamedMap: PythonizeNamedMappingType;
7175
/// Python sequence type (should be representable as python sequence)
7276
type List: PythonizeListType;
7377
}
@@ -98,31 +102,32 @@ impl PythonizeMappingType for PyDict {
98102
///
99103
/// This adapter is commonly applied to use the same unnamed mapping type for
100104
/// both [`PythonizeTypes::Map`] and [`PythonizeTypes::NamedMap`] while only
101-
/// implementing [`PythonizeMappingType`].
102-
pub struct PythonizeUnnamedMappingAdapter<'py, T: PythonizeMappingType> {
105+
// /// implementing [`PythonizeMappingType`].
106+
pub struct PythonizeUnnamedMappingAdapter<T: PythonizeMappingType> {
103107
_unnamed: T,
104-
_marker: PhantomData<&'py ()>,
105108
}
106109

107-
impl<'py, T: PythonizeMappingType> PythonizeNamedMappingType<'py>
108-
for PythonizeUnnamedMappingAdapter<'py, T>
109-
{
110-
type Builder = <T as PythonizeMappingType>::Builder<'py>;
110+
impl<T: PythonizeMappingType> PythonizeNamedMappingType for PythonizeUnnamedMappingAdapter<T> {
111+
type Builder<'py> = T::Builder<'py>;
111112

112-
fn builder(py: Python<'py>, len: usize, _name: &'static str) -> PyResult<Self::Builder> {
113-
<T as PythonizeMappingType>::builder(py, Some(len))
113+
fn builder<'py>(
114+
py: Python<'py>,
115+
len: usize,
116+
_name: &'static str,
117+
) -> PyResult<Self::Builder<'py>> {
118+
T::builder(py, Some(len))
114119
}
115120

116-
fn push_field(
117-
builder: &mut Self::Builder,
121+
fn push_field<'py>(
122+
builder: &mut Self::Builder<'py>,
118123
name: Bound<'py, PyString>,
119124
value: Bound<'py, PyAny>,
120125
) -> PyResult<()> {
121-
<T as PythonizeMappingType>::push_item(builder, name.into_any(), value)
126+
T::push_item(builder, name.into_any(), value)
122127
}
123128

124-
fn finish(builder: Self::Builder) -> PyResult<Bound<'py, PyMapping>> {
125-
<T as PythonizeMappingType>::finish(builder)
129+
fn finish<'py>(builder: Self::Builder<'py>) -> PyResult<Bound<'py, PyMapping>> {
130+
T::finish(builder)
126131
}
127132
}
128133

@@ -154,9 +159,9 @@ impl PythonizeListType for PyTuple {
154159

155160
pub struct PythonizeDefault;
156161

157-
impl<'py> PythonizeTypes<'py> for PythonizeDefault {
162+
impl PythonizeTypes for PythonizeDefault {
158163
type Map = PyDict;
159-
type NamedMap = PythonizeUnnamedMappingAdapter<'py, PyDict>;
164+
type NamedMap = PythonizeUnnamedMappingAdapter<PyDict>;
160165
type List = PyList;
161166
}
162167

@@ -173,7 +178,7 @@ where
173178
pub fn pythonize_custom<'py, P, T>(py: Python<'py>, value: &T) -> Result<Bound<'py, PyAny>>
174179
where
175180
T: ?Sized + Serialize,
176-
P: PythonizeTypes<'py>,
181+
P: PythonizeTypes,
177182
{
178183
value.serialize(Pythonizer::custom::<P>(py))
179184
}
@@ -221,28 +226,28 @@ pub struct PythonTupleVariantSerializer<'py, P> {
221226
}
222227

223228
#[doc(hidden)]
224-
pub struct PythonStructVariantSerializer<'py, P: PythonizeTypes<'py>> {
229+
pub struct PythonStructVariantSerializer<'py, P: PythonizeTypes> {
225230
name: &'static str,
226231
variant: &'static str,
227232
inner: PythonStructDictSerializer<'py, P>,
228233
}
229234

230235
#[doc(hidden)]
231-
pub struct PythonStructDictSerializer<'py, P: PythonizeTypes<'py>> {
236+
pub struct PythonStructDictSerializer<'py, P: PythonizeTypes> {
232237
py: Python<'py>,
233-
builder: <P::NamedMap as PythonizeNamedMappingType<'py>>::Builder,
238+
builder: <P::NamedMap as PythonizeNamedMappingType>::Builder<'py>,
234239
_types: PhantomData<P>,
235240
}
236241

237242
#[doc(hidden)]
238-
pub struct PythonMapSerializer<'py, P: PythonizeTypes<'py>> {
243+
pub struct PythonMapSerializer<'py, P: PythonizeTypes> {
239244
py: Python<'py>,
240245
builder: <P::Map as PythonizeMappingType>::Builder<'py>,
241246
key: Option<Bound<'py, PyAny>>,
242247
_types: PhantomData<P>,
243248
}
244249

245-
impl<'py, P: PythonizeTypes<'py>> Pythonizer<'py, P> {
250+
impl<'py, P: PythonizeTypes> Pythonizer<'py, P> {
246251
/// The default implementation for serialisation functions.
247252
#[inline]
248253
fn serialise_default<T>(self, v: T) -> Result<Bound<'py, PyAny>>
@@ -256,7 +261,7 @@ impl<'py, P: PythonizeTypes<'py>> Pythonizer<'py, P> {
256261
}
257262
}
258263

259-
impl<'py, P: PythonizeTypes<'py>> ser::Serializer for Pythonizer<'py, P> {
264+
impl<'py, P: PythonizeTypes> ser::Serializer for Pythonizer<'py, P> {
260265
type Ok = Bound<'py, PyAny>;
261266
type Error = PythonizeError;
262267
type SerializeSeq = PythonCollectionSerializer<'py, P>;
@@ -464,7 +469,7 @@ impl<'py, P: PythonizeTypes<'py>> ser::Serializer for Pythonizer<'py, P> {
464469
}
465470
}
466471

467-
impl<'py, P: PythonizeTypes<'py>> ser::SerializeSeq for PythonCollectionSerializer<'py, P> {
472+
impl<'py, P: PythonizeTypes> ser::SerializeSeq for PythonCollectionSerializer<'py, P> {
468473
type Ok = Bound<'py, PyAny>;
469474
type Error = PythonizeError;
470475

@@ -482,7 +487,7 @@ impl<'py, P: PythonizeTypes<'py>> ser::SerializeSeq for PythonCollectionSerializ
482487
}
483488
}
484489

485-
impl<'py, P: PythonizeTypes<'py>> ser::SerializeTuple for PythonCollectionSerializer<'py, P> {
490+
impl<'py, P: PythonizeTypes> ser::SerializeTuple for PythonCollectionSerializer<'py, P> {
486491
type Ok = Bound<'py, PyAny>;
487492
type Error = PythonizeError;
488493

@@ -498,7 +503,7 @@ impl<'py, P: PythonizeTypes<'py>> ser::SerializeTuple for PythonCollectionSerial
498503
}
499504
}
500505

501-
impl<'py, P: PythonizeTypes<'py>> ser::SerializeTupleStruct for PythonCollectionSerializer<'py, P> {
506+
impl<'py, P: PythonizeTypes> ser::SerializeTupleStruct for PythonCollectionSerializer<'py, P> {
502507
type Ok = Bound<'py, PyAny>;
503508
type Error = PythonizeError;
504509

@@ -514,9 +519,7 @@ impl<'py, P: PythonizeTypes<'py>> ser::SerializeTupleStruct for PythonCollection
514519
}
515520
}
516521

517-
impl<'py, P: PythonizeTypes<'py>> ser::SerializeTupleVariant
518-
for PythonTupleVariantSerializer<'py, P>
519-
{
522+
impl<'py, P: PythonizeTypes> ser::SerializeTupleVariant for PythonTupleVariantSerializer<'py, P> {
520523
type Ok = Bound<'py, PyAny>;
521524
type Error = PythonizeError;
522525

@@ -538,7 +541,7 @@ impl<'py, P: PythonizeTypes<'py>> ser::SerializeTupleVariant
538541
}
539542
}
540543

541-
impl<'py, P: PythonizeTypes<'py>> ser::SerializeMap for PythonMapSerializer<'py, P> {
544+
impl<'py, P: PythonizeTypes> ser::SerializeMap for PythonMapSerializer<'py, P> {
542545
type Ok = Bound<'py, PyAny>;
543546
type Error = PythonizeError;
544547

@@ -569,7 +572,7 @@ impl<'py, P: PythonizeTypes<'py>> ser::SerializeMap for PythonMapSerializer<'py,
569572
}
570573
}
571574

572-
impl<'py, P: PythonizeTypes<'py>> ser::SerializeStruct for PythonStructDictSerializer<'py, P> {
575+
impl<'py, P: PythonizeTypes> ser::SerializeStruct for PythonStructDictSerializer<'py, P> {
573576
type Ok = Bound<'py, PyAny>;
574577
type Error = PythonizeError;
575578

@@ -590,9 +593,7 @@ impl<'py, P: PythonizeTypes<'py>> ser::SerializeStruct for PythonStructDictSeria
590593
}
591594
}
592595

593-
impl<'py, P: PythonizeTypes<'py>> ser::SerializeStructVariant
594-
for PythonStructVariantSerializer<'py, P>
595-
{
596+
impl<'py, P: PythonizeTypes> ser::SerializeStructVariant for PythonStructVariantSerializer<'py, P> {
596597
type Ok = Bound<'py, PyAny>;
597598
type Error = PythonizeError;
598599

tests/test_custom_types.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ impl PythonizeListType for CustomList {
5858
}
5959

6060
struct PythonizeCustomList;
61-
impl<'py> PythonizeTypes<'py> for PythonizeCustomList {
61+
impl<'py> PythonizeTypes for PythonizeCustomList {
6262
type Map = PyDict;
63-
type NamedMap = PythonizeUnnamedMappingAdapter<'py, PyDict>;
63+
type NamedMap = PythonizeUnnamedMappingAdapter<PyDict>;
6464
type List = CustomList;
6565
}
6666

@@ -133,9 +133,9 @@ impl PythonizeMappingType for CustomDict {
133133
}
134134

135135
struct PythonizeCustomDict;
136-
impl<'py> PythonizeTypes<'py> for PythonizeCustomDict {
136+
impl<'py> PythonizeTypes for PythonizeCustomDict {
137137
type Map = CustomDict;
138-
type NamedMap = PythonizeUnnamedMappingAdapter<'py, CustomDict>;
138+
type NamedMap = PythonizeUnnamedMappingAdapter<CustomDict>;
139139
type List = PyTuple;
140140
}
141141

@@ -215,10 +215,14 @@ impl NamedCustomDict {
215215
}
216216
}
217217

218-
impl<'py> PythonizeNamedMappingType<'py> for NamedCustomDict {
219-
type Builder = Bound<'py, NamedCustomDict>;
218+
impl PythonizeNamedMappingType for NamedCustomDict {
219+
type Builder<'py> = Bound<'py, NamedCustomDict>;
220220

221-
fn builder(py: Python<'py>, len: usize, name: &'static str) -> PyResult<Self::Builder> {
221+
fn builder<'py>(
222+
py: Python<'py>,
223+
len: usize,
224+
name: &'static str,
225+
) -> PyResult<Self::Builder<'py>> {
222226
Bound::new(
223227
py,
224228
NamedCustomDict {
@@ -228,21 +232,21 @@ impl<'py> PythonizeNamedMappingType<'py> for NamedCustomDict {
228232
)
229233
}
230234

231-
fn push_field(
232-
builder: &mut Self::Builder,
235+
fn push_field<'py>(
236+
builder: &mut Self::Builder<'py>,
233237
name: Bound<'py, pyo3::types::PyString>,
234238
value: Bound<'py, PyAny>,
235239
) -> PyResult<()> {
236240
unsafe { builder.downcast_unchecked::<PyMapping>() }.set_item(name, value)
237241
}
238242

239-
fn finish(builder: Self::Builder) -> PyResult<Bound<'py, PyMapping>> {
243+
fn finish<'py>(builder: Self::Builder<'py>) -> PyResult<Bound<'py, PyMapping>> {
240244
Ok(unsafe { builder.into_any().downcast_into_unchecked() })
241245
}
242246
}
243247

244248
struct PythonizeNamedCustomDict;
245-
impl<'py> PythonizeTypes<'py> for PythonizeNamedCustomDict {
249+
impl<'py> PythonizeTypes for PythonizeNamedCustomDict {
246250
type Map = CustomDict;
247251
type NamedMap = NamedCustomDict;
248252
type List = PyTuple;

tests/test_with_serde_path_to_err.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ struct Root<T> {
1313
root_map: BTreeMap<String, Nested<T>>,
1414
}
1515

16-
impl<'py, T> PythonizeTypes<'py> for Root<T> {
16+
impl<'py, T> PythonizeTypes for Root<T> {
1717
type Map = PyDict;
18-
type NamedMap = PythonizeUnnamedMappingAdapter<'py, PyDict>;
18+
type NamedMap = PythonizeUnnamedMappingAdapter<PyDict>;
1919
type List = PyList;
2020
}
2121

0 commit comments

Comments
 (0)