diff --git a/python-utils/src/lib.rs b/python-utils/src/lib.rs index d958e47..37ce5d9 100644 --- a/python-utils/src/lib.rs +++ b/python-utils/src/lib.rs @@ -1,4 +1,28 @@ -use pyo3::{exceptions::PyTypeError, prelude::*}; +use std::convert::Infallible; + +use pyo3::{exceptions::PyTypeError, prelude::*, types::PyNone}; + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct IsNone; + +impl<'py> FromPyObject<'py> for IsNone { + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { + ob.downcast::()?; + Ok(IsNone) + } +} + +impl<'py> IntoPyObject<'py> for IsNone { + type Target = PyNone; + + type Output = Borrowed<'py, 'py, Self::Target>; + + type Error = Infallible; + + fn into_pyobject(self, py: Python<'py>) -> Result { + Ok(PyNone::get(py)) + } +} /// Create a GIL-independent reference pub fn detach(bound: &Bound) -> Py {