Skip to content

CXX-Qt-lib: Implement QHash<K,V> instead of QHash<QHashPair_K_V> #1138

Open
@LeonMatthesKDAB

Description

@LeonMatthesKDAB

Idea

It turns out there is an exception to the orphan rule that we can exploit to make QHash<K,V> work that we didn't think of before.

Basically, whilst you generally cannot implement a foreign trait for a foreign type, you can actually do so, if the trait is templated and one of the template arguments is a local type!

So if we make the QHashPair trait templated over the Key and the Value, we can actually make this work:

pub struct QHash<Key, Value>
where
    QHash<Key, Value>: QHashPair<Key, Value>,
{
    _space: MaybeUninit<usize>,
    _key: PhantomData<Key>,
    _value: PhantomData<Value>,
}

It would then be possible for users to:

impl cxx_qt_lib::QHashPair<cxx_qt_lib::QString, MyType>
    for cxx_qt_lib::QHash<cxx_qt_lib::QString, MyType> {
// ....
}

because MyType is a local type, this is possible, even if the target type and trait are coming from cxx_qt_lib.

Backwards compatibility

The most problematic part of this refactoring is backwards-compatibility.
Our current tutorial videos already show QHash<QHashPair_K_V> and we want to keep those tutorials working as much as possible.

We may be able to solve this issue by using default type paramters, e.g. by setting V=(), we would allow users to use a QHashPair_K_V as the Key whilst leaving the value blank.

Because all behavior in the end is delegated to the trait anyway, we can implement QHashPair<QHashPair_K_V, ()>, while also adding a deprecation notice to the QHashPair_K_V.

Note that we likely want to remove this entirely for the final 1.0 release, as this solution is likely to require some contortion on the implementation of QHash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    👷 refactorSomething needs to change🥳🎉 1.0This issue is part of stabilization for 1.0 release

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions