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

[API Proposal]: Implement ISpanParsable/Formattable for PhysicalAddress #83201

Open
PaulusParssinen opened this issue Mar 9, 2023 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Net
Milestone

Comments

@PaulusParssinen
Copy link
Contributor

PaulusParssinen commented Mar 9, 2023

Background and motivation

PhysicalAddress already provides [Try]Parse overloads and a ToString for IFormattable. I propose giving some love to this type and implementing ISpanFormattable and ISpanParsable<PhysicalAddress>.

These could now light up in various contexts, for example, ISpanFormattable would allow PhysicalAddress light up in the string interpolated handlers.

While not being as important as IPAddress and being hidden in namespace below, I'd argue PhysicalAddress is important enough networking primitive type to warrant these interfaces.

API Proposal

namespace System.Net.NetworkInformation;

public class PhysicalAddress
+    : ISpanFormattable, ISpanParsable<PhysicalAddress>
{
    // I(Span)Parsable overloads
    // can delegate to existing public (Try)Parse(..) overloads.
+    static bool IParsable<PhysicalAddress>.TryParse(string? s, IFormatProvider? provider, out PhysicalAddress? result);
+    static bool ISpanParsable<PhysicalAddress>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out PhysicalAddress? result);

+    static PhysicalAddress IParsable<PhysicalAddress>.Parse(string s, IFormatProvider? provider);
+    static PhysicalAddress ISpanParsable<PhysicalAddress>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider);

    // IFormattable, can delegate to existing public ToString().
+    string IFormattable.ToString(string? format, IFormatProvider? formatProvider);

    // ISpanFormattable
    // Has no existing overload to delegate to, should it be publicly provided like IPAddress and IPNetwork?
+    bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider);
    // e.g. Should we provide a following?
+    public bool TryFormat(Span<char> destination, out int charsWritten);
}

API Usage

PhysicalAddress physicalAddress = ...;
string s = $"The address is {physicalAddress}";

Alternative Designs

Note

Could also implement IUtf8SpanFormattable and IUtf8SpanParsable but should that be separate proposal?

Could also just implement IFormattable and IParsable, but please, don't.

Risks

No response

@PaulusParssinen PaulusParssinen added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Mar 9, 2023
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 9, 2023
@ghost
Copy link

ghost commented Mar 9, 2023

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

PhysicalAddress already provides [Try]Parse overloads and a ToString for IFormattable. I propose giving some love to this type and implementing ISpanFormattable and ISpanParsable<PhysicalAddress>.

These could now light up in various contexts, for example, ISpanFormattable would allow PhysicalAddress it to light up in the string interpolated handlers.

While not being as important as IPAddress and being "hidden" in namespace below, I'd argue PhysicalAddress is important enough networking "primitive" type to warrant these interfaces.

API Proposal

namespace System.Net.NetworkInformation;

public class PhysicalAddress
+    : ISpanFormattable, ISpanParsable<PhysicalAddress>
{
    // I(Span)Parsable overloads
    // can delegate to existing (Try)Parse overloads. IFormatProvider provider would be explicitly ignored I presume.
+    public static bool IParsable<PhysicalAddress>.TryParse(string? s, IFormatProvider? provider, out PhysicalAddress? result);
+    public static bool ISpanParsable<PhysicalAddress>.TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out PhysicalAddress? result);

+    public static PhysicalAddress IParsable<PhysicalAddress>.Parse(string s, IFormatProvider? provider);
+    public static PhysicalAddress ISpanParsable<PhysicalAddress>.Parse(ReadOnlySpan<char> s, IFormatProvider? provider);

    // IFormattable, can delegate to existing ToString(). format and provider would be explicitly ignored I presume.
+    public string IFormattable.ToString(string? format, IFormatProvider? formatProvider);

    // ISpanFormattable
    // Has no existing overloads to delegate to, should they be publicly provided like IPAddress?
+    public bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider);
    // e.g. Should we provide a following simpler overload like the convention used in IPAddress?
+    public bool TryFormat(Span<char> destination, out int charsWritten);
}

API Usage

PhysicalAddress physicalAddress = ...;
string s = $"The address is {physicalAddress}";

Alternative Designs

Could also just implement IFormattable and IParsable.

Risks

No response

Author: PaulusParssinen
Assignees: -
Labels:

api-suggestion, area-System.Net, untriaged

Milestone: -

@CarnaViire
Copy link
Member

Triage: putting to Future for now as there doesn't seem to be much customer ask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Net
Projects
None yet
Development

No branches or pull requests

3 participants