Skip to content

Implement From<T> for types that implements From<&T> and Into<T>, and vice versa#1384

Open
jnbooth wants to merge 1 commit intoKDAB:mainfrom
jnbooth:consistent-borrowing
Open

Implement From<T> for types that implements From<&T> and Into<T>, and vice versa#1384
jnbooth wants to merge 1 commit intoKDAB:mainfrom
jnbooth:consistent-borrowing

Conversation

@jnbooth
Copy link
Contributor

@jnbooth jnbooth commented Dec 24, 2025

There's an odd pattern across many files that looks like this:

impl From<&QLine> for QLineF {
    fn from(line: &QLine) -> Self {
        ffi::qlinef_from_qline(line)
    }
}

impl From<QLineF> for QLine {
    fn from(value: QLineF) -> Self {
        value.to_line()
    }
}

One of the implementations uses a borrow, and the other one does not, even though both are calling functions that take references. Theoretically, in order to be consistent with Qt, both implementations should use borrowing (From<&QLine> and From<&QLineF>). But I don't think it would make sense to remove the by-value implementations, because A) that would be a breaking change, and B) it is useful to be able to do e.g. let pointf = QPoint::new(1, 2).into() rather than let pointf = (&QPoint::new(1, 2)).into(). So instead, I just added complementary implementations so now From<QLine> for QLineF and From<&QLineF> for QLine are implemented as well.

That said, I'm not sure why QPen implements From<&PenStyle>. I added an implementation for From<PenStyle>, so that part is handled, but at some point it might be worth removing the From<&PenStyle> implementation, because PenStyle is just an i32 enum. There's no point in passing it by reference rather than value.

@codecov
Copy link

codecov bot commented Dec 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (ddb1db4) to head (8a110a1).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1384   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           75        75           
  Lines        13457     13457           
=========================================
  Hits         13457     13457           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@BenFordTytherington BenFordTytherington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks reasonable, not sure why we had these inconsistent, but it makes sense to me to have a standard conversion between types with either a reference or otherwise :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants