Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose capacity and reserve on rust::String #924

Merged
merged 1 commit into from
Aug 27, 2021
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/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public:

const char *c_str() noexcept;

size_t capacity() const noexcept;
void reserve(size_t new_cap) noexcept;

using iterator = char *;
iterator begin() noexcept;
iterator end() noexcept;
Expand Down
3 changes: 3 additions & 0 deletions include/cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class String final {

const char *c_str() noexcept;

std::size_t capacity() const noexcept;
void reserve(size_t new_cap) noexcept;

using iterator = char *;
iterator begin() noexcept;
iterator end() noexcept;
Expand Down
11 changes: 11 additions & 0 deletions src/cxx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ bool cxxbridge1$string$from_utf16(rust::String *self, const char16_t *ptr,
void cxxbridge1$string$drop(rust::String *self) noexcept;
const char *cxxbridge1$string$ptr(const rust::String *self) noexcept;
std::size_t cxxbridge1$string$len(const rust::String *self) noexcept;
std::size_t cxxbridge1$string$capacity(const rust::String *self) noexcept;
void cxxbridge1$string$reserve_additional(rust::String *self,
size_t additional) noexcept;
void cxxbridge1$string$reserve_total(rust::String *self,
size_t new_cap) noexcept;

// rust::Str
void cxxbridge1$str$new(rust::Str *self) noexcept;
Expand Down Expand Up @@ -165,6 +168,14 @@ const char *String::c_str() noexcept {
return ptr;
}

std::size_t String::capacity() const noexcept {
return cxxbridge1$string$capacity(this);
}

void String::reserve(std::size_t new_cap) noexcept {
cxxbridge1$string$reserve_total(this, new_cap);
}

String::iterator String::begin() noexcept {
return const_cast<char *>(this->data());
}
Expand Down
13 changes: 13 additions & 0 deletions src/symbols/rust_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,20 @@ unsafe extern "C" fn string_len(this: &String) -> usize {
this.len()
}

#[export_name = "cxxbridge1$string$capacity"]
unsafe extern "C" fn string_capacity(this: &String) -> usize {
this.capacity()
}

#[export_name = "cxxbridge1$string$reserve_additional"]
unsafe extern "C" fn string_reserve_additional(this: &mut String, additional: usize) {
this.reserve(additional);
}

#[export_name = "cxxbridge1$string$reserve_total"]
unsafe extern "C" fn string_reserve_total(this: &mut String, new_cap: usize) {
if new_cap > this.capacity() {
let additional = new_cap - this.len();
this.reserve(additional);
}
}
6 changes: 6 additions & 0 deletions tests/ffi/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,12 @@ extern "C" const char *cxx_run_test() noexcept {
ASSERT(cstring == "foo");
ASSERT(other_cstring == "test");

ASSERT(cstring.capacity() == 3);
cstring.reserve(2);
ASSERT(cstring.capacity() == 3);
cstring.reserve(5);
ASSERT(cstring.capacity() >= 5);

rust::Str cstr = "test";
rust::Str other_cstr = "foo";
swap(cstr, other_cstr);
Expand Down