Skip to content

Commit 0250104

Browse files
Merge pull request #8859 from DustinCampbell/tweak-disposable-action
Change DisposableAction to ref struct
2 parents ec460a9 + aac28da commit 0250104

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Legacy/DisposableAction.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System;
75

86
namespace Microsoft.AspNetCore.Razor.Language.Legacy;
97

10-
internal class DisposableAction : IDisposable
8+
internal ref struct DisposableAction
119
{
1210
private readonly Action _action;
1311
private bool _invoked;
1412

1513
public DisposableAction(Action action)
1614
{
17-
if (action == null)
18-
{
19-
throw new ArgumentNullException(nameof(action));
20-
}
21-
22-
_action = action;
15+
_action = action ?? throw new ArgumentNullException(nameof(action));
2316
}
2417

2518
public void Dispose()

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Legacy/TokenizerBackedParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,17 +626,17 @@ protected TNode GetNodeWithEditHandler<TNode>(TNode node) where TNode : Syntax.G
626626
return (TNode)node.SetAnnotations(new[] { annotation });
627627
}
628628

629-
protected IDisposable PushSpanContextConfig()
629+
protected DisposableAction PushSpanContextConfig()
630630
{
631631
return PushSpanContextConfig(newConfig: (SpanContextConfigActionWithPreviousConfig)null);
632632
}
633633

634-
protected IDisposable PushSpanContextConfig(SpanContextConfigAction newConfig)
634+
protected DisposableAction PushSpanContextConfig(SpanContextConfigAction newConfig)
635635
{
636636
return PushSpanContextConfig(newConfig == null ? null : (SpanEditHandlerBuilder span, ref ISpanChunkGenerator chunkGenerator, SpanContextConfigAction _) => newConfig(span, ref chunkGenerator));
637637
}
638638

639-
protected IDisposable PushSpanContextConfig(SpanContextConfigActionWithPreviousConfig newConfig)
639+
protected DisposableAction PushSpanContextConfig(SpanContextConfigActionWithPreviousConfig newConfig)
640640
{
641641
var old = SpanContextConfig;
642642
ConfigureSpanContext(newConfig);

src/Razor/src/Microsoft.VisualStudio.Editor.Razor/BackgroundParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void Dispose()
5858
_main.Dispose();
5959
}
6060

61-
public IDisposable SynchronizeMainThreadState()
61+
public DisposableAction SynchronizeMainThreadState()
6262
{
6363
return _main.Lock();
6464
}
@@ -147,7 +147,7 @@ public void Cancel()
147147
_cancelSource.Cancel();
148148
}
149149

150-
public IDisposable Lock()
150+
public DisposableAction Lock()
151151
{
152152
Monitor.Enter(_stateLock);
153153
return new DisposableAction(() => Monitor.Exit(_stateLock));

src/Razor/src/Microsoft.VisualStudio.Editor.Razor/DefaultVisualStudioRazorParser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.IO;
99
using System.Threading;
1010
using System.Threading.Tasks;
11+
using Microsoft.AspNetCore.Razor;
1112
using Microsoft.AspNetCore.Razor.Language;
1213
using Microsoft.AspNetCore.Razor.Language.Legacy;
1314
using Microsoft.CodeAnalysis.Razor;
@@ -326,7 +327,7 @@ private void TextBuffer_OnChanged(object sender, TextContentChangedEventArgs arg
326327
var change = new SourceChange(changeInformation.firstChange.OldPosition, changeInformation.oldText.Length, changeInformation.newText);
327328
var result = PartialParseResultInternal.Rejected;
328329
RazorSyntaxTree? partialParseSyntaxTree = null;
329-
using (_parser!.SynchronizeMainThreadState())
330+
using (_parser.AssumeNotNull().SynchronizeMainThreadState())
330331
{
331332
// Check if we can partial-parse
332333
if (_partialParser is not null && _parser.IsIdle)

0 commit comments

Comments
 (0)