Skip to content

Commit

Permalink
Merge pull request #3650 from davidhewitt/float2
Browse files Browse the repository at this point in the history
implement `PyFloatMethods`
  • Loading branch information
adamreichold authored Dec 14, 2023
2 parents 763ecb3 + 8a7c500 commit d1b4b9e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::float::PyFloatMethods;
// pub(crate) use crate::types::sequence::PySequenceMethods;
20 changes: 19 additions & 1 deletion src/types/floatob.rs → src/types/float.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo;
use crate::{
ffi, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject, PyResult, Python, ToPyObject,
ffi, instance::Py2, FromPyObject, IntoPy, PyAny, PyErr, PyNativeType, PyObject, PyResult,
Python, ToPyObject,
};
use std::os::raw::c_double;

Expand All @@ -28,6 +29,23 @@ impl PyFloat {

/// Gets the value of this float.
pub fn value(&self) -> c_double {
Py2::borrowed_from_gil_ref(&self).value()
}
}

/// Implementation of functionality for [`PyFloat`].
///
/// These methods are defined for the `Py2<'py, PyFloat>` 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 = "PyFloat")]
pub trait PyFloatMethods<'py> {
/// Gets the value of this float.
fn value(&self) -> c_double;
}

impl<'py> PyFloatMethods<'py> for Py2<'py, PyFloat> {
fn value(&self) -> c_double {
#[cfg(not(Py_LIMITED_API))]
unsafe {
// Safety: self is PyFloat object
Expand Down
4 changes: 2 additions & 2 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use self::dict::{IntoPyDict, PyDict};
#[cfg(not(PyPy))]
pub use self::dict::{PyDictItems, PyDictKeys, PyDictValues};
pub use self::ellipsis::PyEllipsis;
pub use self::floatob::PyFloat;
pub use self::float::PyFloat;
#[cfg(all(not(Py_LIMITED_API), not(PyPy)))]
pub use self::frame::PyFrame;
pub use self::frozenset::{PyFrozenSet, PyFrozenSetBuilder};
Expand Down Expand Up @@ -281,7 +281,7 @@ mod complex;
mod datetime;
mod dict;
mod ellipsis;
mod floatob;
pub(crate) mod float;
#[cfg(all(not(Py_LIMITED_API), not(PyPy)))]
mod frame;
mod frozenset;
Expand Down

0 comments on commit d1b4b9e

Please sign in to comment.