Skip to content

Commit 5868e63

Browse files
committed
Fix: some restructuring
1 parent 5401b8f commit 5868e63

File tree

3 files changed

+14
-30
lines changed

3 files changed

+14
-30
lines changed

src/CodeOfChaos.GeneratorTools/Backports/NullableAttribute.cs

-5
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ public DoesNotReturnIfAttribute(bool parameterValue) {
103103
public bool ParameterValue { get; }
104104
}
105105

106-
#endif
107-
108-
#if !NETCOREAPP
109-
110106
/// <summary>
111107
/// Specifies that the method or property will ensure that the listed field and property members have not-null
112108
/// values.
@@ -169,5 +165,4 @@ public MemberNotNullWhenAttribute(bool returnValue, params string[] members) {
169165
/// <summary>Gets field or property member names.</summary>
170166
public string[] Members { get; }
171167
}
172-
173168
#endif

src/CodeOfChaos.GeneratorTools/Backports/StackBackports.cs

+7-18
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,24 @@ namespace CodeOfChaos.GeneratorTools;
1717
// Code
1818
// ---------------------------------------------------------------------------------------------------------------------
1919
public static class StackBackports {
20+
// Done so we can easily
2021
#if NETSTANDARD2_0
21-
public static bool TryPop<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result) {
22-
result = default;
23-
if (stack.Count == 0) return false;
24-
result = stack.Pop();
25-
return result is not null;
26-
}
27-
28-
public static bool TryPeek<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result) {
29-
result = default;
30-
if (stack.Count == 0) return false;
31-
result = stack.Peek();
32-
return result is not null;
33-
}
22+
public static bool TryPop<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result) => TryPopBackport(stack, out result);
3423
#endif
35-
36-
#if NET9_0_OR_GREATER
37-
public static bool TryPop<T>(Stack<T> stack, [NotNullWhen(true)] out T? result) {
24+
public static bool TryPopBackport<T>(Stack<T> stack, [NotNullWhen(true)] out T? result) {
3825
result = default;
3926
if (stack.Count == 0) return false;
4027
result = stack.Pop();
4128
return result is not null;
4229
}
4330

44-
public static bool TryPeek<T>(Stack<T> stack, [NotNullWhen(true)] out T? result) {
31+
#if NETSTANDARD2_0
32+
public static bool TryPeek<T>(this Stack<T> stack, [NotNullWhen(true)] out T? result) => TryPeekBackport(stack, out result);
33+
#endif
34+
public static bool TryPeekBackport<T>(Stack<T> stack, [NotNullWhen(true)] out T? result) {
4535
result = default;
4636
if (stack.Count == 0) return false;
4737
result = stack.Peek();
4838
return result is not null;
4939
}
50-
#endif
5140
}

tests/Tests.CodeOfChaos.GeneratorTools/Backports/StackBackportsTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public async Task TryPop_EmptyStack_ReturnsFalse_AndOutNull() {
1515
var stack = new Stack<int>();
1616

1717
// Act
18-
bool result = StackBackports.TryPop(stack,out int value);
18+
bool result = StackBackports.TryPopBackport(stack,out int value);
1919

2020
// Assert
2121
await Assert.That(result).IsFalse().Because("TryPop should return false for an empty stack.");
@@ -31,7 +31,7 @@ public async Task TryPop_NonEmptyStack_ReturnsTrue_AndOutTopElement() {
3131
stack.Push(30);// Top element
3232

3333
// Act
34-
bool result = StackBackports.TryPop(stack,out int value);
34+
bool result = StackBackports.TryPopBackport(stack,out int value);
3535

3636
// Assert
3737
await Assert.That(result).IsTrue().Because("TryPop should return true for a non-empty stack.");
@@ -46,7 +46,7 @@ public async Task TryPeek_EmptyStack_ReturnsFalse_AndOutNull() {
4646
var stack = new Stack<string>();
4747

4848
// Act
49-
bool result = StackBackports.TryPeek(stack, out string? value);
49+
bool result = StackBackports.TryPeekBackport(stack, out string? value);
5050

5151
// Assert
5252
await Assert.That(result).IsFalse().Because("TryPeek should return false for an empty stack.");
@@ -63,7 +63,7 @@ public async Task TryPeek_NonEmptyStack_ReturnsTrue_AndOutTopElement() {
6363
stack.Push("top"); // Top element
6464

6565
// Act
66-
bool result = StackBackports.TryPeek(stack, out string? value);
66+
bool result = StackBackports.TryPeekBackport(stack, out string? value);
6767

6868
// Assert
6969
await Assert.That(result).IsTrue().Because("TryPeek should return true for a non-empty stack.");
@@ -79,7 +79,7 @@ public async Task TryPeekWithNullables_AllowsNullValueHandling() {
7979
stack.Push(10);
8080

8181
// Act
82-
bool result = StackBackports.TryPeek(stack, out int? value);
82+
bool result = StackBackports.TryPeekBackport(stack, out int? value);
8383

8484
// Assert
8585
await Assert.That(result).IsTrue().Because("TryPeek should return true for a non-empty stack (nullable).");
@@ -94,8 +94,8 @@ public async Task TryPeek_WithMutatingCollectionAfterPeek() {
9494
stack.Push(42);
9595

9696
// Act
97-
bool resultPeek = StackBackports.TryPeek(stack, out int resultPeekValue);
98-
bool resultPop = StackBackports.TryPop(stack,out int resultPopValue);
97+
bool resultPeek = StackBackports.TryPeekBackport(stack, out int resultPeekValue);
98+
bool resultPop = StackBackports.TryPopBackport(stack,out int resultPopValue);
9999

100100
// Assert
101101
await Assert.That(resultPeek).IsTrue().Because("TryPeek should succeed.");

0 commit comments

Comments
 (0)