Skip to main content

pyo3/conversions/std/
cell.rs

1use std::cell::Cell;
2
3#[cfg(feature = "experimental-inspect")]
4use crate::inspect::PyStaticExpr;
5use crate::{conversion::IntoPyObject, Borrowed, FromPyObject, PyAny, Python};
6
7impl<'py, T: Copy + IntoPyObject<'py>> IntoPyObject<'py> for Cell<T> {
8    type Target = T::Target;
9    type Output = T::Output;
10    type Error = T::Error;
11
12    #[cfg(feature = "experimental-inspect")]
13    const OUTPUT_TYPE: PyStaticExpr = T::OUTPUT_TYPE;
14
15    #[inline]
16    fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
17        self.get().into_pyobject(py)
18    }
19}
20
21impl<'py, T: Copy + IntoPyObject<'py>> IntoPyObject<'py> for &Cell<T> {
22    type Target = T::Target;
23    type Output = T::Output;
24    type Error = T::Error;
25
26    #[cfg(feature = "experimental-inspect")]
27    const OUTPUT_TYPE: PyStaticExpr = T::OUTPUT_TYPE;
28
29    #[inline]
30    fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
31        self.get().into_pyobject(py)
32    }
33}
34
35impl<'a, 'py, T: FromPyObject<'a, 'py>> FromPyObject<'a, 'py> for Cell<T> {
36    type Error = T::Error;
37
38    #[cfg(feature = "experimental-inspect")]
39    const INPUT_TYPE: PyStaticExpr = T::INPUT_TYPE;
40
41    fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
42        ob.extract().map(Cell::new)
43    }
44}