Repository: dtolnay/cxx (language: rust)

Issue:
Provide member function `bool empty() const noexcept` on rust::Str, rust::String
We apparently already have this on `rust::Slice` and `rust::Vec` but it isn't there on `rust::Str` and `rust::String`.

https://github.com/dtolnay/cxx/blob/1d0556e0687c41ac564938f8d8579f3aab0e524c/include/cxx.h#L168

https://github.com/dtolnay/cxx/blob/1d0556e0687c41ac564938f8d8579f3aab0e524c/include/cxx.h#L300

Should match the API of https://en.cppreference.com/w/cpp/string/basic_string/empty


Discussion / hints:

Fix patch:
diff --git a/book/src/binding/str.md b/book/src/binding/str.md
index a4820aafc..9c1e0a773 100644
--- a/book/src/binding/str.md
+++ b/book/src/binding/str.md
@@ -30,6 +30,7 @@ public:
   const char *data() const noexcept;
   size_t size() const noexcept;
   size_t length() const noexcept;
+  bool empty() const noexcept;
 
   using iterator = const char *;
   using const_iterator = const char *;
diff --git a/book/src/binding/string.md b/book/src/binding/string.md
index a7d0790bd..882fd9847 100644
--- a/book/src/binding/string.md
+++ b/book/src/binding/string.md
@@ -32,6 +32,7 @@ public:
   const char *data() const noexcept;
   size_t size() const noexcept;
   size_t length() const noexcept;
+  bool empty() const noexcept;
 
   const char *c_str() noexcept;
 
diff --git a/include/cxx.h b/include/cxx.h
index e8a206fc9..1153e437f 100644
--- a/include/cxx.h
+++ b/include/cxx.h
@@ -53,6 +53,7 @@ class String final {
   const char *data() const noexcept;
   std::size_t size() const noexcept;
   std::size_t length() const noexcept;
+  bool empty() const noexcept;
 
   const char *c_str() noexcept;
 
@@ -105,6 +106,7 @@ class Str final {
   const char *data() const noexcept;
   std::size_t size() const noexcept;
   std::size_t length() const noexcept;
+  bool empty() const noexcept;
 
   // Important in order for System V ABI to pass in registers.
   Str(const Str &) noexcept = default;
diff --git a/src/cxx.cc b/src/cxx.cc
index b9168a871..ecfc1c69b 100644
--- a/src/cxx.cc
+++ b/src/cxx.cc
@@ -131,6 +131,8 @@ std::size_t String::length() const noexcept {
   return cxxbridge1$string$len(this);
 }
 
+bool String::empty() const noexcept { return this->size() == 0; }
+
 const char *String::c_str() noexcept {
   auto len = this->length();
   cxxbridge1$string$reserve_total(this, len + 1);
@@ -228,6 +230,8 @@ std::size_t Str::size() const noexcept { return cxxbridge1$str$len(this); }
 
 std::size_t Str::length() const noexcept { return this->size(); }
 
+bool Str::empty() const noexcept { return this->size() == 0; }
+
 Str::const_iterator Str::begin() const noexcept { return this->cbegin(); }
 
 Str::const_iterator Str::end() const noexcept { return this->cend(); }


Test patch: