| 43 | |
| 44 | #[inline] |
| 45 | pub unsafe fn intern<S: InternableString>( |
| 46 | &self, |
| 47 | s: S, |
| 48 | typ: PyTypeRef, |
| 49 | ) -> &'static PyStrInterned { |
| 50 | if let Some(found) = self.interned(s.as_ref()) { |
| 51 | return found; |
| 52 | } |
| 53 | |
| 54 | #[cold] |
| 55 | fn miss(zelf: &StringPool, s: PyRefExact<PyStr>) -> &'static PyStrInterned { |
| 56 | let cache = CachedPyStrRef { inner: s }; |
| 57 | let inserted = zelf.inner.write().insert(cache.clone()); |
| 58 | if inserted { |
| 59 | let interned = unsafe { cache.as_interned_str() }; |
| 60 | unsafe { interned.as_object().mark_intern() }; |
| 61 | interned |
| 62 | } else { |
| 63 | unsafe { |
| 64 | zelf.inner |
| 65 | .read() |
| 66 | .get(cache.as_ref()) |
| 67 | .expect("inserted is false") |
| 68 | .as_interned_str() |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | let str_ref = s.into_pyref_exact(typ); |
| 73 | miss(self, str_ref) |
| 74 | } |
| 75 | |
| 76 | #[inline] |
| 77 | pub fn interned<S: MaybeInternedString + ?Sized>( |