Skip to content

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

Merged
ahayzen-kdab merged 1 commit intoKDAB:mainfrom
jnbooth:consistent-borrowing
Mar 27, 2026
Merged

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

Conversation

@jnbooth
Copy link
Copy Markdown
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
Copy Markdown

codecov bot commented Dec 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (1b91ce6) to head (9be8f57).
⚠️ Report is 2 commits behind head on main.

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

☔ 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
Copy Markdown
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 :)

@ahayzen-kdab ahayzen-kdab force-pushed the consistent-borrowing branch from 8a110a1 to 9be8f57 Compare March 27, 2026 17:06
Copy link
Copy Markdown
Collaborator

@ahayzen-kdab ahayzen-kdab left a comment

Choose a reason for hiding this comment

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

LGTM :-)

@ahayzen-kdab ahayzen-kdab enabled auto-merge March 27, 2026 17:06
@ahayzen-kdab ahayzen-kdab added this pull request to the merge queue Mar 27, 2026
Merged via the queue into KDAB:main with commit 2e98d2d Mar 27, 2026
17 checks passed
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.

3 participants