Support ownership transfer for unique_ptr<T>
(move-only holders) from Python to C++ (and back)
#1132
Labels
holders
Issues and PRs regarding holders
I had reviewed some of the prior questions on GitHub, StackOverflow, etc, and found that accepting a
unique_ptr<T>
from Python is not presently possible (ref: one of the GitHub issues).I think that this can be done, and have made a (rough, but not too rough) prototype of doing so that I believe is generally comprehensive for most of the simple edge cases.
branch | head commit (as of writing)
Demo Usage
Code sampled from this pseudo-test case.
Should print something like:
Functionality Overview
move_only_holder_caster
toload
values. If a Python value is loaded into aunique_ptr<T>
, the object is "released" to C++ (see here):pybind11::move<>
).pybind11
can yank the instance out without disturbing the arguments (possible implementation).pybind11::move<>()
for it to work (see commented-out code).type_caster_generic::load_impl<>()
), use the type's "release" mechanism):pybind
instance into a non-owned instance, and let it destruct once its reference count depletes at the end of the caster.pybind11::trampoline
in the prototype).pybind11::object
reference to it in the trampoline wrapper.type_caster_generic::cast()
could have a new clause:Caveats / Limitations
trampoline
wrapper affects destructor order if the object is destroyed in C++. See note here with a workaround.keep_alive
to the weak reference (and this tie can be broken if ownership is transferred back).weak_ref
workflows.default_holder
case. Unless someone actually has their own custom move-only holder, I'd prefer to keep it this way for simplicity's sake.internals
andinstance
. These could be reduced some, if need be, but there'd need to be at least one field.Followup
I would really like to have this feature, so please do let me know if this seems like viable feature option. I am more than happy to submit a PR and go through the review process if so desired (which would be a much more polished, properly unit-tested version of the prototype branch).
\cc @jagerman
The text was updated successfully, but these errors were encountered: