Organize string constructors
diff --git a/include/cxxbridge.h b/include/cxxbridge.h
index a0c0b07..984eca2 100644
--- a/include/cxxbridge.h
+++ b/include/cxxbridge.h
@@ -10,13 +10,16 @@
class String final {
public:
String() noexcept;
- String(const String &other) noexcept;
- String(String &&other) noexcept;
- String(const char *s);
- String(const std::string &s);
- String &operator=(const String &other) noexcept;
- String &operator=(String &&other) noexcept;
+ String(const String &) noexcept;
+ String(String &&) noexcept;
~String() noexcept;
+
+ String(const std::string &);
+ String(const char *);
+
+ String &operator=(const String &) noexcept;
+ String &operator=(String &&) noexcept;
+
explicit operator std::string() const;
// Note: no null terminator.
@@ -32,11 +35,14 @@
class Str final {
public:
Str() noexcept;
- Str(const char *s);
+ Str(const Str &) noexcept;
+
Str(const std::string &s);
+ Str(const char *s);
Str(std::string &&s) = delete;
- Str(const Str &other) noexcept;
- Str &operator=(Str other) noexcept;
+
+ Str &operator=(Str) noexcept;
+
explicit operator std::string() const;
// Note: no null terminator.
@@ -52,7 +58,7 @@
const char *ptr;
size_t len;
};
- Str(Repr repr) noexcept;
+ Str(Repr) noexcept;
explicit operator Repr() noexcept;
private:
diff --git a/src/cxxbridge.cc b/src/cxxbridge.cc
index 7991bfe..1f7c8dd 100644
--- a/src/cxxbridge.cc
+++ b/src/cxxbridge.cc
@@ -41,12 +41,7 @@
cxxbridge01$rust_string$new(&other);
}
-String::String(const char *s) {
- auto len = strlen(s);
- if (!cxxbridge01$rust_string$from(this, s, len)) {
- throw std::invalid_argument("data for rust::String is not utf-8");
- }
-}
+String::~String() noexcept { cxxbridge01$rust_string$drop(this); }
String::String(const std::string &s) {
auto ptr = s.data();
@@ -56,10 +51,11 @@
}
}
-String::~String() noexcept { cxxbridge01$rust_string$drop(this); }
-
-String::operator std::string() const {
- return std::string(this->data(), this->size());
+String::String(const char *s) {
+ auto len = strlen(s);
+ if (!cxxbridge01$rust_string$from(this, s, len)) {
+ throw std::invalid_argument("data for rust::String is not utf-8");
+ }
}
String &String::operator=(const String &other) noexcept {
@@ -79,6 +75,10 @@
return *this;
}
+String::operator std::string() const {
+ return std::string(this->data(), this->size());
+}
+
const char *String::data() const noexcept {
return cxxbridge01$rust_string$ptr(this);
}
@@ -98,11 +98,7 @@
Str::Str() noexcept : repr(Repr{reinterpret_cast<const char *>(this), 0}) {}
-Str::Str(const char *s) : repr(Repr{s, strlen(s)}) {
- if (!cxxbridge01$rust_str$valid(this->repr.ptr, this->repr.len)) {
- throw std::invalid_argument("data for rust::Str is not utf-8");
- }
-}
+Str::Str(const Str &) noexcept = default;
Str::Str(const std::string &s) : repr(Repr{s.data(), s.length()}) {
if (!cxxbridge01$rust_str$valid(this->repr.ptr, this->repr.len)) {
@@ -110,7 +106,11 @@
}
}
-Str::Str(const Str &) noexcept = default;
+Str::Str(const char *s) : repr(Repr{s, strlen(s)}) {
+ if (!cxxbridge01$rust_str$valid(this->repr.ptr, this->repr.len)) {
+ throw std::invalid_argument("data for rust::Str is not utf-8");
+ }
+}
Str &Str::operator=(Str other) noexcept {
this->repr = other.repr;