Skip to content

Commit d81a8ab

Browse files
committed
Add allows ref struct
1 parent 45d19e6 commit d81a8ab

File tree

10 files changed

+54
-13
lines changed

10 files changed

+54
-13
lines changed

AdvancedSharpAdbClient/DeviceMonitor.Async.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ public partial class DeviceMonitor
1919
/// is used to block the <see cref="StartAsync(CancellationToken)"/> method until the <see cref="DeviceMonitorLoopAsync"/>
2020
/// has processed the first list of devices.
2121
/// </summary>
22+
#if NET5_0_OR_GREATER
23+
protected TaskCompletionSource? FirstDeviceListParsed;
24+
#else
2225
protected TaskCompletionSource<object?>? FirstDeviceListParsed;
26+
#endif
2327

2428
/// <summary>
2529
/// A <see cref="CancellationToken"/> that can be used to cancel the <see cref="MonitorTask"/>.
@@ -39,11 +43,11 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
3943
{
4044
try
4145
{
42-
FirstDeviceListParsed = new TaskCompletionSource<object?>();
46+
FirstDeviceListParsed = new();
4347
using CancellationTokenRegistration cancellationTokenRegistration = cancellationToken.Register(() => FirstDeviceListParsed.SetCanceled());
4448
MonitorTask = DeviceMonitorLoopAsync(MonitorTaskCancellationTokenSource.Token);
4549
// Wait for the worker thread to have read the first list of devices.
46-
_ = await FirstDeviceListParsed.Task.ConfigureAwait(false);
50+
await FirstDeviceListParsed.Task.ConfigureAwait(false);
4751
}
4852
finally
4953
{
@@ -133,7 +137,15 @@ protected virtual async Task DeviceMonitorLoopAsync(CancellationToken cancellati
133137
if (FirstDeviceListParsed != null)
134138
{
135139
// Switch to the background thread to avoid blocking the caller.
136-
_ = Task.Factory.StartNew(() => FirstDeviceListParsed?.TrySetResult(null), default, TaskCreationOptions.None, TaskScheduler.Default);
140+
_ = Task.Factory.StartNew(
141+
#if NET5_0_OR_GREATER
142+
() => FirstDeviceListParsed?.TrySetResult(),
143+
#else
144+
() => FirstDeviceListParsed?.TrySetResult(null),
145+
#endif
146+
default,
147+
TaskCreationOptions.None,
148+
TaskScheduler.Default);
137149
}
138150
}
139151
catch (TaskCanceledException ex)

AdvancedSharpAdbClient/DeviceMonitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ public void Start()
162162
{
163163
try
164164
{
165-
FirstDeviceListParsed = new TaskCompletionSource<object?>();
165+
FirstDeviceListParsed = new();
166166
MonitorTask = DeviceMonitorLoopAsync(MonitorTaskCancellationTokenSource.Token);
167167
// Wait for the worker thread to have read the first list of devices.
168-
_ = FirstDeviceListParsed.Task.AwaitByTaskCompleteSource();
168+
FirstDeviceListParsed.Task.AwaitByTaskCompleteSource();
169169
}
170170
finally
171171
{

AdvancedSharpAdbClient/Extensions/EnumerableBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,13 @@ static void ReadLogEntry(BinaryReader reader, params ICollection<object> parent)
276276
ushort? ReadUInt16(in ReadOnlySpan<byte> bytes)
277277
{
278278
ReadOnlySpan<byte> data = ReadBytesSafe(bytes, 2);
279-
return data == null ? null : (ushort)(data[0] | (data[1] << 8));
279+
return data.IsEmpty ? null : (ushort)(data[0] | (data[1] << 8));
280280
}
281281

282282
uint? ReadUInt32(in ReadOnlySpan<byte> bytes)
283283
{
284284
ReadOnlySpan<byte> data = ReadBytesSafe(bytes, 4);
285-
return data == null ? null : (uint)(data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));
285+
return data.IsEmpty ? null : (uint)(data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24));
286286
}
287287

288288
int? ReadInt32(in ReadOnlySpan<byte> bytes)

AdvancedSharpAdbClient/Logs/Interfaces/ILogger.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ public interface ILogger
2828
/// Generally used to enable activation of a named <see cref="ILogger"/> from dependency injection.
2929
/// </summary>
3030
/// <typeparam name="TCategoryName">The type whose name is used for the logger category name.</typeparam>
31-
public interface ILogger<out TCategoryName> : ILogger;
31+
public interface ILogger<out TCategoryName> : ILogger
32+
#if NET9_0_OR_GREATER
33+
where TCategoryName : allows ref struct
34+
#endif
35+
;
3236
}

AdvancedSharpAdbClient/Logs/Interfaces/ILoggerFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public interface ILoggerFactory
2121
/// </summary>
2222
/// <typeparam name="T">The category name for messages produced by the logger.</typeparam>
2323
/// <returns>The <see cref="ILogger"/>.</returns>
24-
ILogger<T> CreateLogger<T>();
24+
ILogger<T> CreateLogger<T>()
25+
#if NET9_0_OR_GREATER
26+
where T : allows ref struct
27+
#endif
28+
;
2529
}
2630
}

