draconware-dev / SpanExtensions.Net

SpanExtensions.Net aims to make ReadOnlySpan<T> and Span<T> more accessible and overall better integrated into the .Net Ecosystem, by providing alternatives for many missing Extension Methods for Span<T>, ranging from string.Split() over Enumerable.Skip() and Enumerable.Take() to an improved ReadOnlySpan<T>.IndexOf().
MIT License
17 stars 5 forks source link

Source Generators #17

Open Guiorgy opened 4 months ago

Guiorgy commented 4 months ago

We should create a source generator that takes the source for Span<T> and replaces occurrences of Span<x> with ReadOnlySpan<x>. Since most of the source for the two is the same, this should simplify maintenance, and reduce the risk of mistakes where one forgets to update the implementation of one after changing it for the other.

Note: Replacing ReadOnlySpan<x> with Span<T> is not advised, since ReadOnlySpan<T> implementations may also accept a ReadOnlySpan for a different purpose that should remain as a ReadOnlySpan in the Span<T> implementation, for example:

public static SpanSplitSequenceEnumerator<T> Split<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> delimiter) where T : IEquatable<T>

public static SpanSplitSequenceEnumerator<T> Split<T>(this Span<T> span, ReadOnlySpan<T> delimiter) where T : IEquatable<T>

delimiter is a ReadOnlySpan<T> in both cases.

PS. Add this to the backlog for now, I'll get to it after the current 2 PRs are dealt with.

dragon7307 commented 4 months ago

For most methods this will work...

Guiorgy commented 4 months ago

Even if it's only 70%, it would still substantially reduce the maintenance burden.

Also, when I say the generator should replace Span<x> with ReadOnlySpan<x>, that is only the default behavior. I plan to make the attribute take a params array of regex find and replace patterns optionally, so we can hopefully make it work with most of the codebase.