Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions book/src/binding/str.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public:
Str &operator=(const Str &) & noexcept;

explicit operator std::string() const;
#if __cplusplus >= 201703L
explicit operator std::string_view() const;
#endif

// Note: no null terminator.
const char *data() const noexcept;
Expand Down
14 changes: 11 additions & 3 deletions include/cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#include <iosfwd>
#include <iterator>
#include <new>
#if __cplusplus >= 202002L
#include <ranges>
#endif
#include <stdexcept>
#include <string>
#include <type_traits>
Expand All @@ -23,6 +20,14 @@
#include <sys/types.h>
#endif

#if __cplusplus >= 201703L
#include <string_view>
#endif

#if __cplusplus >= 202002L
#include <ranges>
#endif

namespace rust {
inline namespace cxxbridge1 {

Expand Down Expand Up @@ -123,6 +128,9 @@ class Str final {
Str &operator=(const Str &) & noexcept = default;

explicit operator std::string() const;
#if __cplusplus >= 201703L
explicit operator std::string_view() const;
#endif

// Note: no null terminator.
const char *data() const noexcept;
Expand Down
6 changes: 6 additions & 0 deletions src/cxx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,12 @@ Str::operator std::string() const {
return std::string(this->data(), this->size());
}

#if __cplusplus >= 201703L
Str::operator std::string_view() const {
return std::string_view(this->data(), this->size());
}
#endif

const char *Str::data() const noexcept { return cxxbridge1$str$ptr(this); }

std::size_t Str::size() const noexcept { return cxxbridge1$str$len(this); }
Expand Down
5 changes: 5 additions & 0 deletions tests/ffi/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,11 @@ extern "C" const char *cxx_run_test() noexcept {
rust::Str out_param;
r_return_str_via_out_param(Shared{2020}, out_param);
ASSERT(out_param == "2020");

#if __cplusplus >= 201703L
std::string_view out_param_as_string_view{out_param};
ASSERT(out_param_as_string_view == "2020");
#endif
}

rust::Str cstr = "test";
Expand Down
Loading