Skip to content

Commit

Permalink
nb::inst*_copy/move: do not do anything when src == dst
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Apr 2, 2024
1 parent 97357f1 commit 369e79a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
21 changes: 10 additions & 11 deletions docs/api_core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2631,17 +2631,13 @@ The documentation below refers to two per-instance flags with the following mean
:cpp:func:`inst_set_state` to disable the flag following the call to
:cpp:func:`inst_copy`.

*New in nanobind v2.0.0*: The function is a no-op when ``src`` and ``dst``
refer to the same object.

.. cpp:function:: void inst_move(handle dst, handle src)

Move-construct the contents of `src` into `dst` and set the *ready* and
*destruct* flags of `dst` to ``true``.

`dst` should be an uninitialized instance of the same type. Note that
setting the *destruct* flag may be problematic if `dst` is an offset into an
existing object created using :cpp:func:`inst_reference` (the destructor
will be called multiple times in this case). If so, you must use
:cpp:func:`inst_set_state` to disable the flag following the call to
:cpp:func:`inst_move`.
Analogous to :cpp:func:`inst_copy`, except that the move constructor
is used instead of the copy constructor.

.. cpp:function:: void inst_replace_copy(handle dst, handle src)

Expand All @@ -2655,10 +2651,13 @@ The documentation below refers to two per-instance flags with the following mean
:cpp:func:`inst_alloc`, :cpp:func:`inst_reference`, or
:cpp:func:`inst_take_ownership`.

*New in nanobind v2.0.0*: The function is a no-op when ``src`` and ``dst``
refer to the same object.

.. cpp:function:: void inst_replace_move(handle dst, handle src)

Analogous to :cpp:func:`inst_replace_copy`, except that a move constructor
is used here.
Analogous to :cpp:func:`inst_replace_copy`, except that the move constructor
is used instead of the copy constructor.

.. cpp:function:: str inst_name(handle h)

Expand Down
10 changes: 10 additions & 0 deletions src/nb_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,9 @@ void nb_inst_destruct(PyObject *o) noexcept {
}

void nb_inst_copy(PyObject *dst, const PyObject *src) noexcept {
if (src == dst)
return;

PyTypeObject *tp = Py_TYPE((PyObject *) src);
type_data *t = nb_type_data(tp);

Expand All @@ -1858,6 +1861,9 @@ void nb_inst_copy(PyObject *dst, const PyObject *src) noexcept {
}

void nb_inst_move(PyObject *dst, const PyObject *src) noexcept {
if (src == dst)
return;

PyTypeObject *tp = Py_TYPE((PyObject *) src);
type_data *t = nb_type_data(tp);

Expand All @@ -1880,6 +1886,8 @@ void nb_inst_move(PyObject *dst, const PyObject *src) noexcept {
}

void nb_inst_replace_move(PyObject *dst, const PyObject *src) noexcept {
if (src == dst)
return;
nb_inst *nbi = (nb_inst *) dst;
bool destruct = nbi->destruct;
nbi->destruct = true;
Expand All @@ -1889,6 +1897,8 @@ void nb_inst_replace_move(PyObject *dst, const PyObject *src) noexcept {
}

void nb_inst_replace_copy(PyObject *dst, const PyObject *src) noexcept {
if (src == dst)
return;
nb_inst *nbi = (nb_inst *) dst;
bool destruct = nbi->destruct;
nbi->destruct = true;
Expand Down

0 comments on commit 369e79a

Please sign in to comment.