cxx-qt icon indicating copy to clipboard operation
cxx-qt copied to clipboard

Ensure that all Qt types have a correct default and similar methods

Open ahayzen-kdab opened this issue 3 years ago • 4 comments

Ensure that our wrapped Qt types have defaults that are the same as Qt and relevant methods (eg getters and setters).

  • [ ] Default (potentially using the constructor on the C++ side)
  • [ ] Display + Debug (see #131 )
  • [ ] Relevant getters and setters

ahayzen-kdab avatar Jan 07 '22 16:01 ahayzen-kdab

We should also check for which types we can make the members public. QPoint comes to mind. I think in the data-driven design of Rust this might be more idiomatic.

LeonMatthesKDAB avatar Jan 12 '22 14:01 LeonMatthesKDAB

For QUrl we have from_str which needs to be Result<T, E> to match the std trait. At the moment we use Infallible but this could use QUrl::isValid() to return an error if the url is not valid!

ahayzen-kdab avatar Feb 11 '22 14:02 ahayzen-kdab

Consider if we want by-ref or by-value in various parts of our API.

  • Invokable parameters are by ref
  • Invokable returns are by value
  • Property setters are by ref for non-opaque and by value for opaque
  • Property getters are by ref for non-opaque and by value for opaque
  • Most of our methods on types take by ref

Related to #58 as well

ahayzen-kdab avatar Feb 11 '22 15:02 ahayzen-kdab

Also ensure that we have Display and Debug for types that make sense.

ahayzen-kdab avatar May 09 '22 12:05 ahayzen-kdab

Related to #55

ahayzen-kdab avatar Nov 01 '22 13:11 ahayzen-kdab

For Ord and Eq we should be able to make a single C++ template like bool operatorEq(const T& a, const T& b) and reuse the single C++ template for all types like we have with construct and drop etc. (Assuming we can't bind with CXX to the operator of the type)

ahayzen avatar Dec 22 '22 21:12 ahayzen

@vimpostor did you want to continue this issue?

Things that are likely left

  • Check relevant types have Default (think most do?)
  • Add PartialOrd/Ord to relevant types (again template likely will work well)
  • We added impl AsRef<[u8]> to QByteArray, to assist with using it in general Rust APIs would this, or similar, be useful on any other types?
  • Consider any other std traits that could be good ?

Member functions is split out into #55 and operators (/ * - +) are in that issue as well, could be a good one after this one.

ahayzen-kdab avatar Jan 13 '23 16:01 ahayzen-kdab

Yep, I will likely add PartialOrd next ;)

vimpostor avatar Jan 13 '23 18:01 vimpostor

@vimpostor I've been adding missing member functions in #55 to the types. But I haven't yet done the operators + - / * (note that Rhs will need to be set in some cases for std::ops::Add etc so that it isn't Self), did you want to do these and move them into this issue? As they are likely similar with templates. Or shall i do these as part of #55 ?

ahayzen-kdab avatar Jan 24 '23 04:01 ahayzen-kdab

And would be good to add Eq i think to the container types, maybe Ord can be skipped for now as discussed.

ahayzen-kdab avatar Jan 24 '23 04:01 ahayzen-kdab

@ahayzen-kdab Yes, I don't mind adding operator +-/* functions. And I agree, PartEq will make sense for containers, I will tackle that next.

vimpostor avatar Jan 26 '23 15:01 vimpostor

@vimpostor OK! moved the operators onto this issue. Also i noticed in #423 you only did the list types, what about the maps like QHash and QMap ?

ahayzen-kdab avatar Jan 31 '23 01:01 ahayzen-kdab

  • [ ] AsRef

Could you explain what was meant with the last item in that list? Is it solved already?

vimpostor avatar Mar 01 '23 13:03 vimpostor

  • [ ] AsRef

Could you explain what was meant with the last item in that list? Is it solved already?

If you look at QByteArray we added this following AsRef implementation as in Rust std APIs AsRef<[u8]> is a common usecase, so it was about checking if any of the other types could benefit from an AsRef as well ? (eg maybe the list/vector but they might be tricky, idk if QString / QUrl or something as well, if there aren't any obvious ones then just mark it as done...)

impl AsRef<[u8]> for QByteArray {
    /// Construct a slice of u8 from a QByteArray
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

ahayzen-kdab avatar Mar 01 '23 13:03 ahayzen-kdab

It's possible this is the only useful one, as note things like QString are UTF16 not UTF8 so would need conversions. Maybe only a QVector<T> -> AsRef<[T]> would be useful, but we don't have any slice mechanism in the containers yet and that might be tricky to do. So potentially there aren't any others that are useful and it can be marked as done ?

ahayzen-kdab avatar Mar 01 '23 13:03 ahayzen-kdab

So potentially there aren't any others that are useful and it can be marked as done ?

Yes, agreed

vimpostor avatar Mar 01 '23 14:03 vimpostor

Completed by #448

ahayzen-kdab avatar Mar 01 '23 15:03 ahayzen-kdab