-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce traits to make ffi ptr handling cleaner
- Loading branch information
1 parent
1583828
commit 6b18a93
Showing
7 changed files
with
111 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::{ffi, instance::Py2, PyAny, PyResult, Python}; | ||
|
||
mod ffi_ptr_ext_sealed { | ||
use super::*; | ||
|
||
pub trait Sealed {} | ||
|
||
impl Sealed for *mut ffi::PyObject {} | ||
} | ||
|
||
use ffi_ptr_ext_sealed::Sealed; | ||
|
||
pub(crate) trait FfiPtrExt: Sealed { | ||
unsafe fn assume_owned_or_ffi_error<'py>(self, py: Python<'py>) -> PyResult<Py2<'py, PyAny>>; | ||
unsafe fn assume_owned_and_infallible<'py>(self, py: Python<'py>) -> Py2<'py, PyAny>; | ||
} | ||
|
||
impl FfiPtrExt for *mut ffi::PyObject { | ||
#[inline] | ||
unsafe fn assume_owned_or_ffi_error<'py>(self, py: Python<'py>) -> PyResult<Py2<'py, PyAny>> { | ||
Py2::from_owned_ptr_or_err(py, self) | ||
} | ||
|
||
#[inline] | ||
unsafe fn assume_owned_and_infallible<'py>(self, py: Python<'py>) -> Py2<'py, PyAny> { | ||
Py2::from_owned_ptr(py, self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use crate::{types::any::PyAnyMethods, Py2, PyAny, PyResult}; | ||
|
||
mod py_result_ext_sealed { | ||
use super::*; | ||
|
||
pub trait Sealed {} | ||
|
||
impl Sealed for PyResult<Py2<'_, PyAny>> {} | ||
} | ||
|
||
use py_result_ext_sealed::Sealed; | ||
|
||
pub(crate) trait PyResultExt<'py>: Sealed { | ||
unsafe fn downcast_into_unchecked<T>(self) -> PyResult<Py2<'py, T>>; | ||
} | ||
|
||
impl<'py> PyResultExt<'py> for PyResult<Py2<'py, PyAny>> { | ||
#[inline] | ||
unsafe fn downcast_into_unchecked<T>(self) -> PyResult<Py2<'py, T>> { | ||
self.map(|instance| instance.downcast_into_unchecked()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters