Consider binding to `IUtf8SpanParsable<T>`/`ISpanParsable<T>` over `IParsable<T>` if a type implements it
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
Currently, if a particular type does not have `BindAsync` specified, which is often the case for libraries that cannot assume `Sdk.Web` project dependency, ASP.NET Core will try to bind parameters of such types to `IParsable` interface implementation.
However, if the type is representable in or parsable from UTF-8, ASP.NET Core may be able to feed the `ReadOnlySpan` observing the input buffer directly, bypassing string allocation and/or transcoding.
### Describe the solution you'd like
Given an example scenario:
```cs
var app = WebApplication
.CreateSlimBuilder(args)
.Build();
app.MapGet("/echo/{name}", (U8String name) =>
TypedResults.Bytes(u8($"Hello, {name}!"), contentType: "text/plain"));
app.Run();
```
The type `U8String` implements `IParsable`, `ISpanParsable` and `IUtf8SpanParsable`. Currently, only `IParsable` participates in binding method resolution. Both `ISpanParsable` and `IUtf8SpanParsable` could participate in the process, allowing for more efficient binding for types that do not need either `string` itself or UTF-16.
### Additional context
For backwards compatibility with pre-.NET 8 targets, the binding can be duck-typed similar to the way it works for `TryParse` itself.
Contributor guide
Research direction
Start by tracing ASP.NET Core parameter-binding method resolution for BindAsync, TryParse, IParsable, ISpanParsable, and IUtf8SpanParsable. Review how duck-typing supports pre-.NET 8 targets, then determine the compatibility and precedence requirements. Done means the U8String example binds through the most efficient applicable interface without regressing existing binding behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100