Make qstring_t and qbytearray_t into POD types
AbandonedPublic

Authored by vandenoever on Mar 11 2018, 1:31 PM.

Details

Summary

This patch makes qstring_t and qbytearray_t into POD types so that the objects can be passed to Rust via FFI.

The object are passes as a pointer.

Test Plan

ninja tests

Diff Detail

Repository
R881 Rust Qt Binding Generator
Lint
Lint Skipped
Unit
Unit Tests Skipped
vandenoever requested review of this revision.Mar 11 2018, 1:31 PM
vandenoever created this revision.
vandenoever edited the summary of this revision. (Show Details)
vandenoever edited the test plan for this revision. (Show Details)

This patch now takes the opposite approach of what @illis proposes in D11078. The function on the rust side is sent as a pointer.

In addition the structs are now POD types.

This patch passes all test.

anthonyfieroni added inline comments.Mar 11 2018, 2:51 PM
src/cpp.cpp
921–925

Use this struct for both, named accordingly.

930

use v.data()

936
*v = QString{ reinterpret_cast<const QChar*>(val->data), val->size };

Someone can use QString like std::string, fromUtf8 will result in data change.

942–947

This will not be used.

957–958
*v = QByteArray{ reinterpret_cast<const char*>(val->data), val->size };
anthonyfieroni added inline comments.Mar 11 2018, 3:08 PM
src/cpp.cpp
902

So for convenience it can looks like:

inline T to_rust(T&& t) { return std::forward(t); }
vandenoever marked an inline comment as done.Mar 11 2018, 7:27 PM
vandenoever added inline comments.
src/cpp.cpp
902

The basic template is used for primitive types. Does std::forward(t) give an advantage there too?

The overloaded versions for qstring_t and qbytearray_t do real work.

921–925

One uses const void* and the other uses const char*. qstring_t uses const void* because it contains utf16 when sending from c++ to rust and char (utf8) when sending from rust to c++.

936

The string that comes from rust is encoded as utf-8. That is why QString::fromUtf8 is used.

957–958

By using v->resize(0) and v->append(val->data, val->len);, potentially an allocation is avoided.

anthonyfieroni added inline comments.Mar 11 2018, 7:38 PM
src/cpp.cpp
902

But in other type you will result in copies, perfect forwarding solves that.

921–925

So utf8 to utf16, it will not result in data change?

vandenoever marked 8 inline comments as done.Mar 11 2018, 7:56 PM
vandenoever added inline comments.
src/cpp.cpp
902

I read that for primitive types it's not good to use it: https://stackoverflow.com/a/28636032

921–925

In Rust, utf8 is used and QString is utf16, so the conversion has to be made. QString can deal with Unicode code points above 65535 and so can utf8, so there should be no data loss.

vandenoever abandoned this revision.Aug 17 2018, 8:16 AM
vandenoever marked an inline comment as done.

Similar code has been committed a while ago.