Skip to content

Commit 2a07edc

Browse files
committed
implement PyBoolMethods
1 parent 79a54cf commit 2a07edc

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ pub use crate::wrap_pyfunction;
2626
// Expected to become public API in 0.21
2727
// pub(crate) use crate::instance::Py2; // Will be stabilized with a different name
2828
// pub(crate) use crate::types::any::PyAnyMethods;
29+
// pub(crate) use crate::types::boolobject::PyBoolMethods;
2930
// pub(crate) use crate::types::sequence::PySequenceMethods;

src/types/boolobject.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[cfg(feature = "experimental-inspect")]
22
use crate::inspect::types::TypeInfo;
3-
use crate::{ffi, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject};
3+
use crate::{
4+
ffi, instance::Py2, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject,
5+
};
46

57
/// Represents a Python `bool`.
68
#[repr(transparent)]
@@ -18,6 +20,24 @@ impl PyBool {
1820
/// Gets whether this boolean is `true`.
1921
#[inline]
2022
pub fn is_true(&self) -> bool {
23+
Py2::<PyBool>::borrowed_from_gil_ref(&self).is_true()
24+
}
25+
}
26+
27+
/// Implementation of functionality for [`PyBool`].
28+
///
29+
/// These methods are defined for the `Py2<'py, PyBool>` smart pointer, so to use method call
30+
/// syntax these methods are separated into a trait, because stable Rust does not yet support
31+
/// `arbitrary_self_types`.
32+
#[doc(alias = "PyBool")]
33+
pub trait PyBoolMethods<'py> {
34+
/// Gets whether this boolean is `true`.
35+
fn is_true(&self) -> bool;
36+
}
37+
38+
impl<'py> PyBoolMethods<'py> for Py2<'py, PyBool> {
39+
#[inline]
40+
fn is_true(&self) -> bool {
2141
self.as_ptr() == unsafe { crate::ffi::Py_True() }
2242
}
2343
}

src/types/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ macro_rules! pyobject_native_type {
270270
}
271271

272272
pub(crate) mod any;
273-
mod boolobject;
273+
pub(crate) mod boolobject;
274274
mod bytearray;
275275
mod bytes;
276276
mod capsule;

0 commit comments

Comments
 (0)