Skip to content

[API Proposal]: Implement IUtf8SpanParsable on Guid #105653

@SirCxyrtyx

Description

@SirCxyrtyx

Background and motivation

As part of #81500 :

Implement IUtf8SpanParsable on Guid. Additionally, add a utf8 Parse and TryParse that do not take the unused IFormatProvider parameter. This matches how ISpanParsable was implemented.

Draft PR: #105654

API Proposal

namespace System;

public readonly partial struct Guid
    : ISpanFormattable,
      IComparable,
      IComparable<Guid>,
      IEquatable<Guid>,
      ISpanParsable<Guid>,
      IUtf8SpanFormattable,
+      IUtf8SpanParsable<Guid>
{
+    public static Guid Parse(ReadOnlySpan<byte> utf8Text);
+    public static bool TryParse(ReadOnlySpan<byte> utf8Text, out Guid result);

+    public static Guid Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
+    public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out Guid result);
}

API Usage

var utf8GuidText = "{a8a110d5-fc49-43c5-bf46-802db8f843ff}"u8;

if (Guid.TryParse(utf8GuidText, out Guid guid))
{
    //do something with guid
}

Alternative Designs

No response

Risks

There are a few aspects to this that I am unsure about.

  • Given that the IFormatProvider parameter is unused, I think IUtf8SpanParsable ought to be implemented explicitly. But that would not be consistent with the ISpanParsable implementation.
  • Should a utf8 version of ParseExact and TryParseExact also be added for consistency?
  • Guid has a constructor that takes a string, which is the same as calling Guid.Parse(string). It also has a constructor that takes a ReadOnlySpan<byte>, but this will not be equivalent to calling Guid.Parse(ReadOnlySpan<byte>), as that constructor deserializes from a binary representation of a Guid. Is this inconsistency a problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationneeds-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions