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 reserve on CxxString #925

Merged
merged 1 commit into from
Aug 27, 2021
Merged

Expose reserve on CxxString #925

merged 1 commit into from
Aug 27, 2021

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Aug 27, 2021

This has come up as necessary in APIs that connect bytes-based code in Rust to std::string-based code in C++.

use bytes::Buf;
use cxx::let_cxx_string;

fn thing(buf: &mut dyn Buf) {
    let_cxx_string!(string = "");

    // handled by helper:
    string.as_mut().reserve(buf.remaining());
    while buf.remaining() > 0 {
        let chunk = buf.chunk();
        string.as_mut().push_bytes(chunk);
        buf.advance(chunk.len());
    }

    ffi::thing(string);
}

// ffi::thing:
// void thing(std::string& s) {
//   WriteMessage msg{std::move(s)};
//   ...
// }

@dtolnay dtolnay merged commit 4e0ac98 into master Aug 27, 2021
@dtolnay dtolnay deleted the cxxstringreserve branch August 27, 2021 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant