From 6a2ae30e749bf6d5ecdec2c6430d07adc9c0dd36 Mon Sep 17 00:00:00 2001 From: maximiliank Date: Wed, 9 Aug 2023 22:17:46 +0200 Subject: [PATCH 1/3] Update nb_list.h to allow for std::vector, see #255 --- include/nanobind/stl/detail/nb_list.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nanobind/stl/detail/nb_list.h b/include/nanobind/stl/detail/nb_list.h index 270a23de..a3ebecd5 100644 --- a/include/nanobind/stl/detail/nb_list.h +++ b/include/nanobind/stl/detail/nb_list.h @@ -59,7 +59,7 @@ template struct list_caster { if (ret.is_valid()) { Py_ssize_t index = 0; - for (auto &value : src) { + for (auto &&value : src) { handle h = Caster::from_cpp(forward_like(value), policy, cleanup); if (!h.is_valid()) { From 2169a3fb9bda4a232d6dd66a2108be2502d93115 Mon Sep 17 00:00:00 2001 From: Maximilian Kleinert Date: Wed, 9 Aug 2023 23:03:41 +0200 Subject: [PATCH 2/3] Added type caster test for std::vector --- tests/test_stl.cpp | 6 ++++++ tests/test_stl.py | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 6ffa1231..aa3fb04f 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -416,4 +416,10 @@ NB_MODULE(test_stl_ext, m) { nb::class_(m, "ClassWithMovableField") .def(nb::init<>()) .def_rw("movable", &ClassWithMovableField::movable); + + // test67 std::vector + m.def("flip_vector_bool", [](std::vector vec) { + vec.flip(); + return vec; + }); } diff --git a/tests/test_stl.py b/tests/test_stl.py index 2fe38a3d..119c2200 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -755,3 +755,7 @@ def __fspath__(self): assert t.parent_path(PseudoStrPath()) == Path("foo") assert t.parent_path(PseudoBytesPath()) == Path("foo") +def test66_vector_bool(): + bool_vector = [True, False, True, False] + result = t.flip_vector_bool(bool_vector) + assert result == [not x for x in bool_vector] From 729174eb55be78e830783778ab6e2dc248c98f8b Mon Sep 17 00:00:00 2001 From: Maximilian Kleinert Date: Wed, 9 Aug 2023 23:09:03 +0200 Subject: [PATCH 3/3] Fixed test name --- tests/test_stl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_stl.py b/tests/test_stl.py index 119c2200..8da3d8e8 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -755,7 +755,7 @@ def __fspath__(self): assert t.parent_path(PseudoStrPath()) == Path("foo") assert t.parent_path(PseudoBytesPath()) == Path("foo") -def test66_vector_bool(): +def test67_vector_bool(): bool_vector = [True, False, True, False] result = t.flip_vector_bool(bool_vector) assert result == [not x for x in bool_vector]