AdvancedSharpAdbClient/Logs/LoggerProvider.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public static class LoggerProvider
3232
/// </summary>
3333
/// <typeparam name="T">The type.</typeparam>
3434
/// <returns>The <see cref="ILogger"/> that was created</returns>
35-
public static ILogger<T> CreateLogger<T>() => _loggerFactory == null ? NullLogger<T>.Instance : _loggerFactory.CreateLogger<T>();
35+
public static ILogger<T> CreateLogger<T>()
36+
#if NET9_0_OR_GREATER
37+
where T : allows ref struct
38+
#endif
39+
=> _loggerFactory == null ? NullLogger<T>.Instance : _loggerFactory.CreateLogger<T>();
3640
}
3741
}

AdvancedSharpAdbClient/Logs/NullLogger.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public void Log(LogLevel logLevel, Exception? exception, string? message, params
2424
/// Minimalistic logger that does nothing.
2525
/// </summary>
2626
public class NullLogger<T> : NullLogger, ILogger<T>
27+
#if NET9_0_OR_GREATER
28+
where T : allows ref struct
29+
#endif
2730
{
2831
/// <summary>
2932
/// Returns an instance of <see cref="NullLogger{T}"/>.

AdvancedSharpAdbClient/Models/AdbCommandLineStatus.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public readonly IEnumerator<string> GetEnumerator()
149149
}
150150

151151
/// <inheritdoc/>
152-
public override string ToString() => string.Join(Environment.NewLine, [.. this]);
152+
public override string ToString() => string.Join(Environment.NewLine, (string[])[.. this]);
153153

154154
#if NET7_0_OR_GREATER
155155
[GeneratedRegex(VersionPattern)]

AdvancedSharpAdbClient/Polyfills/Extensions/TaskExExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.ComponentModel;
99
using System.Runtime.CompilerServices;
1010
using System.Threading;
11+
using System.Threading.Tasks;
1112

1213
#if NET40
1314
using Microsoft.Runtime.CompilerServices;
@@ -137,21 +138,31 @@ public static TResult AwaitByTaskCompleteSource<TResult>(this IAsyncOperation<TR
137138
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work if it has not yet started.</param>
138139
public static void AwaitByTaskCompleteSource(this Task function, CancellationToken cancellationToken = default)
139140
{
141+
#if NET5_0_OR_GREATER
142+
TaskCompletionSource taskCompletionSource = new();
143+
#else
140144
TaskCompletionSource<object?> taskCompletionSource = new();
141-
Task<object?> task = taskCompletionSource.Task;
145+
#endif
146+
#pragma warning disable IDE0008
147+
var task = taskCompletionSource.Task;
148+
#pragma warning restore IDE0008
142149
_ = Task.Factory.StartNew(async () =>
143150
{
144151
try
145152
{
146153
await function.ConfigureAwait(false);
154+
#if NET5_0_OR_GREATER
155+
_ = taskCompletionSource.TrySetResult();
156+
#else
147157
_ = taskCompletionSource.TrySetResult(null);
158+
#endif
148159
}
149160
catch (Exception e)
150161
{
151162
_ = taskCompletionSource.TrySetException(e);
152163
}
153164
}, cancellationToken);
154-
_ = task.Result;
165+
task.Wait(cancellationToken);
155166
}
156167

157168
/// <summary>

AdvancedSharpAdbClient/Polyfills/Interfaces/ICloneable.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ namespace AdvancedSharpAdbClient.Polyfills
4646
/// </summary>
4747
/// <typeparam name="T">The type of the class.</typeparam>
4848
public interface ICloneable<out T> : ICloneable
49+
#if NET9_0_OR_GREATER
50+
where T : allows ref struct
51+
#endif
4952
{
5053
/// <summary>
5154
/// Creates a new <typeparamref name="T"/> object that is a copy of the current instance.

0 commit comments

Comments
 (0)