Merge pull request #3652 from davidhewitt/bool2

implement `PyBoolMethods`
This commit is contained in:
Adam Reichold 2023-12-15 14:55:56 +00:00 committed by GitHub
commit 118d578ac1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View File

@ -26,5 +26,6 @@ pub use crate::wrap_pyfunction;
// Expected to become public API in 0.21 // 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::instance::Py2; // Will be stabilized with a different name
// pub(crate) use crate::types::any::PyAnyMethods; // pub(crate) use crate::types::any::PyAnyMethods;
// pub(crate) use crate::types::boolobject::PyBoolMethods;
// pub(crate) use crate::types::float::PyFloatMethods; // pub(crate) use crate::types::float::PyFloatMethods;
// pub(crate) use crate::types::sequence::PySequenceMethods; // pub(crate) use crate::types::sequence::PySequenceMethods;

View File

@ -1,6 +1,8 @@
#[cfg(feature = "experimental-inspect")] #[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo; 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`. /// Represents a Python `bool`.
#[repr(transparent)] #[repr(transparent)]
@ -18,6 +20,24 @@ impl PyBool {
/// Gets whether this boolean is `true`. /// Gets whether this boolean is `true`.
#[inline] #[inline]
pub fn is_true(&self) -> bool { 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() } self.as_ptr() == unsafe { crate::ffi::Py_True() }
} }
} }

View File

@ -272,7 +272,7 @@ macro_rules! pyobject_native_type {
} }
pub(crate) mod any; pub(crate) mod any;
mod boolobject; pub(crate) mod boolobject;
mod bytearray; mod bytearray;
mod bytes; mod bytes;
mod capsule; mod capsule;