Skip to content

Creating UniquePtr<CxxString> #1667

@tomekb234

Description

@tomekb234

If I understand correctly, there is currently no way to create UniquePtr<CxxString> on the Rust side without writing own utility function:

unsafe extern "C++" {
    fn make_unique_cxx_string(slice: &[u8]) -> UniquePtr<CxxString>;
}
std::unique_ptr<std::string> make_unique_cxx_string(rust::Slice<const uint8_t> slice) {
    return std::make_unique<std::string>(reinterpret_cast<const char*>(slice.data()), slice.size());
}

This is unlike Pin<&mut CxxString> (and consequently &CxxString), which can be made with let_cxx_string!() provided by this library.

Wouldn't it make sense to have a "standard" implementation of make_unique_cxx_string() provided by cxx? It could also be in some more convenient form, for example:

fn make_unique_cxx_string(data: impl AsRef<[u8]>) -> UniquePtr<CxxString>

This problem was also mentioned in #1339.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions