Merge pull request #3706 from youknowone/strtools

more str AsRef<str>
This commit is contained in:
Jeong YunWon
2022-05-16 04:01:25 +09:00
committed by GitHub
4 changed files with 31 additions and 1 deletions

View File

@@ -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<str> for PyStr {
}
}
impl AsRef<str> for Py<PyStr> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<str> for PyStrRef {
fn as_ref(&self) -> &str {
self.as_str()
@@ -1510,6 +1516,12 @@ impl SliceableSequenceOp for PyStr {
}
}
impl AsRef<str> for PyRefExact<PyStr> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -42,6 +42,7 @@ impl StringPool {
}
#[derive(Debug, Clone)]
#[repr(transparent)]
pub struct CachedPyStrRef {
inner: PyRefExact<PyStr>,
}

View File

@@ -1001,6 +1001,16 @@ where
}
}
impl<T> AsRef<Py<T>> for PyRef<T>
where
T: PyObjectPayload,
{
#[inline(always)]
fn as_ref(&self) -> &Py<T> {
self
}
}
impl<T> Deref for PyRef<T>
where
T: PyObjectPayload,

View File

@@ -116,6 +116,13 @@ impl<T: PyPayload> Deref for PyRefExact<T> {
}
}
impl<T: PyObjectPayload> AsRef<Py<T>> for PyRefExact<T> {
#[inline(always)]
fn as_ref(&self) -> &Py<T> {
self.inner.as_ref()
}
}
impl<T: PyPayload> ToPyObject for PyRefExact<T> {
#[inline(always)]
fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef {