diff --git a/tests/test_type_caster_bare_interface_demo.cpp b/tests/test_type_caster_bare_interface_demo.cpp index 78f6fb5dd..12e98f5d2 100644 --- a/tests/test_type_caster_bare_interface_demo.cpp +++ b/tests/test_type_caster_bare_interface_demo.cpp @@ -27,6 +27,12 @@ std::shared_ptr rtrn_mpty_shcp() { return std::shared_ptr) { return "load_shmp"; } const char* pass_mpty_shcp(std::shared_ptr) { return "load_shcp"; } +std::unique_ptr rtrn_mpty_uqmp() { return std::unique_ptr(new mpty); } +std::unique_ptr rtrn_mpty_uqcp() { return std::unique_ptr(new mpty); } + +const char* pass_mpty_uqmp(std::unique_ptr) { return "load_uqmp"; } +const char* pass_mpty_uqcp(std::unique_ptr) { return "load_uqcp"; } + } // namespace type_caster_bare_interface_demo } // namespace pybind11_tests @@ -135,6 +141,44 @@ struct type_caster> { } }; +template <> +struct type_caster> { + static constexpr auto name = _>(); + + static handle cast(std::unique_ptr&& /*src*/, + return_value_policy /*policy*/, + handle /*parent*/) { + return str("cast_uqmp").release(); + } + + template using cast_op_type = std::unique_ptr; + + operator std::unique_ptr() { return rtrn_mpty_uqmp(); } + + bool load(handle /*src*/, bool /*convert*/) { + return true; + } +}; + +template <> +struct type_caster> { + static constexpr auto name = _>(); + + static handle cast(std::unique_ptr&& /*src*/, + return_value_policy /*policy*/, + handle /*parent*/) { + return str("cast_uqcp").release(); + } + + template using cast_op_type = std::unique_ptr; + + operator std::unique_ptr() { return rtrn_mpty_uqcp(); } + + bool load(handle /*src*/, bool /*convert*/) { + return true; + } +}; + } // namespace detail } // namespace pybind11 @@ -161,6 +205,12 @@ TEST_SUBMODULE(type_caster_bare_interface_demo, m) { m.def("pass_mpty_shmp", pass_mpty_shmp); m.def("pass_mpty_shcp", pass_mpty_shcp); + + m.def("rtrn_mpty_uqmp", rtrn_mpty_uqmp); + m.def("rtrn_mpty_uqcp", rtrn_mpty_uqcp); + + m.def("pass_mpty_uqmp", pass_mpty_uqmp); + m.def("pass_mpty_uqcp", pass_mpty_uqcp); } } // namespace type_caster_bare_interface_demo diff --git a/tests/test_type_caster_bare_interface_demo.py b/tests/test_type_caster_bare_interface_demo.py index 97e34c8df..35ae0ee29 100644 --- a/tests/test_type_caster_bare_interface_demo.py +++ b/tests/test_type_caster_bare_interface_demo.py @@ -30,3 +30,13 @@ def test_cast_shared_ptr(): def test_load_shared_ptr(): assert m.pass_mpty_shmp(None) == "load_shmp" assert m.pass_mpty_shcp(None) == "load_shcp" + + +def test_cast_unique_ptr(): + assert m.rtrn_mpty_uqmp() == "cast_uqmp" + assert m.rtrn_mpty_uqcp() == "cast_uqcp" + + +def test_load_unique_ptr(): + assert m.pass_mpty_uqmp(None) == "load_uqmp" + assert m.pass_mpty_uqcp(None) == "load_uqcp"