use crate::ffi_ptr_ext::FfiPtrExt;
use crate::types::any::PyAnyMethods;
use crate::types::{PyAny, PyType};
use crate::{ffi, Bound, Python};
pub unsafe trait PyLayout<T> {}
pub trait PySizedLayout<T>: PyLayout<T> + Sized {}
pub unsafe trait PyTypeInfo: Sized {
const NAME: &'static str;
const MODULE: Option<&'static str>;
fn type_object_raw(py: Python<'_>) -> *mut ffi::PyTypeObject;
#[inline]
fn type_object(py: Python<'_>) -> Bound<'_, PyType> {
unsafe {
Self::type_object_raw(py)
.cast::<ffi::PyObject>()
.assume_borrowed_unchecked(py)
.to_owned()
.downcast_into_unchecked()
}
}
#[deprecated(since = "0.23.0", note = "renamed to `PyTypeInfo::type_object`")]
#[inline]
fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType> {
Self::type_object(py)
}
#[inline]
fn is_type_of(object: &Bound<'_, PyAny>) -> bool {
unsafe { ffi::PyObject_TypeCheck(object.as_ptr(), Self::type_object_raw(object.py())) != 0 }
}
#[deprecated(since = "0.23.0", note = "renamed to `PyTypeInfo::is_type_of`")]
#[inline]
fn is_type_of_bound(object: &Bound<'_, PyAny>) -> bool {
Self::is_type_of(object)
}
#[inline]
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool {
unsafe { ffi::Py_TYPE(object.as_ptr()) == Self::type_object_raw(object.py()) }
}
#[deprecated(since = "0.23.0", note = "renamed to `PyTypeInfo::is_exact_type_of`")]
#[inline]
fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool {
Self::is_exact_type_of(object)
}
}
pub trait PyTypeCheck {
const NAME: &'static str;
fn type_check(object: &Bound<'_, PyAny>) -> bool;
}
impl<T> PyTypeCheck for T
where
T: PyTypeInfo,
{
const NAME: &'static str = <T as PyTypeInfo>::NAME;
#[inline]
fn type_check(object: &Bound<'_, PyAny>) -> bool {
T::is_type_of(object)
}
}