|
| 1 | +#![cfg(feature = "macros")] |
| 2 | + |
| 3 | +use pyo3::types::{PyDict, PyString}; |
| 4 | +use pyo3::{prelude::*, IntoPyObject}; |
| 5 | + |
| 6 | +#[macro_use] |
| 7 | +#[path = "../src/tests/common.rs"] |
| 8 | +mod common; |
| 9 | + |
| 10 | +#[derive(Debug, IntoPyObject)] |
| 11 | +pub struct A<'py> { |
| 12 | + s: String, |
| 13 | + t: Bound<'py, PyString>, |
| 14 | + p: Bound<'py, PyAny>, |
| 15 | +} |
| 16 | + |
| 17 | +#[test] |
| 18 | +fn test_named_fields_struct() { |
| 19 | + Python::with_gil(|py| { |
| 20 | + let a = A { |
| 21 | + s: "Hello".into(), |
| 22 | + t: PyString::new(py, "World"), |
| 23 | + p: 42i32.into_pyobject(py).unwrap().into_any(), |
| 24 | + }; |
| 25 | + let pya = a.into_pyobject(py).unwrap(); |
| 26 | + assert_eq!( |
| 27 | + pya.get_item("s") |
| 28 | + .unwrap() |
| 29 | + .unwrap() |
| 30 | + .downcast::<PyString>() |
| 31 | + .unwrap(), |
| 32 | + "Hello" |
| 33 | + ); |
| 34 | + assert_eq!( |
| 35 | + pya.get_item("t") |
| 36 | + .unwrap() |
| 37 | + .unwrap() |
| 38 | + .downcast::<PyString>() |
| 39 | + .unwrap(), |
| 40 | + "World" |
| 41 | + ); |
| 42 | + assert_eq!( |
| 43 | + pya.get_item("p") |
| 44 | + .unwrap() |
| 45 | + .unwrap() |
| 46 | + .extract::<i32>() |
| 47 | + .unwrap(), |
| 48 | + 42 |
| 49 | + ); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Debug, IntoPyObject)] |
| 54 | +#[pyo3(transparent)] |
| 55 | +pub struct B<'a> { |
| 56 | + test: &'a str, |
| 57 | +} |
| 58 | + |
| 59 | +#[test] |
| 60 | +fn test_transparent_named_field_struct() { |
| 61 | + Python::with_gil(|py| { |
| 62 | + let pyb = B { test: "test" }.into_pyobject(py).unwrap(); |
| 63 | + let b = pyb.extract::<String>().unwrap(); |
| 64 | + assert_eq!(b, "test"); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +#[derive(Debug, IntoPyObject)] |
| 69 | +#[pyo3(transparent)] |
| 70 | +pub struct D<T> { |
| 71 | + test: T, |
| 72 | +} |
| 73 | + |
| 74 | +#[test] |
| 75 | +fn test_generic_transparent_named_field_struct() { |
| 76 | + Python::with_gil(|py| { |
| 77 | + let pyd = D { |
| 78 | + test: String::from("test"), |
| 79 | + } |
| 80 | + .into_pyobject(py) |
| 81 | + .unwrap(); |
| 82 | + let d = pyd.extract::<String>().unwrap(); |
| 83 | + assert_eq!(d, "test"); |
| 84 | + |
| 85 | + let pyd = D { test: 1usize }.into_pyobject(py).unwrap(); |
| 86 | + let d = pyd.extract::<usize>().unwrap(); |
| 87 | + assert_eq!(d, 1); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +#[derive(Debug, IntoPyObject)] |
| 92 | +pub struct Tuple(String, usize); |
| 93 | + |
| 94 | +#[test] |
| 95 | +fn test_tuple_struct() { |
| 96 | + Python::with_gil(|py| { |
| 97 | + let tup = Tuple(String::from("test"), 1).into_pyobject(py).unwrap(); |
| 98 | + assert!(tup.extract::<(usize, String)>().is_err()); |
| 99 | + let tup = tup.extract::<(String, usize)>().unwrap(); |
| 100 | + assert_eq!(tup.0, "test"); |
| 101 | + assert_eq!(tup.1, 1); |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +#[derive(Debug, IntoPyObject)] |
| 106 | +pub struct TransparentTuple(String); |
| 107 | + |
| 108 | +#[test] |
| 109 | +fn test_transparent_tuple_struct() { |
| 110 | + Python::with_gil(|py| { |
| 111 | + let tup = TransparentTuple(String::from("test")) |
| 112 | + .into_pyobject(py) |
| 113 | + .unwrap(); |
| 114 | + assert!(tup.extract::<(String,)>().is_err()); |
| 115 | + let tup = tup.extract::<String>().unwrap(); |
| 116 | + assert_eq!(tup, "test"); |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +#[derive(Debug, IntoPyObject)] |
| 121 | +pub enum Foo<'py> { |
| 122 | + TupleVar(usize, String), |
| 123 | + StructVar { |
| 124 | + test: Bound<'py, PyString>, |
| 125 | + }, |
| 126 | + #[pyo3(transparent)] |
| 127 | + TransparentTuple(usize), |
| 128 | + #[pyo3(transparent)] |
| 129 | + TransparentStructVar { |
| 130 | + a: Option<String>, |
| 131 | + }, |
| 132 | +} |
| 133 | + |
| 134 | +#[test] |
| 135 | +fn test_enum() { |
| 136 | + Python::with_gil(|py| { |
| 137 | + let foo = Foo::TupleVar(1, "test".into()).into_pyobject(py).unwrap(); |
| 138 | + assert_eq!( |
| 139 | + foo.extract::<(usize, String)>().unwrap(), |
| 140 | + (1, String::from("test")) |
| 141 | + ); |
| 142 | + |
| 143 | + let foo = Foo::StructVar { |
| 144 | + test: PyString::new(py, "test"), |
| 145 | + } |
| 146 | + .into_pyobject(py) |
| 147 | + .unwrap() |
| 148 | + .downcast_into::<PyDict>() |
| 149 | + .unwrap(); |
| 150 | + |
| 151 | + assert_eq!( |
| 152 | + foo.get_item("test") |
| 153 | + .unwrap() |
| 154 | + .unwrap() |
| 155 | + .downcast_into::<PyString>() |
| 156 | + .unwrap(), |
| 157 | + "test" |
| 158 | + ); |
| 159 | + |
| 160 | + let foo = Foo::TransparentTuple(1).into_pyobject(py).unwrap(); |
| 161 | + assert_eq!(foo.extract::<usize>().unwrap(), 1); |
| 162 | + |
| 163 | + let foo = Foo::TransparentStructVar { a: None } |
| 164 | + .into_pyobject(py) |
| 165 | + .unwrap(); |
| 166 | + assert!(foo.is_none()); |
| 167 | + }); |
| 168 | +} |
0 commit comments