diff --git a/crates/cpp/helper-types/wit-common.h b/crates/cpp/helper-types/wit-common.h index 771f2b8d4..014b9e288 100644 --- a/crates/cpp/helper-types/wit-common.h +++ b/crates/cpp/helper-types/wit-common.h @@ -29,7 +29,7 @@ template class span { const_iterator begin() const { return address; } const_iterator end() const { return address + length; } bool empty() const { return !length; } - T const &operator[](size_t index) { return address[index]; } + T const &operator[](size_t index) const { return address[index]; } // create from any compatible vector (borrows data!) template span(std::vector const &vec) : address(vec.data()), length(vec.size()) {} diff --git a/crates/cpp/helper-types/wit-guest.h b/crates/cpp/helper-types/wit-guest.h index 8ee598666..fdfb9cfc9 100644 --- a/crates/cpp/helper-types/wit-guest.h +++ b/crates/cpp/helper-types/wit-guest.h @@ -94,6 +94,8 @@ template class vector { // typically called by post static void drop_raw(void *ptr) { free(ptr); } wit::span get_view() const { return wit::span(data_, length); } + template static vector from_view(wit::span const& a); +// static vector from_view(wit::span const& a); }; /// @brief A Resource defined within the guest (guest side) diff --git a/tests/runtime/lists/wasm.new.cpp b/tests/runtime/lists/wasm.new.cpp index 9e383be40..68dd99e8c 100644 --- a/tests/runtime/lists/wasm.new.cpp +++ b/tests/runtime/lists/wasm.new.cpp @@ -8,11 +8,21 @@ uint32_t exports::lists::AllocatedBytes() { template static bool equal(wit::vector const&a, wit::span const& b); template +static bool equal(wit::span const&a, wit::vector const& b); +template +static bool equal(wit::span const&a, std::vector const& b); +template static bool equal(wit::vector const&a, std::vector const& b); static bool equal(wit::string const&a, std::string_view b) { return a.get_view() == b; } +template +static bool equal(T const&a, T const& b) { + return a == b; +} static bool equal(wit::vector const&a, std::vector const& b); +template +static bool equal(std::tuple const&a, std::tuple const& b); void exports::lists::TestImports() { //let _guard = testRust_wasm::guard(); @@ -34,8 +44,8 @@ void exports::lists::TestImports() { assert(equal(test::lists::test::ListResult3(), std::vector{"hello,", "world!"})); assert(equal(test::lists::test::ListRoundtrip(wit::span(std::vector())), std::vector())); - assert(equal(test::lists::test::ListRoundtrip(wit::span(std::vector{'x'})), std::vector{'x'})); - assert(equal(test::lists::test::ListRoundtrip(wit::span(std::vector{'h', 'e', 'l', 'l', 'o'})), std::vector{'h', 'e', 'l', 'l', 'o'})); + assert(equal(test::lists::test::ListRoundtrip(wit::span(std::vector{'x'})), std::vector{'x'})); + assert(equal(test::lists::test::ListRoundtrip(wit::span(std::vector{'h', 'e', 'l', 'l', 'o'})), std::vector{'h', 'e', 'l', 'l', 'o'})); assert(equal(test::lists::test::StringRoundtrip("x"), "x")); assert(equal(test::lists::test::StringRoundtrip(""), "")); @@ -92,25 +102,25 @@ void exports::test::lists::test::ListParam(wit::span list) { } void exports::test::lists::test::ListParam2(std::string_view ptr) { - assert(equal(ptr, "foo")); + assert(equal(ptr, std::string_view("foo"))); } void exports::test::lists::test::ListParam3(wit::span ptr) { - assert(equal(ptr.size(), 3)); - assert(equal(ptr[0], "foo")); - assert(equal(ptr[1], "bar")); - assert(equal(ptr[2], "baz")); + assert(equal(ptr.size(), size_t(3))); + assert(equal(ptr[0], std::string_view("foo"))); + assert(equal(ptr[1], std::string_view("bar"))); + assert(equal(ptr[2], std::string_view("baz"))); } void exports::test::lists::test::ListParam4(wit::span> ptr) { - assert(equal(ptr.size(), 2)); - assert(equal(ptr[0][0], "foo")); - assert(equal(ptr[0][1], "bar")); - assert(equal(ptr[1][0], "baz")); + assert(equal(ptr.size(), size_t(2))); + assert(equal(ptr[0][0], std::string_view("foo"))); + assert(equal(ptr[0][1], std::string_view("bar"))); + assert(equal(ptr[1][0], std::string_view("baz"))); } wit::vector exports::test::lists::test::ListResult() { - return std::vector{1, 2, 3, 4, 5}; + return wit::vector::from_view(wit::span(std::vector{1, 2, 3, 4, 5})); } wit::string exports::test::lists::test::ListResult2() { @@ -118,11 +128,11 @@ wit::string exports::test::lists::test::ListResult2() { } wit::vector exports::test::lists::test::ListResult3() { - return std::vector{wit::string::from_view("hello,"), wit::string::from_view("world!")}; + return wit::vector::from_view(wit::span(std::vector{wit::string::from_view("hello,"), wit::string::from_view("world!")})); } wit::vector exports::test::lists::test::ListRoundtrip(wit::span x) { - return wit::vector::from_span(x); + return wit::vector::from_view(x); } wit::string exports::test::lists::test::StringRoundtrip(std::string_view x) { @@ -130,21 +140,21 @@ wit::string exports::test::lists::test::StringRoundtrip(std::string_view x) { } std::tuple, wit::vector> exports::test::lists::test::ListMinmax8(wit::span a, wit::span b) { - return std::make_tuple(a, b); + return std::make_tuple(wit::vector::from_view(a), wit::vector::from_view(b)); } std::tuple, wit::vector> exports::test::lists::test::ListMinmax16(wit::span a, wit::span b) { - return std::make_tuple(a, b); + return std::make_tuple(wit::vector::from_view(a), wit::vector::from_view(b)); } std::tuple, wit::vector> exports::test::lists::test::ListMinmax32(wit::span a, wit::span b) { - return std::make_tuple(a, b); + return std::make_tuple(wit::vector::from_view(a), wit::vector::from_view(b)); } std::tuple, wit::vector> exports::test::lists::test::ListMinmax64(wit::span a, wit::span b) { - return std::make_tuple(a, b); + return std::make_tuple(wit::vector::from_view(a), wit::vector::from_view(b)); } std::tuple, wit::vector> exports::test::lists::test::ListMinmaxFloat(wit::span a, wit::span b) { - return std::make_tuple(a, b); + return std::make_tuple(wit::vector::from_view(a), wit::vector::from_view(b)); }