Skip to content

Commit

Permalink
Use ReadOnlySpan<T> instead of Span<T> in more places
Browse files Browse the repository at this point in the history
Fixes #1211
  • Loading branch information
AArnott committed Sep 10, 2024
1 parent 25fc640 commit 6201117
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverloads(MethodDefi
{
remainsRefType = false;
parameters[param.SequenceNumber - 1] = parameters[param.SequenceNumber - 1]
.WithType((isIn && isConst ? MakeReadOnlySpanOfT(elementType) : MakeSpanOfT(elementType)).WithTrailingTrivia(TriviaList(Space)));
.WithType((isIn ? MakeReadOnlySpanOfT(elementType) : MakeSpanOfT(elementType)).WithTrailingTrivia(TriviaList(Space)));
fixedBlocks.Add(VariableDeclaration(externParam.Type).AddVariables(
VariableDeclarator(localName.Identifier).WithInitializer(EqualsValueClause(origName))));
arguments[param.SequenceNumber - 1] = Argument(localName);
Expand Down Expand Up @@ -323,7 +323,7 @@ private IEnumerable<MethodDeclarationSyntax> DeclareFriendlyOverloads(MethodDefi

// Accept a span instead of a pointer.
parameters[param.SequenceNumber - 1] = parameters[param.SequenceNumber - 1]
.WithType((isIn && isConst ? MakeReadOnlySpanOfT(elementType) : MakeSpanOfT(elementType)).WithTrailingTrivia(TriviaList(Space)));
.WithType((isIn ? MakeReadOnlySpanOfT(elementType) : MakeSpanOfT(elementType)).WithTrailingTrivia(TriviaList(Space)));
fixedBlocks.Add(VariableDeclaration(externParam.Type).AddVariables(
VariableDeclarator(localName.Identifier).WithInitializer(EqualsValueClause(origName))));
arguments[param.SequenceNumber - 1] = Argument(localName);
Expand Down
10 changes: 10 additions & 0 deletions test/Microsoft.Windows.CsWin32.Tests/FriendlyOverloadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ public void SpecializedRAIIFree_OutParameter()
Assert.Equal("DsGetDcCloseWSafeHandle", Assert.IsType<IdentifierNameSyntax>(method.ParameterList.Parameters.Last().Type).Identifier.ValueText);
}

[Fact]
public void InAttributeOnArraysProjectedAsReadOnlySpan()
{
const string Method = "RmRegisterResources";
this.GenerateApi(Method);

MethodDeclarationSyntax method = Assert.Single(this.FindGeneratedMethod(Method), m => !IsOrContainsExternMethod(m));
Assert.Equal(3, method.ParameterList.Parameters.Count(p => p.Type is GenericNameSyntax { Identifier.ValueText: "ReadOnlySpan" }));
}

private void Generate(string name)
{
this.compilation = this.compilation.WithOptions(this.compilation.Options.WithPlatform(Platform.X64));
Expand Down

0 comments on commit 6201117

Please sign in to comment.