Skip to content

Commit

Permalink
Merge pull request #1613 from davidhewitt/inline-callback
Browse files Browse the repository at this point in the history
callback: add `#[inline]` to callback conversion code
  • Loading branch information
davidhewitt committed May 19, 2021
2 parents 95d7fa2 + cb2b70b commit 8bb72a7
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ where
T: IntoPyCallbackOutput<U>,
E: Into<PyErr>,
{
#[inline]
fn convert(self, py: Python) -> PyResult<U> {
self.map_err(Into::into).and_then(|t| t.convert(py))
}
Expand All @@ -54,30 +55,35 @@ impl<T> IntoPyCallbackOutput<*mut ffi::PyObject> for T
where
T: IntoPy<PyObject>,
{
#[inline]
fn convert(self, py: Python) -> PyResult<*mut ffi::PyObject> {
Ok(self.into_py(py).into_ptr())
}
}

impl IntoPyCallbackOutput<Self> for *mut ffi::PyObject {
#[inline]
fn convert(self, _: Python) -> PyResult<Self> {
Ok(self)
}
}

impl IntoPyCallbackOutput<std::os::raw::c_int> for () {
#[inline]
fn convert(self, _: Python) -> PyResult<std::os::raw::c_int> {
Ok(0)
}
}

impl IntoPyCallbackOutput<std::os::raw::c_int> for bool {
#[inline]
fn convert(self, _: Python) -> PyResult<std::os::raw::c_int> {
Ok(self as c_int)
}
}

impl IntoPyCallbackOutput<()> for () {
#[inline]
fn convert(self, _: Python) -> PyResult<()> {
Ok(())
}
Expand All @@ -97,12 +103,14 @@ impl IntoPyCallbackOutput<ffi::Py_ssize_t> for usize {
// Converters needed for `#[pyproto]` implementations

impl IntoPyCallbackOutput<bool> for bool {
#[inline]
fn convert(self, _: Python) -> PyResult<bool> {
Ok(self)
}
}

impl IntoPyCallbackOutput<usize> for usize {
#[inline]
fn convert(self, _: Python) -> PyResult<usize> {
Ok(self)
}
Expand All @@ -112,6 +120,7 @@ impl<T> IntoPyCallbackOutput<PyObject> for T
where
T: IntoPy<PyObject>,
{
#[inline]
fn convert(self, py: Python) -> PyResult<PyObject> {
Ok(self.into_py(py))
}
Expand Down Expand Up @@ -225,6 +234,7 @@ macro_rules! callback_body {
/// Then this will fail to compile, because the result of #foo borrows _slf, but _slf drops when
/// the block passed to the macro ends.
#[doc(hidden)]
#[inline]
pub unsafe fn handle_panic<F, R>(body: F) -> R
where
F: FnOnce(Python) -> PyResult<R> + UnwindSafe,
Expand Down

0 comments on commit 8bb72a7

Please sign in to comment.