Repository navigation
feat: ISpanFormattable and IUtf8SpanFormattable on every range, range set and value set (#41) - #654
Merged
Merged
Conversation
…aces, TryFormat on every type (#41) The three factory interfaces take both span interfaces in their base lists, with no default, and a static virtual TryFormatValue defaulting to FormatValue. Every range union (so every variant), range set and value set writes the text ToString gives, by construction: the first cut formats the string and copies it. SpanFormatContractTests holds parity in UTF-16 and UTF-8 and refuses every too-small destination; the literal provider and culture sweeps reach the span forms and interpolation too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…#41) Two seeds for SpanFormatContractTests (a cut literal reported as written, a UTF-8 destination measured in chars), both caught; claims J43 and J44 for the new docs/serialization.md paragraph; a migration section for a range type of one's own; Added and Breaking fragments; four Native AOT smoke cases, measured under JIT and a local osx-arm64 Native AOT publish. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #41
Problem
Ranges, range sets and value sets were
IFormattableonly. String interpolation,Utf8.TryWriteand other span writers had to go throughToString, and generic code over the factory interfaces couldn't reach a span path without a type test. The maintainer decided (2026-11-30) to putISpanFormattableandIUtf8SpanFormattablein the base lists of the three factory interfaces for 11.0.0, plus a static virtualTryFormatValuedefaulting toFormatValue.Change
Core/):IRangeFactory<TRange, T>,IRangeSetFactory<TSet, TRange, T>andIValueSetFactory<TSet, T>now extendISpanFormattableandIUtf8SpanFormattable, with no default instance member (that is the Native AOT on .NET 11 RC2: every value setToStringtraps (SIGTRAP) in the defaultIFormattable.ToStringofIValueSetFactory<TSet, T>#291 trap). This is breaking for a range type of your own. The other two can't be implemented outside the library (internal members).TryFormatValue(T, Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)is astatic virtualonIRangeFactoryandIValueSetFactory. Its default formats withFormatValueand copies.TryFormat(Span<char>, …)andTryFormat(Span<byte>, …)on every range union (13, inherited by each variant), range set (13, forwarding toRangeSetEnginelikeToStringdoes) and value set (34, wrapper arities included).Internals/SpanLiteral.csformats the string and copies it, so the output isToString's text by construction. An empty format span counts as no format. A destination that is too small getsfalsewith a count of 0. UTF-8 goes throughUtf8.FromUtf16, which is lossless here because a string set refuses a lone surrogate. Writing straight into the span throughTryFormatValueis the follow-up Write range, range set and value set literals straight into a span #656.UnknownRangeVariantTests.RogueRange(the one outside implementer in the repo) gets both methods. That is the break in practice.docs/serialization.md#print-a-range-as-a-literal, and a migration section, "If you implementIRangeFactoryyourself". Fragments:changes/41.md(Added) andchanges/41-interfaces.md(Breaking).Tests
SpanFormatContractTests(differential, registered inoracles.json), run over every discovered range shape, range set and value set:ToString(format, provider)for formats null,"",G,oandN2× four providers. When a format is refused, both span forms refuse it the same way.falsewith 0 written. The exact length fits with nothing written past it.TryFormatValuewritesFormatValue's text, and a buffer one char too small getsfalse.LiteralProviderContractTestsandLiteralCultureContractTestsnow also coverTryFormat(char and byte),string.Create(provider, $"{value}"),$"{value}"andUtf8.TryWriteunder the five cultures.span-format-writes-a-cut-literal(7 tests fail) andspan-format-measures-utf8-in-chars(3 fail). Both were caught, and both patches were generated from the committed tree.J43-tryformat-writes-the-tostring-textandJ44-tryformatvalue-writes-formatvalue-text. Surface coverage is complete.aot-tryformat*): generic overISpanFormattable/IUtf8SpanFormattable, interpolation andUtf8.TryWrite, andTryFormatValuethrough both factory interfaces, includingInt32Set<Code>(an instantiation over a value type that nothing else creates). Expected lines were measured under JIT and under a local osx-arm64 Native AOT publish (no trim warnings, same output). CI's smoke test is the authority.Found on the way:
Float64Range/Float32Range.FormatValueread""as an explicit format (their set siblings don't), soFormatValue(1.5, "", de-DE)is1,5. The literal isn't affected becauseToStringnormalizes first. Filed as #655; this PR's sweep compares an empty span againstFormatValue(…, null, …), which is what an empty span means.🤖 Generated with Claude Code