pyo3/conversions/std/
vec.rs

1use crate::conversion::IntoPyObject;
2#[cfg(feature = "experimental-inspect")]
3use crate::inspect::types::TypeInfo;
4use crate::{Bound, PyAny, PyErr, Python};
5
6impl<'py, T> IntoPyObject<'py> for Vec<T>
7where
8    T: IntoPyObject<'py>,
9{
10    type Target = PyAny;
11    type Output = Bound<'py, Self::Target>;
12    type Error = PyErr;
13
14    /// Turns [`Vec<u8>`] into [`PyBytes`], all other `T`s will be turned into a [`PyList`]
15    ///
16    /// [`PyBytes`]: crate::types::PyBytes
17    /// [`PyList`]: crate::types::PyList
18    #[inline]
19    fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
20        T::owned_sequence_into_pyobject(self, py, crate::conversion::private::Token)
21    }
22
23    #[cfg(feature = "experimental-inspect")]
24    fn type_output() -> TypeInfo {
25        TypeInfo::list_of(T::type_output())
26    }
27}
28
29impl<'a, 'py, T> IntoPyObject<'py> for &'a Vec<T>
30where
31    &'a T: IntoPyObject<'py>,
32    T: 'a, // MSRV
33{
34    type Target = PyAny;
35    type Output = Bound<'py, Self::Target>;
36    type Error = PyErr;
37
38    #[inline]
39    fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
40        // NB: we could actually not cast to `PyAny`, which would be nice for
41        // `&Vec<u8>`, but that'd be inconsistent with the `IntoPyObject` impl
42        // above which always returns a `PyAny` for `Vec<T>`.
43        self.as_slice().into_pyobject(py).map(Bound::into_any)
44    }
45
46    #[cfg(feature = "experimental-inspect")]
47    fn type_output() -> TypeInfo {
48        TypeInfo::list_of(<&T>::type_output())
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use crate::conversion::IntoPyObject;
55    use crate::types::{PyAnyMethods, PyBytes, PyBytesMethods, PyList};
56    use crate::Python;
57
58    #[test]
59    fn test_vec_intopyobject_impl() {
60        Python::with_gil(|py| {
61            let bytes: Vec<u8> = b"foobar".to_vec();
62            let obj = bytes.clone().into_pyobject(py).unwrap();
63            assert!(obj.is_instance_of::<PyBytes>());
64            let obj = obj.downcast_into::<PyBytes>().unwrap();
65            assert_eq!(obj.as_bytes(), &bytes);
66
67            let nums: Vec<u16> = vec![0, 1, 2, 3];
68            let obj = nums.into_pyobject(py).unwrap();
69            assert!(obj.is_instance_of::<PyList>());
70        });
71    }
72
73    #[test]
74    fn test_vec_reference_intopyobject_impl() {
75        Python::with_gil(|py| {
76            let bytes: Vec<u8> = b"foobar".to_vec();
77            let obj = (&bytes).into_pyobject(py).unwrap();
78            assert!(obj.is_instance_of::<PyBytes>());
79            let obj = obj.downcast_into::<PyBytes>().unwrap();
80            assert_eq!(obj.as_bytes(), &bytes);
81
82            let nums: Vec<u16> = vec![0, 1, 2, 3];
83            let obj = (&nums).into_pyobject(py).unwrap();
84            assert!(obj.is_instance_of::<PyList>());
85        });
86    }
87}
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here