macro_rules! make_nonzero {
($name:ident($ty:ty), $num:ty) => {
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq, Hash, ::pyo3::IntoPyObject)]
#[allow(unreachable_pub)]
pub struct $name(pub $ty);
impl<'a, 'py> ::pyo3::FromPyObject<'a, 'py> for $name {
type Error = ::pyo3::PyErr;
fn extract(ob: ::pyo3::Borrowed<'a, 'py, ::pyo3::PyAny>) -> Result<Self, Self::Error> {
ob.extract::<$num>()
.ok()
.and_then(|value| ::std::convert::TryFrom::try_from(value).ok())
.map(Self)
.ok_or_else(|| {
::pyo3::exceptions::PyValueError::new_err(concat!(
"expected a positive ",
stringify!($num)
))
})
}
}
#[cfg(feature = "stubs")]
impl ::pyo3_stub_gen::PyStubType for $name {
fn type_output() -> ::pyo3_stub_gen::TypeInfo {
::pyo3_stub_gen::TypeInfo::builtin("int")
}
}
};
}
make_nonzero!(NonZeroU16(std::num::NonZeroU16), u16);