Remove explicit matches in conversions.

Don't explicitly match Options and Results if only one of the
variants is changed.
This commit is contained in:
Sebastian Puetz 2020-07-19 13:26:59 +02:00
parent 53a248c617
commit 77d03d01f3

View file

@ -41,10 +41,8 @@ where
{ {
#[inline] #[inline]
fn as_ptr(&self) -> *mut ffi::PyObject { fn as_ptr(&self) -> *mut ffi::PyObject {
match *self { self.as_ref()
Some(ref t) => t.as_ptr(), .map_or_else(std::ptr::null_mut, |t| t.into_ptr())
None => std::ptr::null_mut(),
}
} }
} }
@ -55,10 +53,7 @@ where
{ {
#[inline] #[inline]
fn into_ptr(self) -> *mut ffi::PyObject { fn into_ptr(self) -> *mut ffi::PyObject {
match self { self.map_or_else(std::ptr::null_mut, |t| t.into_ptr())
Some(t) => t.into_ptr(),
None => std::ptr::null_mut(),
}
} }
} }
@ -218,10 +213,8 @@ where
T: ToPyObject, T: ToPyObject,
{ {
fn to_object(&self, py: Python) -> PyObject { fn to_object(&self, py: Python) -> PyObject {
match *self { self.as_ref()
Some(ref val) => val.to_object(py), .map_or_else(|| py.None(), |val| val.to_object(py))
None => py.None(),
}
} }
} }
@ -230,10 +223,7 @@ where
T: IntoPy<PyObject>, T: IntoPy<PyObject>,
{ {
fn into_py(self, py: Python) -> PyObject { fn into_py(self, py: Python) -> PyObject {
match self { self.map_or_else(|| py.None(), |val| val.into_py(py))
Some(val) => val.into_py(py),
None => py.None(),
}
} }
} }
@ -307,10 +297,7 @@ where
if obj.as_ptr() == unsafe { ffi::Py_None() } { if obj.as_ptr() == unsafe { ffi::Py_None() } {
Ok(None) Ok(None)
} else { } else {
match T::extract(obj) { T::extract(obj).map(Some)
Ok(v) => Ok(Some(v)),
Err(e) => Err(e),
}
} }
} }
} }
@ -427,27 +414,18 @@ impl FromPy<()> for Py<PyTuple> {
pub unsafe trait FromPyPointer<'p>: Sized { pub unsafe trait FromPyPointer<'p>: Sized {
unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self>; unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self>;
unsafe fn from_owned_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self { unsafe fn from_owned_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
match Self::from_owned_ptr_or_opt(py, ptr) { Self::from_owned_ptr_or_opt(py, ptr).unwrap_or_else(|| err::panic_after_error(py))
Some(s) => s,
None => err::panic_after_error(py),
}
} }
unsafe fn from_owned_ptr(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self { unsafe fn from_owned_ptr(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
Self::from_owned_ptr_or_panic(py, ptr) Self::from_owned_ptr_or_panic(py, ptr)
} }
unsafe fn from_owned_ptr_or_err(py: Python<'p>, ptr: *mut ffi::PyObject) -> PyResult<&'p Self> { unsafe fn from_owned_ptr_or_err(py: Python<'p>, ptr: *mut ffi::PyObject) -> PyResult<&'p Self> {
match Self::from_owned_ptr_or_opt(py, ptr) { Self::from_owned_ptr_or_opt(py, ptr).ok_or_else(|| err::PyErr::fetch(py))
Some(s) => Ok(s),
None => Err(err::PyErr::fetch(py)),
}
} }
unsafe fn from_borrowed_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) unsafe fn from_borrowed_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject)
-> Option<&'p Self>; -> Option<&'p Self>;
unsafe fn from_borrowed_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self { unsafe fn from_borrowed_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
match Self::from_borrowed_ptr_or_opt(py, ptr) { Self::from_borrowed_ptr_or_opt(py, ptr).unwrap_or_else(|| err::panic_after_error(py))
Some(s) => s,
None => err::panic_after_error(py),
}
} }
unsafe fn from_borrowed_ptr(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self { unsafe fn from_borrowed_ptr(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
Self::from_borrowed_ptr_or_panic(py, ptr) Self::from_borrowed_ptr_or_panic(py, ptr)
@ -456,10 +434,7 @@ pub unsafe trait FromPyPointer<'p>: Sized {
py: Python<'p>, py: Python<'p>,
ptr: *mut ffi::PyObject, ptr: *mut ffi::PyObject,
) -> PyResult<&'p Self> { ) -> PyResult<&'p Self> {
match Self::from_borrowed_ptr_or_opt(py, ptr) { Self::from_borrowed_ptr_or_opt(py, ptr).ok_or_else(|| err::PyErr::fetch(py))
Some(s) => Ok(s),
None => Err(err::PyErr::fetch(py)),
}
} }
} }