-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FromPyObject for #[pyclass] with T: Clone
- Loading branch information
1 parent
d2174c2
commit fdf407e
Showing
9 changed files
with
137 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use pyo3::prelude::*; | ||
|
||
#[pyclass] | ||
#[derive(Clone, Debug, PartialEq)] | ||
struct Cloneable { | ||
x: i32, | ||
} | ||
|
||
#[test] | ||
fn test_cloneable_pyclass() { | ||
let c = Cloneable { x: 10 }; | ||
|
||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
|
||
let py_c = Py::new(py, c.clone()).unwrap().to_object(py); | ||
|
||
let c2: Cloneable = py_c.extract(py).unwrap(); | ||
let rc: &Cloneable = py_c.extract(py).unwrap(); | ||
let mrc: &mut Cloneable = py_c.extract(py).unwrap(); | ||
|
||
assert_eq!(c, c2); | ||
assert_eq!(&c, rc); | ||
assert_eq!(&c, mrc); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
#[test] | ||
fn test_compile_errors() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/ui/invalid_pymethod_names.rs"); | ||
t.compile_fail("tests/ui/missing_clone.rs"); | ||
t.compile_fail("tests/ui/reject_generics.rs"); | ||
t.compile_fail("tests/ui/too_many_args_to_getter.rs"); | ||
t.compile_fail("tests/ui/invalid_pymethod_names.rs"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use pyo3::prelude::*; | ||
|
||
#[pyclass] | ||
struct TestClass { | ||
num: u32, | ||
} | ||
|
||
fn main() { | ||
let t = TestClass { num: 10 }; | ||
|
||
let gil = Python::acquire_gil(); | ||
let py = gil.python(); | ||
|
||
let pyvalue = Py::new(py, t).unwrap().to_object(py); | ||
let t: TestClass = pyvalue.extract(py).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error[E0277]: the trait bound `TestClass: std::clone::Clone` is not satisfied | ||
--> $DIR/missing_clone.rs:15:32 | ||
| | ||
15 | let t: TestClass = pyvalue.extract(py).unwrap(); | ||
| ^^^^^^^ the trait `std::clone::Clone` is not implemented for `TestClass` | ||
| | ||
= note: required because of the requirements on the impl of `pyo3::conversion::extract_impl::ExtractImpl<'_, TestClass>` for `pyo3::conversion::extract_impl::Cloned` | ||
= note: required because of the requirements on the impl of `pyo3::conversion::FromPyObject<'_>` for `TestClass` |