Remove DerefToPyType

This commit is contained in:
Jeong Yunwon
2022-04-17 10:03:48 +09:00
parent 2e6875c8d5
commit 7edebbf28c
3 changed files with 21 additions and 48 deletions

View File

@@ -12,8 +12,8 @@ use crate::{
pyclass::{PyClassImpl, StaticType},
pyobject::PyLease,
types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr},
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue,
TypeProtocol, VirtualMachine,
IdProtocol, PyContext, PyObjectRef, PyObjectWeak, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
};
use itertools::Itertools;
use std::{
@@ -209,8 +209,11 @@ impl PyType {
}
impl PyTypeRef {
/// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__,
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
/// method.
pub fn issubclass(&self, cls: &impl Borrow<crate::PyObject>) -> bool {
self._issubclass(cls)
self.as_object().is(cls.borrow()) || self.mro.iter().any(|c| c.is(cls.borrow()))
}
pub fn iter_mro(&self) -> impl Iterator<Item = &PyTypeRef> + DoubleEndedIterator {
@@ -788,36 +791,6 @@ pub(crate) fn init(ctx: &PyContext) {
PyType::extend_class(ctx, &ctx.types.type_type);
}
impl PyLease<'_, PyType> {
pub fn issubclass(&self, cls: &impl Borrow<crate::PyObject>) -> bool {
self._issubclass(cls)
}
}
pub trait DerefToPyType: Borrow<PyObject> {
fn deref_to_type(&self) -> &PyType;
/// Determines if `subclass` is actually a subclass of `cls`, this doesn't call __subclasscheck__,
/// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
/// method.
fn _issubclass(&self, cls: &impl Borrow<crate::PyObject>) -> bool {
self.borrow().is(cls.borrow())
|| self.deref_to_type().mro.iter().any(|c| c.is(cls.borrow()))
}
}
impl DerefToPyType for PyTypeRef {
fn deref_to_type(&self) -> &PyType {
self.deref()
}
}
impl<'a> DerefToPyType for PyLease<'a, PyType> {
fn deref_to_type(&self) -> &PyType {
self.deref()
}
}
pub(crate) fn call_slot_new(
typ: PyTypeRef,
subtype: PyTypeRef,

View File

@@ -1,7 +1,7 @@
pub use crate::_pyobjectrc::{
PyObject, PyObjectRef, PyObjectView, PyObjectWeak, PyObjectWrap, PyRef, PyWeakRef,
};
use crate::common::{lock::PyRwLockReadGuard, rc::PyRc};
use crate::common::lock::PyRwLockReadGuard;
use crate::{
builtins::{
builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef},
@@ -445,7 +445,7 @@ impl<'a, T: PyObjectPayload + PyValue> PyLease<'a, T> {
impl<'a, T: PyObjectPayload + PyValue> Borrow<PyObject> for PyLease<'a, T> {
fn borrow(&self) -> &PyObject {
self.inner.as_object()
self.inner.as_ref()
}
}

View File

@@ -466,11 +466,11 @@ impl ToOwned for PyObject {
pub trait PyObjectWrap
where
Self: Borrow<PyObject> + AsRef<PyObject>,
Self: Borrow<PyObject>,
{
#[inline(always)]
fn as_object(&self) -> &PyObject {
self.as_ref()
self.borrow()
}
fn into_object(self) -> PyObjectRef;
@@ -986,7 +986,17 @@ where
T: PyObjectPayload,
{
fn borrow(&self) -> &PyObject {
self.as_object()
(**self).as_object()
}
}
impl<T> AsRef<PyObject> for PyRef<T>
where
T: PyObjectPayload,
{
#[inline(always)]
fn as_ref(&self) -> &PyObject {
self.borrow()
}
}
@@ -1001,16 +1011,6 @@ where
}
}
impl<T> AsRef<PyObject> for PyRef<T>
where
T: PyObjectPayload,
{
#[inline(always)]
fn as_ref(&self) -> &PyObject {
(**self).as_object()
}
}
impl<T> Borrow<PyObjectView<T>> for PyRef<T>
where
T: PyObjectPayload,