11use crate :: {
2- builtins:: { PyStr , PyTypeRef } ,
2+ builtins:: { PyStr , PyStrInterned , PyTypeRef } ,
33 common:: lock:: PyRwLock ,
44 convert:: ToPyObject ,
55 AsObject , Py , PyExact , PyObject , PyObjectRef , PyPayload , PyRef , PyRefExact , VirtualMachine ,
@@ -42,17 +42,16 @@ impl StringPool {
4242 let cache = CachedPyStrRef { inner : s } ;
4343 let inserted = zelf. inner . write ( ) . insert ( cache. clone ( ) ) ;
4444 if inserted {
45- let interned = unsafe { PyStrInterned :: borrow_cache ( & cache) } ;
45+ let interned = unsafe { cache. as_interned_str ( ) } ;
4646 unsafe { interned. as_object ( ) . mark_intern ( ) } ;
4747 interned
4848 } else {
4949 unsafe {
50- PyStrInterned :: borrow_cache (
51- zelf. inner
52- . read ( )
53- . get ( cache. as_ref ( ) )
54- . expect ( "inserted is false" ) ,
55- )
50+ zelf. inner
51+ . read ( )
52+ . get ( cache. as_ref ( ) )
53+ . expect ( "inserted is false" )
54+ . as_interned_str ( )
5655 }
5756 }
5857 }
@@ -68,7 +67,7 @@ impl StringPool {
6867 self . inner
6968 . read ( )
7069 . get ( s. as_ref ( ) )
71- . map ( |cached| unsafe { PyStrInterned :: borrow_cache ( cached) } )
70+ . map ( |cached| unsafe { cached. as_interned_str ( ) } )
7271 }
7372}
7473
@@ -107,6 +106,13 @@ impl AsRef<str> for CachedPyStrRef {
107106}
108107
109108impl CachedPyStrRef {
109+ /// # Safety
110+ /// the given cache must be alive while returned reference is alive
111+ #[ inline]
112+ unsafe fn as_interned_str ( & self ) -> & ' static PyStrInterned {
113+ std:: mem:: transmute_copy ( self )
114+ }
115+
110116 #[ inline]
111117 fn as_str ( & self ) -> & str {
112118 self . inner . as_str ( )
@@ -188,37 +194,6 @@ impl<T: PyPayload> ToPyObject for &'static PyInterned<T> {
188194 }
189195}
190196
191- /// The unique reference of interned PyStr
192- /// Always intended to be used as a static reference
193- pub type PyStrInterned = PyInterned < PyStr > ;
194-
195- impl PyStrInterned {
196- /// # Safety
197- /// the given cache must be alive while returned reference is alive
198- #[ inline]
199- unsafe fn borrow_cache ( cache : & CachedPyStrRef ) -> & ' static Self {
200- std:: mem:: transmute_copy ( cache)
201- }
202-
203- #[ inline]
204- pub fn to_exact ( & ' static self ) -> PyRefExact < PyStr > {
205- unsafe { PyRefExact :: new_unchecked ( self . to_owned ( ) ) }
206- }
207- }
208-
209- impl std:: fmt:: Display for PyStrInterned {
210- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
211- std:: fmt:: Display :: fmt ( self . as_str ( ) , f)
212- }
213- }
214-
215- impl AsRef < str > for PyStrInterned {
216- #[ inline( always) ]
217- fn as_ref ( & self ) -> & str {
218- self . as_str ( )
219- }
220- }
221-
222197mod sealed {
223198 use crate :: {
224199 builtins:: PyStr ,
0 commit comments