From cff48691ef432b6a5d0f7084a150df570d7e238d Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 3 May 2025 21:08:10 -0400 Subject: [PATCH] feat(python-utils): a type that validates as the `None` Python type --- python-utils/src/lib.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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 {