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.
illis | |
anthonyfieroni |
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.
ninja tests
Lint Skipped |
Unit Tests Skipped |
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 }; |
src/cpp.cpp | ||
---|---|---|
902 | So for convenience it can looks like: inline T to_rust(T&& t) { return std::forward(t); } |
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. |
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. |