Skip to content

Commit

Permalink
restore TryFromPyCell for 0.18 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Mar 23, 2023
1 parent 37e5f54 commit cf94106
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/derive_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,29 @@

//! Functionality for the code generated by the derive backend

use crate::{types::PyModule, Python};
use crate::{types::PyModule, PyCell, PyClass, PyErr, Python};

/// A trait for types that can be borrowed from a cell.
///
/// This serves to unify the use of `PyRef` and `PyRefMut` in automatically
/// derived code, since both types can be obtained from a `PyCell`.
#[doc(hidden)]
pub trait TryFromPyCell<'a, T: PyClass>: Sized {
type Error: Into<PyErr>;
fn try_from_pycell(cell: &'a crate::PyCell<T>) -> Result<Self, Self::Error>;
}

impl<'a, T, R> TryFromPyCell<'a, T> for R
where
T: 'a + PyClass,
R: std::convert::TryFrom<&'a PyCell<T>>,
R::Error: Into<PyErr>,
{
type Error = R::Error;
fn try_from_pycell(cell: &'a crate::PyCell<T>) -> Result<Self, Self::Error> {
<R as std::convert::TryFrom<&'a PyCell<T>>>::try_from(cell)
}
}

/// Enum to abstract over the arguments of Python function wrappers.
pub enum PyFunctionArguments<'a> {
Expand Down

0 comments on commit cf94106

Please sign in to comment.