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

Fix casting of list of textruns #333

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stefannikolei
Copy link
Contributor

@stefannikolei stefannikolei commented Jun 1, 2024

Directly casting is not possible. That's why using the Cast method is the easiest way

Fixes #332

Copy link

codecov bot commented Jun 1, 2024

Codecov Report

Attention: Patch coverage is 0% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 80%. Comparing base (ac272a2) to head (ce0f50c).
Report is 13 commits behind head on main.

Files Patch % Lines
...c/ImageSharp.Drawing/Processing/RichTextOptions.cs 0% 1 Missing ���️
Additional details and impacted files
@@         Coverage Diff         @@
##           main   #333   +/-   ##
===================================
  Coverage    80%    80%           
===================================
  Files        97     97           
  Lines      4960   4971   +11     
  Branches    886    888    +2     
===================================
+ Hits       4011   4023   +12     
+ Misses      754    753    -1     
  Partials    195    195           
Flag Coverage Δ
unittests 80% <0%> (+<1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@JimBobSquarePants
Copy link
Member

Would direct casting not work if we explicitly set an empty array as the default value?

@@ -33,7 +33,7 @@ public RichTextOptions(RichTextOptions options)
/// </summary>
public new IReadOnlyList<RichTextRun> TextRuns
{
get => (IReadOnlyList<RichTextRun>)base.TextRuns;
get => base.TextRuns.Cast<RichTextRun>().ToList();
Copy link
Member

@antonfirsov antonfirsov Jun 3, 2024

Choose a reason for hiding this comment

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

This creates a copy and in my understanding Cast should also fail since the default instance is has the property assigned to TextRun[] not RichTextRun[].

Would direct casting not work if we explicitly set an empty array as the default value?

That would be a better solution IMO, but it would allow bringing the object into an invalid state by downcasting RichTextOptions to TextOptions. It is an unusual but valid code.

RichTextOptions a = new(font);

Foo(a);
void Foo(TextOptions b) =>  b.TextRuns = new TextRun[0];

IReadOnlyList<RichTextRun> runs = a.TextRuns; // exception
Copy link
Member

Choose a reason for hiding this comment

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

I think we'd probably fix that with some sort of generic pattern in the future but for now our options are limited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants