Skip to content

Commit 7edebbf

Browse files
committed
Remove DerefToPyType
1 parent 2e6875c commit 7edebbf

File tree

3 files changed

+20
-47
lines changed

3 files changed

+20
-47
lines changed

vm/src/builtins/pytype.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::{
1212
pyclass::{PyClassImpl, StaticType},
1313
pyobject::PyLease,
1414
types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr},
15-
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue,
16-
TypeProtocol, VirtualMachine,
15+
IdProtocol, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, TypeProtocol,
16+
VirtualMachine,
1717
};
1818
use itertools::Itertools;
1919
use std::{
@@ -209,8 +209,11 @@ impl PyType {
209209
}
210210

211211
impl PyTypeRef {
212+
/// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__,
213+
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
214+
/// method.
212215
pub fn issubclass(&self, cls: &impl Borrow<crate::PyObject>) -> bool {
213-
self._issubclass(cls)
216+
self.as_object().is(cls.borrow()) || self.mro.iter().any(|c| c.is(cls.borrow()))
214217
}
215218

216219
pub fn iter_mro(&self) -> impl Iterator<Item = &PyTypeRef> + DoubleEndedIterator {
@@ -788,36 +791,6 @@ pub(crate) fn init(ctx: &PyContext) {
788791
PyType::extend_class(ctx, &ctx.types.type_type);
789792
}
790793

791-
impl PyLease<'_, PyType> {
792-
pub fn issubclass(&self, cls: &impl Borrow<crate::PyObject>) -> bool {
793-
self._issubclass(cls)
794-
}
795-
}
796-
797-
pub trait DerefToPyType: Borrow<PyObject> {
798-
fn deref_to_type(&self) -> &PyType;
799-
800-
/// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__,
801-
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
802-
/// method.
803-
fn _issubclass(&self, cls: &impl Borrow<crate::PyObject>) -> bool {
804-
self.borrow().is(cls.borrow())
805-
|| self.deref_to_type().mro.iter().any(|c| c.is(cls.borrow()))
806-
}
807-
}
808-
809-
impl DerefToPyType for PyTypeRef {
810-
fn deref_to_type(&self) -> &PyType {
811-
self.deref()
812-
}
813-
}
814-
815-
impl<'a> DerefToPyType for PyLease<'a, PyType> {
816-
fn deref_to_type(&self) -> &PyType {
817-
self.deref()
818-
}
819-
}
820-
821794
pub(crate) fn call_slot_new(
822795
typ: PyTypeRef,
823796
subtype: PyTypeRef,

vm/src/pyobject.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub use crate::_pyobjectrc::{
22
PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef,
33
};
4-
use crate::common::{lock::PyRwLockReadGuard, rc::PyRc};
4+
use crate::common::lock::PyRwLockReadGuard;
55
use crate::{
66
builtins::{
77
builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef},
@@ -445,7 +445,7 @@ impl<'a, T: PyObjectPayload + PyValue> PyLease<'a, T> {
445445

446446
impl<'a, T: PyObjectPayload + PyValue> Borrow<PyObject> for PyLease<'a, T> {
447447
fn borrow(&self) -> &PyObject {
448-
self.inner.as_object()
448+
self.inner.as_ref()
449449
}
450450
}
451451

vm/src/pyobjectrc.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ impl ToOwned for PyObject {
466466

467467
pub trait PyObjectWrap
468468
where
469-
Self: Borrow<PyObject> + AsRef<PyObject>,
469+
Self: Borrow<PyObject>,
470470
{
471471
#[inline(always)]
472472
fn as_object(&self) -> &PyObject {
473-
self.as_ref()
473+
self.borrow()
474474
}
475475

476476
fn into_object(self) -> PyObjectRef;
@@ -986,28 +986,28 @@ where
986986
T: PyObjectPayload,
987987
{
988988
fn borrow(&self) -> &PyObject {
989-
self.as_object()
989+
(**self).as_object()
990990
}
991991
}
992992

993-
impl<T> PyObjectWrap for PyRef<T>
993+
impl<T> AsRef<PyObject> for PyRef<T>
994994
where
995995
T: PyObjectPayload,
996996
{
997-
#[inline]
998-
fn into_object(self) -> PyObjectRef {
999-
let me = ManuallyDrop::new(self);
1000-
PyObjectRef { ptr: me.ptr.cast() }
997+
#[inline(always)]
998+
fn as_ref(&self) -> &PyObject {
999+
self.borrow()
10011000
}
10021001
}
10031002

1004-
impl<T> AsRef<PyObject> for PyRef<T>
1003+
impl<T> PyObjectWrap for PyRef<T>
10051004
where
10061005
T: PyObjectPayload,
10071006
{
1008-
#[inline(always)]
1009-
fn as_ref(&self) -> &PyObject {
1010-
(**self).as_object()
1007+
#[inline]
1008+
fn into_object(self) -> PyObjectRef {
1009+
let me = ManuallyDrop::new(self);
1010+
PyObjectRef { ptr: me.ptr.cast() }
10111011
}
10121012
}
10131013

0 commit comments

Comments
 (0)