Skip to content

Commit abcdbb7

Browse files
committed
simplify, use without caching
1 parent e6c3e8f commit abcdbb7

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/input/return_enums.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use serde::{ser::Error, Serialize, Serializer};
1919
use crate::errors::{
2020
py_err_string, ErrorType, ErrorTypeDefaults, InputValue, ToErrorValue, ValError, ValLineError, ValResult,
2121
};
22-
use crate::tools::{extract_i64, py_err};
22+
use crate::tools::{extract_i64, new_py_string, py_err};
2323
use crate::validators::{CombinedValidator, Exactness, ValidationState, Validator};
2424

2525
use super::{py_error_on_minusone, BorrowInput, Input};
@@ -437,14 +437,7 @@ impl<'a> EitherString<'a> {
437437

438438
pub fn as_py_string(&'a self, py: Python<'a>, cache_str: StringCacheMode) -> Bound<'a, PyString> {
439439
match self {
440-
Self::Cow(cow) => {
441-
if matches!(cache_str, StringCacheMode::All) {
442-
let s = cow.as_ref();
443-
jiter::cached_py_string(py, s, bytecount::num_chars(s.as_bytes()) == s.len())
444-
} else {
445-
PyString::new_bound(py, cow.as_ref())
446-
}
447-
}
440+
Self::Cow(cow) => new_py_string(py, cow.as_ref(), cache_str),
448441
Self::Py(py_string) => py_string.clone(),
449442
}
450443
}

src/tools.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use pyo3::prelude::*;
55
use pyo3::types::{PyDict, PyString};
66
use pyo3::{ffi, intern, FromPyObject};
77

8+
use jiter::{cached_py_string, pystring_fast_new, StringCacheMode};
9+
810
pub trait SchemaDict<'py> {
911
fn get_as<T>(&self, key: &Bound<'_, PyString>) -> PyResult<Option<T>>
1012
where
@@ -143,3 +145,12 @@ pub fn extract_i64(v: &Bound<'_, PyAny>) -> Option<i64> {
143145
None
144146
}
145147
}
148+
149+
pub(crate) fn new_py_string<'py>(py: Python<'py>, s: &str, cache_str: StringCacheMode) -> Bound<'py, PyString> {
150+
let ascii_only = bytecount::num_chars(s.as_bytes()) == s.len();
151+
if matches!(cache_str, StringCacheMode::All) {
152+
cached_py_string(py, s, ascii_only)
153+
} else {
154+
pystring_fast_new(py, s, ascii_only)
155+
}
156+
}

src/validators/validation_state.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use pyo3::types::PyString;
44
use jiter::StringCacheMode;
55

66
use crate::recursion_guard::{ContainsRecursionState, RecursionState};
7+
use crate::tools::new_py_string;
78

89
use super::Extra;
910

@@ -72,11 +73,7 @@ impl<'a, 'py> ValidationState<'a, 'py> {
7273
}
7374

7475
pub fn maybe_cached_str(&self, py: Python<'py>, s: &str) -> Bound<'py, PyString> {
75-
if matches!(self.extra.cache_str, StringCacheMode::All) {
76-
jiter::cached_py_string(py, s, bytecount::num_chars(s.as_bytes()) == s.len())
77-
} else {
78-
PyString::new_bound(py, s)
79-
}
76+
new_py_string(py, s, self.extra.cache_str)
8077
}
8178
}
8279

0 commit comments

Comments
 (0)