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

Explore how to represent container types #347

Closed
6 tasks done
ahayzen-kdab opened this issue Nov 1, 2022 · 8 comments
Closed
6 tasks done

Explore how to represent container types #347

ahayzen-kdab opened this issue Nov 1, 2022 · 8 comments
Assignees
Labels
⬆️ feature New feature or request
Milestone

Comments

@ahayzen-kdab
Copy link
Collaborator

ahayzen-kdab commented Nov 1, 2022

QtCore

Problems

Container types are hard to represent via extern FFI or CXX blocks due to the following problems

CXX

For representing std::vector<T>, CXX has a CxxVector<T> this has PhantomData inside it https://github.dev/dtolnay/cxx/blob/c7060d40361c4d955fc64adf35f67b28b5c75299/src/cxx_vector.rs#L24

Then a trait VectorElement https://github.dev/dtolnay/cxx/blob/c7060d40361c4d955fc64adf35f67b28b5c75299/src/cxx_vector.rs#L333 is implemented for all supported types T. In methods such as len then T::__vector_size(self) is used. https://github.dev/dtolnay/cxx/blob/c7060d40361c4d955fc64adf35f67b28b5c75299/src/cxx_vector.rs#L45

A macro is used to generate the trait implementation for each type which then, eg for __vector_size, uses an inline extern "C" block to create a method such as cxxbridge1$std$vector$i32$size which then has the type __vector_size(&CxxVector<i32>) -> usize; https://github.dev/dtolnay/cxx/blob/c7060d40361c4d955fc64adf35f67b28b5c75299/src/cxx_vector.rs#L401

There is then also C++ generation in the write phase which creates the matching C++ method with the name and type then using return self.size(); https://github.dev/dtolnay/cxx/blob/c7060d40361c4d955fc64adf35f67b28b5c75299/gen/src/write.rs#L1886

Questions

This allows you to represent something with a single type in Rust, but has the following problems

  • Does not work with a key-value store as how would the T::__map_size(self) work, as it needs to have both K and V ?
  • CXX's VectorElement is undocumented, so it's not documented that developers could implement this on their own types, we should have a stable trait for this
  • Developers will also need to include C++ code for their VectorElement impl to link to (unless we can generate this somehow?)
  • Is there any other route that could be explored with any other types like pointers or boxes etc? 🤔

CXX-Qt

For QSet, QVector, Qt5List` we can likely do something similar to CXX, but with a documented element trait.

For QHash, QMap we need to figure out a further solution.

@ahayzen-kdab ahayzen-kdab added the ⬆️ feature New feature or request label Nov 1, 2022
@ahayzen-kdab ahayzen-kdab added this to the 0.5 milestone Nov 1, 2022
@ahayzen-kdab
Copy link
Collaborator Author

For maps do we group the key and value together to created a combined type/struct ? Then have to implement the trait for every combination you want. This would be similar to QHash internally that has typedef QHashNode<Key, T> Node;

Then a developer could define i32Tof64Pair and then implement ::value(i32) -> f64 or ::size() etc ?

@ahayzen-kdab
Copy link
Collaborator Author

Can we copy how impl UniquePtr<QColor> {} then does the implementation for QColor with UniquePtr but for vector and hashmaps.

Eg

impl QVectorElement<QPoint> {} to add the code generation for QPoint as an element for a vector.

or

impl QHashPair<i32, QByteArray> {} to add the code generation for a QHash<i32, QByteArray>

The remaining question if we went with that route would be, where does the generated C++ end up? In the cpp file for the bridge module?

@ahayzen-kdab ahayzen-kdab self-assigned this Nov 2, 2022
@ahayzen-kdab
Copy link
Collaborator Author

We should also consider if can upstream impl VectorElement<T> {} upstream (assuming you can't do that already) and then look at adding HashMap, HashSet, CxxMap etc upstream.

@ahayzen-kdab
Copy link
Collaborator Author

Maybe the API would actually be impl QVector<QPoint> {} or impl QHash<i32, QByteArray> {} and then for upstream impl CxxVector<T> {}.

@ahayzen-kdab
Copy link
Collaborator Author

ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 11, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 11, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 11, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 11, 2022
@ahayzen-kdab
Copy link
Collaborator Author

For the initial code we will requires developers to implement the trait themselves on the custom type for container types. Later we can look again at an impl generator #355

ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 11, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 11, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 11, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 16, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 16, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 16, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 16, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 18, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 21, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 23, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 23, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Nov 24, 2022
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
@ahayzen-kdab
Copy link
Collaborator Author

For Qt5 vs Qt6 fun for QVector and QList, do we actually just implement both QVector and QList as types their have their internal size etc be different depending on Qt version, as the API that we'll expose to Rust looks similar even with the changes?

ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 14, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 14, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 14, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 14, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 14, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 19, 2022
Be-ing pushed a commit that referenced this issue Dec 19, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 20, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Dec 20, 2022
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Jan 9, 2023
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Jan 9, 2023
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Jan 9, 2023
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Jan 9, 2023
ahayzen-kdab added a commit to ahayzen-kdab/cxx-qt that referenced this issue Jan 9, 2023
Be-ing pushed a commit that referenced this issue Jan 10, 2023
@ahayzen-kdab
Copy link
Collaborator Author

All container types are done :-D

przempore pushed a commit to przempore/cxx-qt that referenced this issue Mar 15, 2023
Also define all primitive types and cxx_qt_lib types as T.

Related to KDAB#347
przempore pushed a commit to przempore/cxx-qt that referenced this issue Mar 15, 2023
przempore pushed a commit to przempore/cxx-qt that referenced this issue Mar 15, 2023
przempore pushed a commit to przempore/cxx-qt that referenced this issue Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⬆️ feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant