Organize string constructors
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;