From 2a07edcde7538f9ee6fcb60cfae860040573c088 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Thu, 14 Dec 2023 18:01:23 +0000 Subject: [PATCH] implement `PyBoolMethods` --- src/prelude.rs | 1 + src/types/boolobject.rs | 22 +++++++++++++++++++++- src/types/mod.rs | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/prelude.rs b/src/prelude.rs index e6a3cb7fe72..7edea28f22c 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -26,4 +26,5 @@ pub use crate::wrap_pyfunction; // Expected to become public API in 0.21 // pub(crate) use crate::instance::Py2; // Will be stabilized with a different name // pub(crate) use crate::types::any::PyAnyMethods; +// pub(crate) use crate::types::boolobject::PyBoolMethods; // pub(crate) use crate::types::sequence::PySequenceMethods; diff --git a/src/types/boolobject.rs b/src/types/boolobject.rs index 1f42db90c3b..d89e3bf0005 100644 --- a/src/types/boolobject.rs +++ b/src/types/boolobject.rs @@ -1,6 +1,8 @@ #[cfg(feature = "experimental-inspect")] use crate::inspect::types::TypeInfo; -use crate::{ffi, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject}; +use crate::{ + ffi, instance::Py2, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject, +}; /// Represents a Python `bool`. #[repr(transparent)] @@ -18,6 +20,24 @@ impl PyBool { /// Gets whether this boolean is `true`. #[inline] pub fn is_true(&self) -> bool { + Py2::::borrowed_from_gil_ref(&self).is_true() + } +} + +/// Implementation of functionality for [`PyBool`]. +/// +/// These methods are defined for the `Py2<'py, PyBool>` smart pointer, so to use method call +/// syntax these methods are separated into a trait, because stable Rust does not yet support +/// `arbitrary_self_types`. +#[doc(alias = "PyBool")] +pub trait PyBoolMethods<'py> { + /// Gets whether this boolean is `true`. + fn is_true(&self) -> bool; +} + +impl<'py> PyBoolMethods<'py> for Py2<'py, PyBool> { + #[inline] + fn is_true(&self) -> bool { self.as_ptr() == unsafe { crate::ffi::Py_True() } } } diff --git a/src/types/mod.rs b/src/types/mod.rs index ef2390cc809..c245387d259 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -270,7 +270,7 @@ macro_rules! pyobject_native_type { } pub(crate) mod any; -mod boolobject; +pub(crate) mod boolobject; mod bytearray; mod bytes; mod capsule;