Skip to content

Commit 38d7a05

Browse files
committed
Add short docs for the unnamed mapping adapter type
1 parent 893a1bc commit 38d7a05

4 files changed

Lines changed: 18 additions & 11 deletions

File tree

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ pub use crate::de::{depythonize, Depythonizer};
4646
pub use crate::error::{PythonizeError, Result};
4747
pub use crate::ser::{
4848
pythonize, pythonize_custom, PythonizeDefault, PythonizeListType, PythonizeMappingType,
49-
PythonizeNamedMappingType, PythonizeTypes, PythonizeUnnamedMappingWrapper, Pythonizer,
49+
PythonizeNamedMappingType, PythonizeTypes, PythonizeUnnamedMappingAdapter, Pythonizer,
5050
};

src/ser.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,19 @@ impl<'py> PythonizeMappingType<'py> for PyDict {
9292
}
9393
}
9494

95-
pub struct PythonizeUnnamedMappingWrapper<'py, T: PythonizeMappingType<'py>> {
95+
/// Adapter type to use an unnamed mapping type, i.e. one that implements
96+
/// [`PythonizeMappingType`], as a named mapping type, i.e. one that implements
97+
/// [`PythonizeNamedMappingType`]. The adapter simply drops the provided name.
98+
///
99+
/// This adapter is commonly applied to use the same unnamed mapping type for
100+
/// both [`PythonizeTypes::Map`] and [`PythonizeTypes::NamedMap`] while only
101+
/// implementing [`PythonizeMappingType`].
102+
pub struct PythonizeUnnamedMappingAdapter<'py, T: PythonizeMappingType<'py>> {
96103
unnamed: T,
97104
_marker: PhantomData<&'py ()>,
98105
}
99106

100-
impl<'py, T: PythonizeMappingType<'py>> PythonizeUnnamedMappingWrapper<'py, T> {
107+
impl<'py, T: PythonizeMappingType<'py>> PythonizeUnnamedMappingAdapter<'py, T> {
101108
#[must_use]
102109
pub fn new(unnamed: T) -> Self {
103110
Self {
@@ -112,14 +119,14 @@ impl<'py, T: PythonizeMappingType<'py>> PythonizeUnnamedMappingWrapper<'py, T> {
112119
}
113120
}
114121

115-
impl<'py, T: PythonizeMappingType<'py>> From<T> for PythonizeUnnamedMappingWrapper<'py, T> {
122+
impl<'py, T: PythonizeMappingType<'py>> From<T> for PythonizeUnnamedMappingAdapter<'py, T> {
116123
fn from(value: T) -> Self {
117124
Self::new(value)
118125
}
119126
}
120127

121128
impl<'py, T: PythonizeMappingType<'py>> PythonizeNamedMappingType<'py>
122-
for PythonizeUnnamedMappingWrapper<'py, T>
129+
for PythonizeUnnamedMappingAdapter<'py, T>
123130
{
124131
type Builder = <T as PythonizeMappingType<'py>>::Builder;
125132

@@ -173,7 +180,7 @@ pub struct PythonizeDefault;
173180

174181
impl<'py> PythonizeTypes<'py> for PythonizeDefault {
175182
type Map = PyDict;
176-
type NamedMap = PythonizeUnnamedMappingWrapper<'py, PyDict>;
183+
type NamedMap = PythonizeUnnamedMappingAdapter<'py, PyDict>;
177184
type List = PyList;
178185
}
179186

tests/test_custom_types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use pyo3::{
77
};
88
use pythonize::{
99
depythonize, pythonize_custom, PythonizeListType, PythonizeMappingType, PythonizeTypes,
10-
PythonizeUnnamedMappingWrapper, Pythonizer,
10+
PythonizeUnnamedMappingAdapter, Pythonizer,
1111
};
1212
use serde::Serialize;
1313
use serde_json::{json, Value};
@@ -58,7 +58,7 @@ impl PythonizeListType for CustomList {
5858
struct PythonizeCustomList;
5959
impl<'py> PythonizeTypes<'py> for PythonizeCustomList {
6060
type Map = PyDict;
61-
type NamedMap = PythonizeUnnamedMappingWrapper<'py, PyDict>;
61+
type NamedMap = PythonizeUnnamedMappingAdapter<'py, PyDict>;
6262
type List = CustomList;
6363
}
6464

@@ -133,7 +133,7 @@ impl<'py> PythonizeMappingType<'py> for CustomDict {
133133
struct PythonizeCustomDict;
134134
impl<'py> PythonizeTypes<'py> for PythonizeCustomDict {
135135
type Map = CustomDict;
136-
type NamedMap = PythonizeUnnamedMappingWrapper<'py, CustomDict>;
136+
type NamedMap = PythonizeUnnamedMappingAdapter<'py, CustomDict>;
137137
type List = PyTuple;
138138
}
139139

tests/test_with_serde_path_to_err.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use pyo3::{
44
prelude::*,
55
types::{PyDict, PyList},
66
};
7-
use pythonize::{PythonizeTypes, PythonizeUnnamedMappingWrapper};
7+
use pythonize::{PythonizeTypes, PythonizeUnnamedMappingAdapter};
88
use serde::{Deserialize, Serialize};
99

1010
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
@@ -15,7 +15,7 @@ struct Root<T> {
1515

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

0 commit comments

Comments
 (0)