Add rust::Str(const char *, size_t) constructor

This commit is contained in:
David Tolnay 2020-07-29 18:20:00 -07:00
parent c2bbd952d2
commit 894c5e45dc
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 5 additions and 6 deletions

View File

@ -59,6 +59,7 @@ public:
Str(const std::string &);
Str(const char *);
Str(const char *, size_t);
Str(std::string &&) = delete;
Str &operator=(Str) noexcept;

View File

@ -106,13 +106,11 @@ Str::Str() noexcept : repr(Repr{reinterpret_cast<const char *>(this), 0}) {}
Str::Str(const Str &) noexcept = default;
Str::Str(const std::string &s) : repr(Repr{s.data(), s.length()}) {
if (!cxxbridge03$str$valid(this->repr.ptr, this->repr.len)) {
panic<std::invalid_argument>("data for rust::Str is not utf-8");
}
}
Str::Str(const std::string &s) : Str(s.data(), s.length()) {}
Str::Str(const char *s) : repr(Repr{s, std::strlen(s)}) {
Str::Str(const char *s) : Str(s, std::strlen(s)) {}
Str::Str(const char *s, size_t len) : repr(Repr{s, len}) {
if (!cxxbridge03$str$valid(this->repr.ptr, this->repr.len)) {
panic<std::invalid_argument>("data for rust::Str is not utf-8");
}