diff --git a/include/libpy/to_object.h b/include/libpy/to_object.h index e0efb1e6..d9be3b28 100644 --- a/include/libpy/to_object.h +++ b/include/libpy/to_object.h @@ -67,25 +67,28 @@ py::owned_ref<> to_object(T&& ob) { } namespace dispatch { -template -py::owned_ref<> sequence_to_list(const C& v) { - py::owned_ref out(PyList_New(v.size())); - if (!out) { - return nullptr; - } - std::size_t ix = 0; - for (const auto& elem : v) { - PyObject* ob = py::to_object(elem).escape(); - if (!ob) { +template +struct sequence_to_object { + static py::owned_ref<> f(const C& v) { + py::owned_ref out(PyList_New(v.size())); + if (!out) { return nullptr; } - PyList_SET_ITEM(out.get(), ix++, ob); - } + std::size_t ix = 0; + for (const auto& elem : v) { + PyObject* ob = py::to_object(elem).escape(); + if (!ob) { + return nullptr; + } - return out; -} + PyList_SET_ITEM(out.get(), ix++, ob); + } + + return out; + } +}; template struct to_object> { @@ -102,11 +105,7 @@ struct to_object { }; template -struct to_object> { - static py::owned_ref<> f(const std::array& v) { - return sequence_to_list(v); - } -}; +struct to_object> : public sequence_to_object> {}; /** Identity conversion for `owned_ref`. */ @@ -273,11 +272,7 @@ struct to_object> : public map_to_object> {}; template -struct to_object> { - static py::owned_ref<> f(const std::vector& v) { - return sequence_to_list(v); - } -}; +struct to_object> : public sequence_to_object> {}; template struct set_to_object {