From 378a6a484c385b7e848528fef97a6f8cf8a95016 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sun, 19 Apr 2020 12:57:31 +0200 Subject: [PATCH] Move `TryFromPyCell` trait to `derive_utils` module --- src/class/iter.rs | 2 +- src/derive_utils.rs | 26 ++++++++++++++++++++++++-- src/pycell.rs | 21 --------------------- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/class/iter.rs b/src/class/iter.rs index cf7cb405..08c12963 100644 --- a/src/class/iter.rs +++ b/src/class/iter.rs @@ -3,8 +3,8 @@ //! Trait and support implementation for implementing iterators use crate::callback::IntoPyCallbackOutput; +use crate::derive_utils::TryFromPyCell; use crate::err::PyResult; -use crate::pycell::TryFromPyCell; use crate::{ffi, IntoPy, IntoPyPointer, PyClass, PyObject, Python}; /// Python Iterator Interface. diff --git a/src/derive_utils.rs b/src/derive_utils.rs index ab180e87..e70d5842 100644 --- a/src/derive_utils.rs +++ b/src/derive_utils.rs @@ -4,13 +4,13 @@ //! Functionality for the code generated by the derive backend -use crate::err::PyResult; +use crate::err::{PyErr, PyResult}; use crate::exceptions::TypeError; use crate::instance::PyNativeType; use crate::pyclass::PyClass; use crate::pyclass_init::PyClassInitializer; use crate::types::{PyAny, PyDict, PyModule, PyTuple}; -use crate::{ffi, GILPool, IntoPy, PyObject, Python}; +use crate::{ffi, GILPool, IntoPy, PyCell, PyObject, Python}; use std::cell::UnsafeCell; /// Description of a python parameter; used for `parse_args()`. @@ -243,3 +243,25 @@ where { type Target = T; } + +/// 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; + fn try_from_pycell(cell: &'a crate::PyCell) -> Result; +} + +impl<'a, T, R> TryFromPyCell<'a, T> for R +where + T: 'a + PyClass, + R: std::convert::TryFrom<&'a PyCell>, + R::Error: Into, +{ + type Error = R::Error; + fn try_from_pycell(cell: &'a crate::PyCell) -> Result { + >>::try_from(cell) + } +} diff --git a/src/pycell.rs b/src/pycell.rs index b5ec6641..82dcf9ba 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -647,27 +647,6 @@ impl fmt::Debug for PyRefMut<'_, T> { } } -/// 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`. -pub trait TryFromPyCell<'a, T: PyClass>: Sized { - type Error: Into; - fn try_from_pycell(cell: &'a crate::PyCell) -> Result; -} - -impl<'a, T, R> TryFromPyCell<'a, T> for R -where - T: 'a + PyClass, - R: std::convert::TryFrom<&'a PyCell>, - R::Error: Into, -{ - type Error = R::Error; - fn try_from_pycell(cell: &'a crate::PyCell) -> Result { - >>::try_from(cell) - } -} - #[derive(Copy, Clone, Eq, PartialEq)] struct BorrowFlag(usize);