-
Notifications
You must be signed in to change notification settings - Fork 397
Description
Hi,
I would like to construct a buffer of bytes in Rust-owned memory from C++. For instance, I might want to use rust::Vec<u8> as the owned key type to be stored in a Rust hash map or other data structure, and I might want to build that key incrementally from C++ by concatenating the parts of the key together.
It doesn't look like there is a way to append a bunch of T onto the end of a rust::Vec<T> other than repeatedly calling emplace_back().
If I use the workaround to expose set_len() from #990 I can do something like:
std::array<uint8_t, 100> arr;
std::iota(arr.begin(), arr.end(), 1);
auto buf = rust::Vec<uint8_t>{101, 102, 103, 104};
const auto new_size = buf.size() + arr.size();
buf.reserve(new_size);
std::ranges::copy(arr, buf.end());
vec_u8_set_len(buf, new_size);But naively calling emplace_bacK() like this results in a ~10X slowdown from that:
std::array<uint8_t, 100> arr;
std::iota(arr.begin(), arr.end(), 1);
auto buf = rust::Vec<uint8_t>{101, 102, 103, 104};
for (auto a : arr) {
buf.emplace_back(a);
}While the set_len() workaround from #990, or having a public set_len() method would enable a memcpy() or std::ranges::copy based workaround, it would be nice to have a more ergonomic way to append to the end of a rust::Vec<T> as part of the API.
If this were a normal C++ vector I'd expect to be able to do something like insert()'ing at the end, or perhaps C++23's append_range(), or if this were a normal Rust Vec I'd expect to be able to use something like extend() or extend_from_slice().
Thanks!