@@ -40,11 +40,7 @@ impl<'py> FromPyObject<'_, 'py> for StringCacheMode {
4040
4141impl From < bool > for StringCacheMode {
4242 fn from ( mode : bool ) -> Self {
43- if mode {
44- Self :: All
45- } else {
46- Self :: None
47- }
43+ if mode { Self :: All } else { Self :: None }
4844 }
4945}
5046
@@ -131,11 +127,13 @@ pub unsafe fn cached_py_string_ascii<'py>(py: Python<'py>, s: &str) -> Bound<'py
131127///
132128/// Caller must match the ascii_only flag to the string passed in.
133129unsafe fn cached_py_string_maybe_ascii < ' py > ( py : Python < ' py > , s : & str , ascii_only : bool ) -> Bound < ' py , PyString > {
134- // from tests, 0 and 1 character strings are faster not cached
135- if ( 2 ..64 ) . contains ( & s. len ( ) ) {
136- get_string_cache ( ) . get_or_insert ( py, s, ascii_only)
137- } else {
138- pystring_fast_new_maybe_ascii ( py, s, ascii_only)
130+ unsafe {
131+ // from tests, 0 and 1 character strings are faster not cached
132+ if ( 2 ..64 ) . contains ( & s. len ( ) ) {
133+ get_string_cache ( ) . get_or_insert ( py, s, ascii_only)
134+ } else {
135+ pystring_fast_new_maybe_ascii ( py, s, ascii_only)
136+ }
139137 }
140138}
141139
@@ -254,19 +252,21 @@ unsafe fn pystring_fast_new_maybe_ascii<'py>(py: Python<'py>, s: &str, ascii_onl
254252///
255253/// `s` must be ASCII only
256254pub unsafe fn pystring_ascii_new < ' py > ( py : Python < ' py > , s : & str ) -> Bound < ' py , PyString > {
257- #[ cfg( not( any( PyPy , GraalPy , Py_LIMITED_API ) ) ) ]
258- {
259- let ptr = pyo3:: ffi:: PyUnicode_New ( s. len ( ) as isize , 127 ) ;
260- // see https://github.com/pydantic/jiter/pull/72#discussion_r1545485907
261- debug_assert_eq ! ( pyo3:: ffi:: PyUnicode_KIND ( ptr) , pyo3:: ffi:: PyUnicode_1BYTE_KIND ) ;
262- let data_ptr = pyo3:: ffi:: PyUnicode_DATA ( ptr) . cast ( ) ;
263- core:: ptr:: copy_nonoverlapping ( s. as_ptr ( ) , data_ptr, s. len ( ) ) ;
264- core:: ptr:: write ( data_ptr. add ( s. len ( ) ) , 0 ) ;
265- Bound :: from_owned_ptr ( py, ptr) . cast_into_unchecked ( )
266- }
255+ unsafe {
256+ #[ cfg( not( any( PyPy , GraalPy , Py_LIMITED_API ) ) ) ]
257+ {
258+ let ptr = pyo3:: ffi:: PyUnicode_New ( s. len ( ) as isize , 127 ) ;
259+ // see https://github.com/pydantic/jiter/pull/72#discussion_r1545485907
260+ debug_assert_eq ! ( pyo3:: ffi:: PyUnicode_KIND ( ptr) , pyo3:: ffi:: PyUnicode_1BYTE_KIND ) ;
261+ let data_ptr = pyo3:: ffi:: PyUnicode_DATA ( ptr) . cast ( ) ;
262+ core:: ptr:: copy_nonoverlapping ( s. as_ptr ( ) , data_ptr, s. len ( ) ) ;
263+ core:: ptr:: write ( data_ptr. add ( s. len ( ) ) , 0 ) ;
264+ Bound :: from_owned_ptr ( py, ptr) . cast_into_unchecked ( )
265+ }
267266
268- #[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
269- {
270- PyString :: new ( py, s)
267+ #[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
268+ {
269+ PyString :: new ( py, s)
270+ }
271271 }
272272}
0 commit comments