diff --git a/include/cxx.h b/include/cxx.h index 002282551..3414e4c8a 100644 --- a/include/cxx.h +++ b/include/cxx.h @@ -176,6 +176,9 @@ class Slice final Slice() noexcept; Slice(T *, std::size_t count) noexcept; + template + explicit Slice(C& c) : Slice(c.data(), c.size()) {} + Slice &operator=(const Slice &) &noexcept = default; Slice &operator=(Slice &&) &noexcept = default; diff --git a/tests/ffi/tests.cc b/tests/ffi/tests.cc index 8cf74bebb..ca71276f8 100644 --- a/tests/ffi/tests.cc +++ b/tests/ffi/tests.cc @@ -884,6 +884,12 @@ extern "C" const char *cxx_run_test() noexcept { rust::String bad_utf16_rstring = rust::String::lossy(bad_utf16_literal); ASSERT(bad_utf8_rstring == bad_utf16_rstring); + std::vector cpp_vec{1, 2, 3}; + rust::Slice slice_of_cpp_vec(cpp_vec); + ASSERT(slice_of_cpp_vec.data() == cpp_vec.data()); + ASSERT(slice_of_cpp_vec.size() == cpp_vec.size()); + ASSERT(slice_of_cpp_vec[0] == 1); + rust::Vec vec1{1, 2}; rust::Vec vec2{3, 4}; swap(vec1, vec2);