1414#include < basetsd.h>
1515#endif
1616
17+ #ifdef CXXBRIDGE_HAS_STRING_VIEW
18+ #error "string_view gets auto-detected"
19+ #endif
20+
21+ // clang-format off
22+ // detect if string_view supported (C++17 required)
23+ #ifdef __has_include
24+ #if __has_include(<version>)
25+ // gcc >= 9, clang >= 9, msvc >= 19.22
26+ #include < version>
27+ #if __cpp_lib_string_view >= 201603L
28+ #define CXXBRIDGE_HAS_STRING_VIEW
29+ #endif
30+ #else
31+ // no <version> include
32+ #if defined(__cpp_lib_string_view) && __cpp_lib_string_view >= 201603L
33+ // gcc: 8, clang: 5 - 8, msvc: 19.15 - 19.21
34+ #define CXXBRIDGE_HAS_STRING_VIEW
35+ #else
36+ // only include if compiled with c++17
37+ #if (__GNUC__ >= 7 && __cplusplus >= 201703L) || (_MSVC_LANG >= 201703L)
38+ // gcc: 7, msvc: 19.14
39+ #define CXXBRIDGE_HAS_STRING_VIEW
40+ #endif
41+ #endif
42+ #endif
43+ #else
44+ // no __has_include
45+ #if _MSC_VER >= 1910L && _MSVC_LANG > 201402L
46+ // msvc: 19.10 requires c++latest (!)
47+ #define CPPBRIDGE_HAS_STRING_VIEW
48+ #endif
49+ #endif
50+ // clang-format on
51+
52+ #ifdef CXXBRIDGE_HAS_STRING_VIEW
53+ #include < string_view>
54+ #endif
55+
1756namespace rust {
1857inline namespace cxxbridge1 {
1958
@@ -33,14 +72,21 @@ class String final {
3372 String (String &&) noexcept ;
3473 ~String () noexcept ;
3574
75+ #ifdef CXXBRIDGE_HAS_STRING_VIEW
76+ String (std::string_view);
77+ #else
3678 String (const std::string &);
79+ #endif
3780 String (const char *);
3881 String (const char *, size_t );
3982
4083 String &operator =(const String &) noexcept ;
4184 String &operator =(String &&) noexcept ;
4285
4386 explicit operator std::string () const ;
87+ #ifdef CXXBRIDGE_HAS_STRING_VIEW
88+ explicit operator std::string_view () const noexcept ;
89+ #endif
4490
4591 // Note: no null terminator.
4692 const char *data () const noexcept ;
@@ -60,13 +106,20 @@ class String final {
60106class Str final {
61107public:
62108 Str () noexcept ;
109+ #ifdef CXXBRIDGE_HAS_STRING_VIEW
110+ Str (std::string_view);
111+ #else
63112 Str (const std::string &);
113+ #endif
64114 Str (const char *);
65115 Str (const char *, size_t );
66116
67117 Str &operator =(const Str &) noexcept = default ;
68118
69119 explicit operator std::string () const ;
120+ #ifdef CXXBRIDGE_HAS_STRING_VIEW
121+ explicit operator std::string_view () const noexcept ;
122+ #endif
70123
71124 // Note: no null terminator.
72125 const char *data () const noexcept ;
0 commit comments