Skip to content

Commit a13cf32

Browse files
committed
Add PyObjectRc::into_ref()
1 parent d975858 commit a13cf32

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

vm/src/pyobject.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,15 +1187,6 @@ pub trait PyValue: fmt::Debug + PyThreadingConstraint + Sized + 'static {
11871187
}
11881188
}
11891189

1190-
impl<T> PyValue for std::mem::MaybeUninit<T>
1191-
where
1192-
T: PyValue,
1193-
{
1194-
fn class(vm: &VirtualMachine) -> PyClassRef {
1195-
T::class(vm)
1196-
}
1197-
}
1198-
11991190
pub trait BorrowValue<'a>: PyValue {
12001191
type Borrowed: 'a + Deref;
12011192
fn borrow_value(&'a self) -> Self::Borrowed;

vm/src/pyobjectrc.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,53 @@ use std::ops::Deref;
77
pub struct PyObjectRc<T = dyn PyObjectPayload>
88
where
99
T: ?Sized + PyObjectPayload,
10+
PyRc<PyObject<T>>: AsPyObjectRef,
1011
{
1112
inner: PyRc<PyObject<T>>,
1213
}
1314

1415
pub type PyObjectWeak<T = dyn PyObjectPayload> = PyWeak<PyObject<T>>;
1516

17+
pub trait AsPyObjectRef {
18+
fn _as_ref(self) -> PyRc<PyObject<dyn PyObjectPayload>>;
19+
}
20+
21+
impl<T> AsPyObjectRef for PyRc<PyObject<T>>
22+
where
23+
T: PyObjectPayload,
24+
{
25+
fn _as_ref(self) -> PyRc<PyObject<dyn PyObjectPayload>> {
26+
self
27+
}
28+
}
29+
30+
impl AsPyObjectRef for PyRc<PyObject<dyn PyObjectPayload>> {
31+
fn _as_ref(self) -> PyRc<PyObject<dyn PyObjectPayload>> {
32+
self
33+
}
34+
}
35+
1636
impl<T> PyObjectRc<T>
1737
where
1838
T: ?Sized + PyObjectPayload,
39+
PyRc<PyObject<T>>: AsPyObjectRef,
1940
{
2041
pub fn into_raw(this: Self) -> *const PyObject<T> {
2142
let ptr = PyRc::as_ptr(&this.inner);
2243
std::mem::forget(this);
2344
ptr
2445
}
2546

47+
unsafe fn into_rc(this: Self) -> PyRc<PyObject<T>> {
48+
std::mem::transmute(this)
49+
}
50+
51+
pub fn into_ref(this: Self) -> PyObjectRc<dyn PyObjectPayload> {
52+
PyObjectRc::<dyn PyObjectPayload> {
53+
inner: unsafe { Self::into_rc(this) }._as_ref(),
54+
}
55+
}
56+
2657
/// # Safety
2758
/// See PyRc::from_raw
2859
pub unsafe fn from_raw(ptr: *const PyObject<T>) -> Self {
@@ -58,13 +89,24 @@ where
5889
}
5990

6091
#[cfg(feature = "threading")]
61-
unsafe impl<T> Send for PyObjectRc<T> where T: ?Sized + PyObjectPayload {}
92+
unsafe impl<T> Send for PyObjectRc<T>
93+
where
94+
T: ?Sized + PyObjectPayload,
95+
PyRc<PyObject<T>>: AsPyObjectRef,
96+
{
97+
}
6298
#[cfg(feature = "threading")]
63-
unsafe impl<T> Sync for PyObjectRc<T> where T: ?Sized + PyObjectPayload {}
99+
unsafe impl<T> Sync for PyObjectRc<T>
100+
where
101+
T: ?Sized + PyObjectPayload,
102+
PyRc<PyObject<T>>: AsPyObjectRef,
103+
{
104+
}
64105

65106
impl<T> Deref for PyObjectRc<T>
66107
where
67108
T: ?Sized + PyObjectPayload,
109+
PyRc<PyObject<T>>: AsPyObjectRef,
68110
{
69111
type Target = PyObject<T>;
70112

@@ -77,6 +119,7 @@ where
77119
impl<T> Clone for PyObjectRc<T>
78120
where
79121
T: ?Sized + PyObjectPayload,
122+
PyRc<PyObject<T>>: AsPyObjectRef,
80123
{
81124
fn clone(&self) -> Self {
82125
PyObjectRc {
@@ -88,6 +131,7 @@ where
88131
impl<T> fmt::Display for PyObjectRc<T>
89132
where
90133
T: ?Sized + PyObjectPayload,
134+
PyRc<PyObject<T>>: AsPyObjectRef,
91135
PyObject<T>: fmt::Display,
92136
{
93137
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -98,6 +142,7 @@ where
98142
impl<T> fmt::Debug for PyObjectRc<T>
99143
where
100144
T: ?Sized + PyObjectPayload,
145+
PyRc<PyObject<T>>: AsPyObjectRef,
101146
PyObject<T>: fmt::Debug,
102147
{
103148
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -108,6 +153,7 @@ where
108153
impl<T> fmt::Pointer for PyObjectRc<T>
109154
where
110155
T: ?Sized + PyObjectPayload,
156+
PyRc<PyObject<T>>: AsPyObjectRef,
111157
PyObject<T>: fmt::Pointer,
112158
{
113159
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -118,7 +164,7 @@ where
118164
impl<T> borrow::Borrow<T> for PyObjectRc<T>
119165
where
120166
T: ?Sized + PyObjectPayload,
121-
PyRc<PyObject<T>>: borrow::Borrow<T>,
167+
PyRc<PyObject<T>>: AsPyObjectRef + borrow::Borrow<T>,
122168
{
123169
fn borrow(&self) -> &T {
124170
self.inner.borrow()
@@ -128,7 +174,7 @@ where
128174
impl<T> AsRef<T> for PyObjectRc<T>
129175
where
130176
T: ?Sized + PyObjectPayload,
131-
PyRc<PyObject<T>>: AsRef<T>,
177+
PyRc<PyObject<T>>: AsPyObjectRef + AsRef<T>,
132178
{
133179
fn as_ref(&self) -> &T {
134180
self.inner.as_ref()

0 commit comments

Comments
 (0)