Skip to content

Commit 30f6f55

Browse files
authored
tidy up some uses of Bound<'_, PyString>::to_str (#1544)
1 parent 6472887 commit 30f6f55

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

src/serializers/infer.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub(crate) fn infer_to_python_known(
131131
v.into_py(py)
132132
}
133133
ObType::Decimal => value.to_string().into_py(py),
134-
ObType::StrSubclass => value.downcast::<PyString>()?.to_str()?.into_py(py),
134+
ObType::StrSubclass => PyString::new_bound(py, value.downcast::<PyString>()?.to_str()?).into(),
135135
ObType::Bytes => extra
136136
.config
137137
.bytes_mode
@@ -609,16 +609,11 @@ pub(crate) fn infer_json_key_known<'a>(
609609
}
610610
ObType::Decimal => Ok(Cow::Owned(key.to_string())),
611611
ObType::Bool => super::type_serializers::simple::bool_json_key(key),
612-
ObType::Str | ObType::StrSubclass => {
613-
let py_str = key.downcast::<PyString>()?;
614-
Ok(Cow::Owned(py_str.to_str()?.to_string()))
615-
}
612+
ObType::Str | ObType::StrSubclass => key.downcast::<PyString>()?.to_cow(),
616613
ObType::Bytes => extra
617614
.config
618615
.bytes_mode
619-
.bytes_to_string(key.py(), key.downcast::<PyBytes>()?.as_bytes())
620-
// FIXME it would be nice to have a "PyCow" which carries ownership of the Python type too
621-
.map(|s| Cow::Owned(s.into_owned())),
616+
.bytes_to_string(key.py(), key.downcast::<PyBytes>()?.as_bytes()),
622617
ObType::Bytearray => {
623618
let py_byte_array = key.downcast::<PyByteArray>()?;
624619
// Safety: the GIL is held while serialize_bytes is running; it doesn't run

src/serializers/type_serializers/format.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,15 @@ impl BuildSerializer for FormatSerializer {
7070
) -> PyResult<CombinedSerializer> {
7171
let py = schema.py();
7272
let formatting_string: Bound<'_, PyString> = schema.get_as_req(intern!(py, "formatting_string"))?;
73-
let formatting_string = formatting_string.to_str()?;
74-
if formatting_string.is_empty() {
73+
if formatting_string.is_empty()? {
7574
ToStringSerializer::build(schema, config, definitions)
7675
} else {
7776
Ok(Self {
7877
format_func: py
7978
.import_bound(intern!(py, "builtins"))?
8079
.getattr(intern!(py, "format"))?
8180
.into_py(py),
82-
formatting_string: PyString::new_bound(py, formatting_string).into(),
81+
formatting_string: formatting_string.unbind(),
8382
when_used: WhenUsed::new(schema, WhenUsed::JsonUnlessNone)?,
8483
}
8584
.into())

0 commit comments

Comments
 (0)