Skip to content

Commit ad7c966

Browse files
committed
more str tools
1 parent 95cd2f9 commit ad7c966

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

vm/src/builtins/str.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, IterNextIterable,
1717
Iterable, PyComparisonOp, Unconstructible,
1818
},
19-
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
19+
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult,
2020
TryFromBorrowedObject, VirtualMachine,
2121
};
2222
use ascii::{AsciiStr, AsciiString};
@@ -116,6 +116,12 @@ impl AsRef<str> for PyStr {
116116
}
117117
}
118118

119+
impl AsRef<str> for Py<PyStr> {
120+
fn as_ref(&self) -> &str {
121+
self.as_str()
122+
}
123+
}
124+
119125
impl AsRef<str> for PyStrRef {
120126
fn as_ref(&self) -> &str {
121127
self.as_str()
@@ -1510,6 +1516,12 @@ impl SliceableSequenceOp for PyStr {
15101516
}
15111517
}
15121518

1519+
impl AsRef<str> for PyRefExact<PyStr> {
1520+
fn as_ref(&self) -> &str {
1521+
self.as_str()
1522+
}
1523+
}
1524+
15131525
#[cfg(test)]
15141526
mod tests {
15151527
use super::*;

vm/src/intern.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl StringPool {
4242
}
4343

4444
#[derive(Debug, Clone)]
45+
#[repr(transparent)]
4546
pub struct CachedPyStrRef {
4647
inner: PyRefExact<PyStr>,
4748
}

vm/src/object/core.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,16 @@ where
10011001
}
10021002
}
10031003

1004+
impl<T> AsRef<Py<T>> for PyRef<T>
1005+
where
1006+
T: PyObjectPayload,
1007+
{
1008+
#[inline(always)]
1009+
fn as_ref(&self) -> &Py<T> {
1010+
self
1011+
}
1012+
}
1013+
10041014
impl<T> Deref for PyRef<T>
10051015
where
10061016
T: PyObjectPayload,

vm/src/object/ext.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ impl<T: PyPayload> Deref for PyRefExact<T> {
116116
}
117117
}
118118

119+
impl<T: PyObjectPayload> AsRef<Py<T>> for PyRefExact<T> {
120+
#[inline(always)]
121+
fn as_ref(&self) -> &Py<T> {
122+
self.inner.as_ref()
123+
}
124+
}
125+
119126
impl<T: PyPayload> ToPyObject for PyRefExact<T> {
120127
#[inline(always)]
121128
fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef {

0 commit comments

Comments
 (0)