Skip to content

Commit 5702ef8

Browse files
committed
Merge pull request 1449 from JonathonReinhart/add-Slice-ctor-deduction-guide
2 parents bdf2e39 + 33f3fa8 commit 5702ef8

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

include/cxx.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ class Slice final
220220
std::array<std::uintptr_t, 2> repr;
221221
};
222222

223+
#ifdef __cpp_deduction_guides
224+
template <typename C>
225+
explicit Slice(C &c)
226+
-> Slice<std::remove_reference_t<decltype(*std::declval<C>().data())>>;
227+
#endif // __cpp_deduction_guides
228+
223229
template <typename T>
224230
class Slice<T>::iterator final {
225231
public:

tests/ffi/tests.cc

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#include "tests/ffi/tests.h"
22
#include "tests/ffi/lib.rs.h"
3+
#include <array>
34
#include <cstdlib>
45
#include <cstring>
56
#include <iterator>
67
#include <memory>
78
#include <numeric>
9+
#ifdef __cpp_lib_span
10+
#include <span>
11+
#endif // __cpp_lib_span
812
#include <stdexcept>
913
#include <string>
1014
#include <tuple>
@@ -891,11 +895,71 @@ extern "C" const char *cxx_run_test() noexcept {
891895
rust::String bad_utf16_rstring = rust::String::lossy(bad_utf16_literal);
892896
ASSERT(bad_utf8_rstring == bad_utf16_rstring);
893897

894-
std::vector<int> cpp_vec{1, 2, 3};
895-
rust::Slice<int> slice_of_cpp_vec(cpp_vec);
896-
ASSERT(slice_of_cpp_vec.data() == cpp_vec.data());
897-
ASSERT(slice_of_cpp_vec.size() == cpp_vec.size());
898-
ASSERT(slice_of_cpp_vec[0] == 1);
898+
// Test Slice<T> explicit constructor from container
899+
{
900+
std::vector<int> cpp_vec{1, 2, 3};
901+
rust::Slice<int> slice_of_cpp_vec(cpp_vec);
902+
ASSERT(slice_of_cpp_vec.data() == cpp_vec.data());
903+
ASSERT(slice_of_cpp_vec.size() == cpp_vec.size());
904+
ASSERT(slice_of_cpp_vec[0] == 1);
905+
}
906+
907+
// Test Slice<T> template deduction guides
908+
#ifdef __cpp_deduction_guides
909+
{
910+
// std::array<T> -> Slice<T>
911+
std::array<int, 3> cpp_array{1, 2, 3};
912+
auto auto_slice_of_cpp_array = rust::Slice(cpp_array);
913+
static_assert(
914+
std::is_same_v<decltype(auto_slice_of_cpp_array), rust::Slice<int>>);
915+
}
916+
{
917+
// const std::array<T> -> Slice<const T>
918+
const std::array<int, 3> cpp_array{1, 2, 3};
919+
auto auto_slice_of_cpp_array = rust::Slice(cpp_array);
920+
static_assert(std::is_same_v<decltype(auto_slice_of_cpp_array),
921+
rust::Slice<const int>>);
922+
}
923+
{
924+
// std::array<const T> -> Slice<const T>
925+
std::array<const int, 3> cpp_array{1, 2, 3};
926+
auto auto_slice_of_cpp_array = rust::Slice(cpp_array);
927+
static_assert(std::is_same_v<decltype(auto_slice_of_cpp_array),
928+
rust::Slice<const int>>);
929+
}
930+
{
931+
// std::vector<T> -> Slice<T>
932+
std::vector<int> cpp_vec{1, 2, 3};
933+
auto auto_slice_of_cpp_vec = rust::Slice(cpp_vec);
934+
static_assert(
935+
std::is_same_v<decltype(auto_slice_of_cpp_vec), rust::Slice<int>>);
936+
}
937+
{
938+
// const std::vector<T> -> Slice<const T>
939+
const std::vector<int> cpp_vec{1, 2, 3};
940+
auto auto_slice_of_cpp_vec = rust::Slice(cpp_vec);
941+
static_assert(std::is_same_v<decltype(auto_slice_of_cpp_vec),
942+
rust::Slice<const int>>);
943+
}
944+
#ifdef __cpp_lib_span
945+
{
946+
// std::array<T> -> Slice<T>
947+
std::array<int, 3> cpp_array{1, 2, 3};
948+
std::span<int> cpp_span(cpp_array);
949+
auto auto_slice_of_cpp_array = rust::Slice(cpp_span);
950+
static_assert(
951+
std::is_same_v<decltype(auto_slice_of_cpp_array), rust::Slice<int>>);
952+
}
953+
{
954+
// const std::array<T> -> Slice<const T>
955+
const std::array<int, 3> cpp_array{1, 2, 3};
956+
std::span<const int> cpp_span(cpp_array);
957+
auto auto_slice_of_cpp_array = rust::Slice(cpp_span);
958+
static_assert(std::is_same_v<decltype(auto_slice_of_cpp_array),
959+
rust::Slice<const int>>);
960+
}
961+
#endif // __cpp_lib_span
962+
#endif // __cpp_deduction_guides
899963

900964
rust::Vec<int> vec1{1, 2};
901965
rust::Vec<int> vec2{3, 4};

0 commit comments

Comments
 (0)