Skip to content

Commit a64cef6

Browse files
committed
asn1: Use PyStringMethods::to_str when possible
Signed-off-by: Facundo Tuesca <[email protected]>
1 parent 03a854d commit a64cef6

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/rust/src/declarative_asn1/encode.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,27 +138,33 @@ impl asn1::Asn1Writable for AnnotatedTypeObject<'_> {
138138
let val: &pyo3::Bound<'_, PrintableString> = value
139139
.cast()
140140
.map_err(|_| asn1::WriteError::AllocationError)?;
141-
let inner_str = val
142-
.get()
143-
.inner
144-
.to_cow(py)
145-
.map_err(|_| asn1::WriteError::AllocationError)?;
141+
let inner_str = &val.get().inner;
142+
143+
#[cfg(Py_3_10)]
144+
let rust_str = inner_str.to_str(py);
145+
#[cfg(not(Py_3_10))]
146+
let rust_str = inner_str.to_cow(py);
147+
148+
let rust_str = rust_str.map_err(|_| asn1::WriteError::AllocationError)?;
146149
let printable_string: asn1::PrintableString<'_> =
147-
asn1::PrintableString::new(&inner_str)
150+
asn1::PrintableString::new(&rust_str)
148151
.ok_or(asn1::WriteError::AllocationError)?;
149152
write_value(writer, &printable_string, encoding)
150153
}
151154
Type::IA5String() => {
152155
let val: &pyo3::Bound<'_, IA5String> = value
153156
.cast()
154157
.map_err(|_| asn1::WriteError::AllocationError)?;
155-
let inner_str = val
156-
.get()
157-
.inner
158-
.to_cow(py)
159-
.map_err(|_| asn1::WriteError::AllocationError)?;
158+
let inner_str = &val.get().inner;
159+
160+
#[cfg(Py_3_10)]
161+
let rust_str = inner_str.to_str(py);
162+
#[cfg(not(Py_3_10))]
163+
let rust_str = inner_str.to_cow(py);
164+
165+
let rust_str = rust_str.map_err(|_| asn1::WriteError::AllocationError)?;
160166
let ia5_string: asn1::IA5String<'_> =
161-
asn1::IA5String::new(&inner_str).ok_or(asn1::WriteError::AllocationError)?;
167+
asn1::IA5String::new(&rust_str).ok_or(asn1::WriteError::AllocationError)?;
162168
write_value(writer, &ia5_string, encoding)
163169
}
164170
Type::UtcTime() => {

src/rust/src/declarative_asn1/types.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,12 @@ impl PrintableString {
143143
#[new]
144144
#[pyo3(signature = (inner,))]
145145
fn new(py: pyo3::Python<'_>, inner: pyo3::Py<pyo3::types::PyString>) -> pyo3::PyResult<Self> {
146-
if Asn1PrintableString::new(&inner.to_cow(py)?).is_none() {
146+
#[cfg(Py_3_10)]
147+
let rust_str = &inner.to_str(py)?;
148+
#[cfg(not(Py_3_10))]
149+
let rust_str = &inner.to_cow(py)?;
150+
151+
if Asn1PrintableString::new(rust_str).is_none() {
147152
return Err(pyo3::exceptions::PyValueError::new_err(format!(
148153
"invalid PrintableString: {inner}"
149154
)));
@@ -176,7 +181,12 @@ impl IA5String {
176181
#[new]
177182
#[pyo3(signature = (inner,))]
178183
fn new(py: pyo3::Python<'_>, inner: pyo3::Py<pyo3::types::PyString>) -> pyo3::PyResult<Self> {
179-
if Asn1IA5String::new(&inner.to_cow(py)?).is_none() {
184+
#[cfg(Py_3_10)]
185+
let rust_str = &inner.to_str(py)?;
186+
#[cfg(not(Py_3_10))]
187+
let rust_str = &inner.to_cow(py)?;
188+
189+
if Asn1IA5String::new(rust_str).is_none() {
180190
return Err(pyo3::exceptions::PyValueError::new_err(format!(
181191
"invalid IA5String: {inner}"
182192
)));

0 commit comments

Comments
 (0)