diff --git a/vm/src/builtins/str.rs b/vm/src/builtins/str.rs index 911d19dda7..dad5a16e9e 100644 --- a/vm/src/builtins/str.rs +++ b/vm/src/builtins/str.rs @@ -16,7 +16,7 @@ use crate::{ AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible, }, - AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, + AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult, TryFromBorrowedObject, VirtualMachine, }; use ascii::{AsciiStr, AsciiString}; @@ -116,6 +116,12 @@ impl AsRef for PyStr { } } +impl AsRef for Py { + fn as_ref(&self) -> &str { + self.as_str() + } +} + impl AsRef for PyStrRef { fn as_ref(&self) -> &str { self.as_str() @@ -1510,6 +1516,12 @@ impl SliceableSequenceOp for PyStr { } } +impl AsRef for PyRefExact { + fn as_ref(&self) -> &str { + self.as_str() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/vm/src/intern.rs b/vm/src/intern.rs index d43115d345..ca284da037 100644 --- a/vm/src/intern.rs +++ b/vm/src/intern.rs @@ -42,6 +42,7 @@ impl StringPool { } #[derive(Debug, Clone)] +#[repr(transparent)] pub struct CachedPyStrRef { inner: PyRefExact, } diff --git a/vm/src/object/core.rs b/vm/src/object/core.rs index 53c55957c8..19c7aa47ec 100644 --- a/vm/src/object/core.rs +++ b/vm/src/object/core.rs @@ -1001,6 +1001,16 @@ where } } +impl AsRef> for PyRef +where + T: PyObjectPayload, +{ + #[inline(always)] + fn as_ref(&self) -> &Py { + self + } +} + impl Deref for PyRef where T: PyObjectPayload, diff --git a/vm/src/object/ext.rs b/vm/src/object/ext.rs index 762e728694..67105953fd 100644 --- a/vm/src/object/ext.rs +++ b/vm/src/object/ext.rs @@ -116,6 +116,13 @@ impl Deref for PyRefExact { } } +impl AsRef> for PyRefExact { + #[inline(always)] + fn as_ref(&self) -> &Py { + self.inner.as_ref() + } +} + impl ToPyObject for PyRefExact { #[inline(always)] fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef {