Skip to content

Commit 2f337d6

Browse files
Xiaofei Wangcopybara-github
authored andcommitted
Add operator const ProtoType&& to proto caster.
PiperOrigin-RevId: 469581055
1 parent 8dbb33b commit 2f337d6

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

pybind11_protobuf/proto_caster_impl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ struct proto_caster : public proto_caster_load_impl<ProtoType>,
269269
ensure_owned();
270270
return std::move(*owned);
271271
}
272-
272+
explicit operator const ProtoType &&() {
273+
if (!value) throw pybind11::reference_cast_error();
274+
ensure_owned();
275+
return std::move(*owned);
276+
}
273277
#if PYBIND11_PROTOBUF_UNSAFE
274278
// The following unsafe conversions are not enabled:
275279
explicit operator ProtoType *() { return const_cast<ProtoType *>(value); }

pybind11_protobuf/tests/pass_by_module.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ PYBIND11_MODULE(pass_by_module, m) {
8686
return CheckIntMessage(&message, value);
8787
},
8888
py::arg("message"), py::arg("value"));
89+
m.def(
90+
"concrete_crval",
91+
[](const IntMessage&& message, int value) {
92+
return CheckIntMessage(&message, value);
93+
},
94+
py::arg("message"), py::arg("value"));
8995
m.def(
9096
"concrete_cref",
9197
[](const IntMessage& message, int value) {

pybind11_protobuf/tests/pass_by_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def get_pass_by_params():
5353
'concrete_ptr_notnone',
5454
'concrete_ref',
5555
'concrete_rval',
56+
'concrete_crval',
5657
'concrete_sptr',
5758
'concrete_uptr',
5859
'concrete_uptr_ptr',

0 commit comments

Comments
 (0)