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

[IMPL] Add implicit conversion from RECT -> System.Drawing.Rectangle. #388

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions sources/Interop/Windows/Windows/shared/windef/RECT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace TerraFX.Interop.Windows;

using System.Drawing;

/// <include file='RECT.xml' path='doc/member[@name="RECT"]/*' />
public partial struct RECT
{
Expand All @@ -23,4 +25,19 @@ public partial struct RECT
/// <include file='RECT.xml' path='doc/member[@name="RECT.bottom"]/*' />
[NativeTypeName("LONG")]
public int bottom;

// type 'Rectangle' is from System.Drawing.Primitives which is from the base shared framework.
// as such these operators *should* be safe to include here.
public static implicit operator Rectangle(RECT rectangle)
=> Rectangle.FromLTRB(rectangle.left, rectangle.top, rectangle.right, rectangle.bottom);
Comment on lines +31 to +32
Copy link
Member

Choose a reason for hiding this comment

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

This isn't safe to be implicit, as it potentially overflows and thus is lossy.


public static explicit operator RECT(Rectangle rectangle)
=> new
{
Comment on lines +35 to +36
Copy link
Member

@tannergooding tannergooding Mar 3, 2024

Choose a reason for hiding this comment

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

nit: target typed new is one of my least favorite language features and I'm pretty sure it's blocked in the editorconfig, so I expect this will cause CI to fail.

// Obtained from inspecting 'Rectangle.FromLTRB(int, int, int, int)'.
left = rectangle.X,
top = rectangle.Y,
right = rectangle.Width + rectangle.X,
bottom = rectangle.Height + rectangle.Y,
};
}