wjakob / nanobind

nanobind: tiny and efficient C++/Python bindings
BSD 3-Clause "New" or "Revised" License
2.29k stars 191 forks source link

Model relinquished instances as a separate state from not-ready #591

Closed oremanj closed 4 months ago

oremanj commented 4 months ago

Instances become relinquished when the object they wrap has its ownership transferred to a C++ unique_ptr. Unlike ready instances, they can't be validly accessed (because the C++ object might have been destroyed without our knowledge). Unlike non-ready instances, they can't be validly constructed (because the C++ object might still be within its lifetime). They need a new state. This PR handles them by expanding the previous nb_inst::ready flag into a two-bit field nb_inst::state. Due to a careful choice of which number represents which state, this should have negligible overhead.

Fixes #550.

wjakob commented 4 months ago

Awesome, this looks great!