diff --git a/src/FastIDs.TypeId/TypeId.Core/TypeIdDecoded.cs b/src/FastIDs.TypeId/TypeId.Core/TypeIdDecoded.cs index 1cf34e3..0bf85e2 100644 --- a/src/FastIDs.TypeId/TypeId.Core/TypeIdDecoded.cs +++ b/src/FastIDs.TypeId/TypeId.Core/TypeIdDecoded.cs @@ -8,23 +8,23 @@ namespace FastIDs.TypeId; public readonly struct TypeIdDecoded : IEquatable, ISpanFormattable, IUtf8SpanFormattable { private static readonly UuidGenerator UuidGenerator = new(); - + /// /// The type part of the TypeId. /// public string Type { get; } - + /// /// The ID part of the TypeId. /// public Guid Id { get; } - + internal TypeIdDecoded(string type, Guid id) { Type = type; Id = id; } - + /// /// Returns the ID part of the TypeId as an encoded string. /// @@ -50,7 +50,7 @@ public int GetSuffix(Span output) return Base32.Encode(idBytes, output); } - + public int GetSuffix(Span utf8Output) { Span idBytes = stackalloc byte[Base32Constants.DecodedLength]; @@ -115,7 +115,7 @@ public override string ToString() /// This method ignores and parameters and outputs the same result as . /// public string ToString(string? format, IFormatProvider? formatProvider) => ToString(); - + /// /// Tries to format the value of the current instance into the provided span of characters. /// @@ -139,7 +139,7 @@ public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan var suffixSpan = destination[charsWritten..]; if (suffixSpan.Length < Base32Constants.EncodedLength) return false; - + var suffixCharsWritten = GetSuffix(suffixSpan); charsWritten += suffixCharsWritten; @@ -169,13 +169,22 @@ public bool TryFormat(Span utf8Destination, out int bytesWritten, ReadOnly var suffixSpan = utf8Destination[bytesWritten..]; if (suffixSpan.Length < Base32Constants.EncodedLength) return false; - + var suffixBytesWritten = GetSuffix(suffixSpan); bytesWritten += suffixBytesWritten; return true; } + /// + /// Implicitly converts TypeIdDecoded to Guid by extracting the ID part. + /// + /// The TypeIdDecoded instance. + public static implicit operator Guid(TypeIdDecoded typeIdDecoded) + { + return typeIdDecoded.Id; + } + public bool Equals(TypeIdDecoded other) => Type == other.Type && Id.Equals(other.Id); public override bool Equals(object? obj) => obj is TypeIdDecoded other && Equals(other); @@ -196,7 +205,7 @@ public bool TryFormat(Span utf8Destination, out int bytesWritten, ReadOnly /// This method validates the type. If you are sure that type is valid use to skip type validation. /// public static TypeIdDecoded New(string type) => FromUuidV7(type, UuidGenerator.New()); - + /// /// Generates new TypeId with the specified type and random UUIDv7. If is false, type is not validated. ///