Skip to content

Commit ecbc893

Browse files
authored
fix EitherString::as_cow implementation (#1794)
1 parent d0dbb09 commit ecbc893

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

src/input/input_abstract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub trait Input<'py>: fmt::Debug {
8585

8686
fn validate_dataclass_args<'a>(&'a self, dataclass_name: &str) -> ValResult<Self::Arguments<'a>>;
8787

88-
fn validate_str(&self, strict: bool, coerce_numbers_to_str: bool) -> ValMatch<EitherString<'_>>;
88+
fn validate_str(&self, strict: bool, coerce_numbers_to_str: bool) -> ValMatch<EitherString<'_, 'py>>;
8989

9090
fn validate_bytes<'a>(&'a self, strict: bool, mode: ValBytesMode) -> ValMatch<EitherBytes<'a, 'py>>;
9191

@@ -103,7 +103,7 @@ pub trait Input<'py>: fmt::Debug {
103103

104104
/// Extract a String from the input, only allowing exact
105105
/// matches for a String (no subclasses)
106-
fn exact_str(&self) -> ValResult<EitherString<'_>> {
106+
fn exact_str(&self) -> ValResult<EitherString<'_, 'py>> {
107107
self.validate_str(true, false).and_then(|val_match| {
108108
val_match
109109
.require_exact()

src/input/input_json.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
107107
}
108108
}
109109

110-
fn validate_str(&self, strict: bool, coerce_numbers_to_str: bool) -> ValResult<ValidationMatch<EitherString<'_>>> {
110+
fn validate_str(
111+
&self,
112+
strict: bool,
113+
coerce_numbers_to_str: bool,
114+
) -> ValResult<ValidationMatch<EitherString<'_, 'py>>> {
111115
// Justification for `strict` instead of `exact` is that in JSON strings can also
112116
// represent other datatypes such as UUID and date more exactly, so string is a
113117
// converting input
@@ -163,7 +167,7 @@ impl<'py, 'data> Input<'py> for JsonValue<'data> {
163167
}
164168
}
165169

166-
fn exact_str(&self) -> ValResult<EitherString<'_>> {
170+
fn exact_str(&self) -> ValResult<EitherString<'_, 'py>> {
167171
match self {
168172
JsonValue::Str(s) => Ok(s.as_ref().into()),
169173
_ => Err(ValError::new(ErrorTypeDefaults::StringType, self)),
@@ -414,7 +418,7 @@ impl<'py> Input<'py> for str {
414418
&self,
415419
_strict: bool,
416420
_coerce_numbers_to_str: bool,
417-
) -> ValResult<ValidationMatch<EitherString<'_>>> {
421+
) -> ValResult<ValidationMatch<EitherString<'_, 'py>>> {
418422
// Justification for `strict` instead of `exact` is that in JSON strings can also
419423
// represent other datatypes such as UUID and date more exactly, so string is a
420424
// converting input

src/input/input_python.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
163163
}
164164
}
165165

166-
fn validate_str(&self, strict: bool, coerce_numbers_to_str: bool) -> ValResult<ValidationMatch<EitherString<'_>>> {
166+
fn validate_str(
167+
&self,
168+
strict: bool,
169+
coerce_numbers_to_str: bool,
170+
) -> ValResult<ValidationMatch<EitherString<'_, 'py>>> {
167171
if let Ok(py_str) = self.downcast_exact::<PyString>() {
168172
return Ok(ValidationMatch::exact(py_str.clone().into()));
169173
} else if let Ok(py_str) = self.downcast::<PyString>() {
@@ -310,7 +314,7 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
310314
}
311315
}
312316

313-
fn exact_str(&self) -> ValResult<EitherString<'_>> {
317+
fn exact_str(&self) -> ValResult<EitherString<'_, 'py>> {
314318
if let Ok(py_str) = self.downcast_exact() {
315319
Ok(EitherString::Py(py_str.clone()))
316320
} else {

src/input/input_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'py> Input<'py> for StringMapping<'py> {
105105
&self,
106106
_strict: bool,
107107
_coerce_numbers_to_str: bool,
108-
) -> ValResult<ValidationMatch<EitherString<'_>>> {
108+
) -> ValResult<ValidationMatch<EitherString<'_, 'py>>> {
109109
match self {
110110
Self::String(s) => Ok(ValidationMatch::strict(s.clone().into())),
111111
Self::Mapping(_) => Err(ValError::new(ErrorTypeDefaults::StringType, self)),

src/input/return_enums.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -468,41 +468,41 @@ impl<'data> GenericJsonIterator<'data> {
468468
}
469469

470470
#[cfg_attr(debug_assertions, derive(Debug))]
471-
pub enum EitherString<'a> {
471+
pub enum EitherString<'a, 'py> {
472472
Cow(Cow<'a, str>),
473-
Py(Bound<'a, PyString>),
473+
Py(Bound<'py, PyString>),
474474
}
475475

476-
impl<'a> EitherString<'a> {
476+
impl<'py> EitherString<'_, 'py> {
477477
pub fn as_cow(&self) -> ValResult<Cow<'_, str>> {
478478
match self {
479-
Self::Cow(data) => Ok(data.clone()),
479+
Self::Cow(data) => Ok(Cow::Borrowed(data)),
480480
Self::Py(py_str) => Ok(Cow::Borrowed(py_string_str(py_str)?)),
481481
}
482482
}
483483

484-
pub fn as_py_string(&'a self, py: Python<'a>, cache_str: StringCacheMode) -> Bound<'a, PyString> {
484+
pub fn as_py_string(&self, py: Python<'py>, cache_str: StringCacheMode) -> Bound<'py, PyString> {
485485
match self {
486486
Self::Cow(cow) => new_py_string(py, cow.as_ref(), cache_str),
487487
Self::Py(py_string) => py_string.clone(),
488488
}
489489
}
490490
}
491491

492-
impl<'a> From<&'a str> for EitherString<'a> {
492+
impl<'a> From<&'a str> for EitherString<'a, '_> {
493493
fn from(data: &'a str) -> Self {
494494
Self::Cow(Cow::Borrowed(data))
495495
}
496496
}
497497

498-
impl From<String> for EitherString<'_> {
498+
impl From<String> for EitherString<'_, '_> {
499499
fn from(data: String) -> Self {
500500
Self::Cow(Cow::Owned(data))
501501
}
502502
}
503503

504-
impl<'a> From<Bound<'a, PyString>> for EitherString<'a> {
505-
fn from(date: Bound<'a, PyString>) -> Self {
504+
impl<'py> From<Bound<'py, PyString>> for EitherString<'_, 'py> {
505+
fn from(date: Bound<'py, PyString>) -> Self {
506506
Self::Py(date)
507507
}
508508
}

0 commit comments

Comments
 (0)