-
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
728b93e
commit 090d7e1
Showing
7 changed files
with
109 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 sealed { | ||
use super::*; | ||
|
||
pub trait Sealed {} | ||
|
||
impl Sealed for *mut ffi::PyObject {} | ||
} | ||
|
||
use sealed::Sealed; | ||
|
||
pub(crate) trait FfiPtrExt: Sealed { | ||
unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Py2<'_, PyAny>>; | ||
unsafe fn assume_owned(self, py: Python<'_>) -> Py2<'_, PyAny>; | ||
} | ||
|
||
impl FfiPtrExt for *mut ffi::PyObject { | ||
#[inline] | ||
unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Py2<'_, PyAny>> { | ||
Py2::from_owned_ptr_or_err(py, self) | ||
} | ||
|
||
#[inline] | ||
unsafe fn assume_owned(self, py: Python<'_>) -> Py2<'_, 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 sealed { | ||
use super::*; | ||
|
||
pub trait Sealed {} | ||
|
||
impl Sealed for PyResult<Py2<'_, PyAny>> {} | ||
} | ||
|
||
use 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