From 60d1565a8fc1cb456485fd6cb71c790844f1dc09 Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 12 Jun 2018 17:47:24 +0200 Subject: [PATCH] Always clone on in getters Since copy implies clone, this doesn't code. --- CHANGELOG.md | 4 ++++ pyo3-derive-backend/src/py_class.rs | 2 +- tests/test_getter_setter.rs | 5 ++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68583dee..7a1dec27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +* Always clone in getters. This allows using the get-annotation on all Clone-Types + ## 0.2.7 (2018-05-18) * Fix nightly breakage with proc_macro_path diff --git a/pyo3-derive-backend/src/py_class.rs b/pyo3-derive-backend/src/py_class.rs index d2a8c7dc..89625781 100644 --- a/pyo3-derive-backend/src/py_class.rs +++ b/pyo3-derive-backend/src/py_class.rs @@ -302,7 +302,7 @@ fn impl_descriptors( quote! { impl #cls { fn #name(&self) -> _pyo3::PyResult<#field_ty> { - Ok(self.#name) + Ok(self.#name.clone()) } } } diff --git a/tests/test_getter_setter.rs b/tests/test_getter_setter.rs index b7dfa874..a7616669 100644 --- a/tests/test_getter_setter.rs +++ b/tests/test_getter_setter.rs @@ -54,6 +54,8 @@ fn class_with_properties() { struct GetterSetter { #[prop(get, set)] num: i32, + #[prop(get, set)] + text: String, token: PyToken } @@ -70,8 +72,9 @@ fn getter_setter_autogen() { let gil = Python::acquire_gil(); let py = gil.python(); - let inst = py.init(|t| GetterSetter{num: 10, token: t}).unwrap(); + let inst = py.init(|t| GetterSetter{num: 10, token: t, text: "Hello".to_string()}).unwrap(); py_run!(py, inst, "assert inst.num == 10"); py_run!(py, inst, "inst.num = 20; assert inst.num == 20"); + py_run!(py, inst, "assert inst.text == 'Hello'; inst.text = 'There'; assert inst.text == 'There'"); }