Skip to content

Commit 6de1efb

Browse files
committed
移除兼容性代码,使用 .NET 6/7 新特性/糖
1 parent 3d7c01e commit 6de1efb

File tree

27 files changed

+151
-272
lines changed

27 files changed

+151
-272
lines changed

src/Cosmos.Core/Cosmos/Exceptions/Try.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,20 @@ public static partial class Try
2323
/// Lifts <br />
2424
/// 直接使用一个异常生成 <see cref="Try{T}"/> 实例
2525
/// </summary>
26-
/// <param name="ex"></param>
26+
/// <param name="exception"></param>
2727
/// <param name="cause"></param>
2828
/// <typeparam name="T"></typeparam>
2929
/// <returns></returns>
30-
public static Try<T> LiftException<T>(Exception ex, string cause = null) => new Failure<T>(ex, cause);
30+
public static Try<T> LiftException<T>(Exception exception, string cause = null) => new Failure<T>(exception, cause);
3131

32-
private static void NotNull<T>(T t, string nameOfParams)
33-
{
34-
#if NET6_0_OR_GREATER
35-
ArgumentNullException.ThrowIfNull(t, nameOfParams);
36-
#else
37-
if (t is null)
38-
throw new ArgumentNullException(nameOfParams);
39-
#endif
40-
}
41-
42-
private static void TaskGuard(Task task, string nameOfParams)
43-
{
44-
if (task is null)
45-
throw new InvalidOperationException($"The task factory {nameOfParams} failed to run.");
46-
}
32+
// private static void NotNull<T>(T? t, string nameOfParams)
33+
// {
34+
// ArgumentNullException.ThrowIfNull(t, nameOfParams);
35+
// }
36+
//
37+
// private static void TaskGuard(Task? task, string nameOfParams)
38+
// {
39+
// if (task is null)
40+
// throw new InvalidOperationException($"The task factory {nameOfParams} failed to run.");
41+
// }
4742
}

src/Cosmos.Core/Cosmos/Exceptions/TryAction.Success.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal SuccessAction(int hashOfAction)
4848
public override void Deconstruct(out bool tryResult, out TryInvokingException exception)
4949
{
5050
tryResult = IsSuccess;
51-
exception = default;
51+
exception = default!;
5252
}
5353

5454
/// <inheritdoc />

src/Cosmos.Core/Cosmos/Exceptions/Try`1.Failure.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ internal Failure(Exception exception, string cause)
4545
public override bool Equals(object obj) => obj is Failure<T> failure && Equals(failure);
4646

4747
/// <inheritdoc/>
48-
public override int GetHashCode() => Exception?.GetHashCode() ?? 0;
48+
public override int GetHashCode() => Exception.GetHashCode();
4949

5050
/// <inheritdoc />
5151
public override void Deconstruct(out T value, out Exception exception)
5252
{
53-
value = default;
53+
value = default!;
5454
exception = Exception;
5555
}
5656

@@ -125,7 +125,7 @@ public override Try<T> Tap(Action<T> successFunction = null, Action<TryCreatingV
125125
/// <inheritdoc />
126126
public override Try<T> Tap(Action<T> successFunction = null, Action<Exception, string> failureFunction = null)
127127
{
128-
failureFunction?.Invoke(Exception?.InnerException, Exception?.Cause);
128+
failureFunction?.Invoke(Exception.InnerException, Exception.Cause);
129129
return this;
130130
}
131131

src/Cosmos.Core/Cosmos/Exceptions/Try`1.Success.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal Success(T value)
2323
public override bool IsSuccess => true;
2424

2525
/// <inheritdoc />
26-
public override TryCreatingValueException Exception => default;
26+
public override TryCreatingValueException Exception => default!;
2727

2828
/// <inheritdoc />
2929
public override T Value { get; }
@@ -48,14 +48,14 @@ internal Success(T value)
4848
public override void Deconstruct(out T value, out Exception exception)
4949
{
5050
value = Value;
51-
exception = default;
51+
exception = default!;
5252
}
5353

5454
public override void Deconstruct(out T value, out bool tryResult, out Exception exception)
5555
{
5656
value = Value;
5757
tryResult = IsSuccess;
58-
exception = default;
58+
exception = default!;
5959
}
6060

6161
/// <inheritdoc />

src/Cosmos.Core/Cosmos/Exceptions/Try`1.cs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public virtual Task<T> GetValueAsync()
5757
/// <returns></returns>
5858
public virtual T GetSafeValue()
5959
{
60-
return IsSuccess ? Value : default;
60+
return IsSuccess ? Value : default!;
6161
}
6262

6363
/// <summary>
@@ -82,7 +82,7 @@ public virtual T GetSafeValue(Func<T> defaultValFunc)
8282
return IsSuccess
8383
? Value
8484
: defaultValFunc is null
85-
? default
85+
? default!
8686
: defaultValFunc();
8787
}
8888

@@ -97,7 +97,7 @@ public virtual T GetSafeValue(Func<TryCreatingValueException, T> defaultValFunc)
9797
return IsSuccess
9898
? Value
9999
: defaultValFunc is null
100-
? default
100+
? default!
101101
: defaultValFunc(Exception);
102102
}
103103

@@ -112,8 +112,8 @@ public virtual T GetSafeValue(Func<Exception, string, T> defaultValFunc)
112112
return IsSuccess
113113
? Value
114114
: defaultValFunc is null
115-
? default
116-
: defaultValFunc(Exception.InnerException, Exception.Cause);
115+
? default!
116+
: defaultValFunc(Exception?.InnerException, Exception?.Cause);
117117
}
118118

119119
/// <summary>
@@ -181,7 +181,7 @@ public virtual Task<T> GetSafeValueAsync(Func<Task<T>> defaultValAsyncFunc)
181181
if (IsSuccess)
182182
return Task.FromResult(Value);
183183
return defaultValAsyncFunc is null
184-
? Task.FromResult(default(T))
184+
? Task.FromResult(default(T)!)
185185
: defaultValAsyncFunc();
186186
}
187187

@@ -196,7 +196,7 @@ public virtual Task<T> GetSafeValueAsync(Func<TryCreatingValueException, Task<T>
196196
if (IsSuccess)
197197
return Task.FromResult(Value);
198198
return defaultValAsyncFunc is null
199-
? Task.FromResult(default(T))
199+
? Task.FromResult(default(T)!)
200200
: defaultValAsyncFunc(Exception);
201201
}
202202

@@ -211,8 +211,8 @@ public virtual Task<T> GetSafeValueAsync(Func<Exception, string, Task<T>> defaul
211211
if (IsSuccess)
212212
return Task.FromResult(Value);
213213
return defaultValAsyncFunc is null
214-
? Task.FromResult(default(T))
215-
: defaultValAsyncFunc(Exception.InnerException, Exception.Cause);
214+
? Task.FromResult(default(T)!)
215+
: defaultValAsyncFunc(Exception?.InnerException, Exception?.Cause);
216216
}
217217

218218
/// <summary>
@@ -256,7 +256,7 @@ public virtual bool TryGetValue(out T value, Func<T> defaultValFunc)
256256
}
257257
catch
258258
{
259-
value = default;
259+
value = default!;
260260
return false;
261261
}
262262
}
@@ -277,7 +277,7 @@ public virtual bool TryGetValue(out T value, Func<TryCreatingValueException, T>
277277
}
278278
catch
279279
{
280-
value = default;
280+
value = default!;
281281
return false;
282282
}
283283
}
@@ -298,7 +298,7 @@ public virtual bool TryGetValue(out T value, Func<Exception, string, T> defaultV
298298
}
299299
catch
300300
{
301-
value = default;
301+
value = default!;
302302
return false;
303303
}
304304
}
@@ -611,8 +611,6 @@ public TException ExceptionAs<TException>() where TException : Exception
611611
/// <returns></returns>
612612
public Try<TResult> Map<TResult>(Func<T, TResult> map)
613613
{
614-
if (map is null)
615-
throw new ArgumentNullException(nameof(map));
616614
return Bind(value => Try.Create(() => map(value)));
617615
}
618616

@@ -642,18 +640,7 @@ public Try<TResult> Map<TResult>(Func<T, TResult> map)
642640

643641
private static Task<TResult> FromException<TResult>(Exception exception)
644642
{
645-
#if NET6_0_OR_GREATER
646643
ArgumentNullException.ThrowIfNull(exception, nameof(exception));
647-
#else
648-
if (exception is null)
649-
throw new ArgumentNullException(nameof(exception));
650-
#endif
651-
#if NET451 || NET452
652-
var tcs = new TaskCompletionSource<TResult>();
653-
tcs.TrySetException(exception);
654-
return tcs.Task;
655-
#else
656644
return Task.FromException<TResult>(exception);
657-
#endif
658645
}
659646
}

src/Cosmos.Core/Cosmos/ObjectExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using Cosmos.Text;
2-
31
// ReSharper disable once CheckNamespace
42
namespace Cosmos;
53

@@ -48,7 +46,7 @@ public static T AsOrDefault<T>(this object @this)
4846
}
4947
catch (Exception)
5048
{
51-
return default;
49+
return default!;
5250
}
5351
}
5452

@@ -137,7 +135,7 @@ public static bool TryAs<T>(this object @this, out T value)
137135
}
138136
catch
139137
{
140-
value = default;
138+
value = default!;
141139
return false;
142140
}
143141
}

src/Cosmos.Core/Cosmos/PairOf.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ public PairOf(T1 item1, T2 item2)
1919
Item2 = item2;
2020
}
2121

22-
public bool Equals(PairOf<T1, T2> obj) => Item1.Equals(obj.Item1) && Item2.Equals(obj.Item2);
22+
public bool Equals(PairOf<T1, T2> obj)
23+
{
24+
if (Item1 is null && Item2 is null && obj.Item1 is null && obj.Item2 is null)
25+
return true;
26+
if (Item1 is null || Item2 is null)
27+
return false;
28+
return Item1.Equals(obj.Item1) && Item2.Equals(obj.Item2);
29+
}
2330

2431
public override bool Equals(object obj) => obj is PairOf<T1, T2> of && Equals(of);
2532

@@ -31,7 +38,7 @@ public override int GetHashCode()
3138
return (hash1 << 5) + hash1 ^ hash2;
3239
}
3340

34-
public override string ToString() => $"({Item1?.ToString() ?? "null"}, {Item2.ToString() ?? "null"})";
41+
public override string ToString() => $"({Item1?.ToString() ?? "null"}, {Item2?.ToString() ?? "null"})";
3542

3643
public void Deconstruct(out T1 item1, out T2 item2)
3744
{

src/Cosmos.Core/Cosmos/Reflection/Reflectors/Internals/InternalRefactorExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
internal static class InternalRefactorExtensions
44
{
5-
internal static MethodInfo? GetMethodBySign(this TypeInfo typeInfo, MethodInfo method)
5+
internal static MethodInfo GetMethodBySign(this TypeInfo typeInfo, MethodInfo method)
66
{
77
return typeInfo.DeclaredMethods.FirstOrDefault(m => m.ToString() == method.ToString());
88
}

src/Cosmos.Core/Cosmos/Reflection/Reflectors/MethodReflectors/MethodReflector.Call.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected override Func<object, object[], object> CreateInvoker()
7878
}
7979
});
8080

81-
Func<object, object[], object> CreateDelegate(Action? callback = null)
81+
Func<object, object[], object> CreateDelegate(Action callback = null)
8282
{
8383
ilGen.EmitCall(OpCodes.Call, _reflectionInfo, null);
8484
callback?.Invoke();

src/Cosmos.Core/Cosmos/Reflection/Reflectors/MethodReflectors/MethodReflector.Static.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected override Func<object, object[], object> CreateInvoker()
6666
}
6767
});
6868

69-
Func<object, object[], object> CreateDelegate(Action? callback = null)
69+
Func<object, object[], object> CreateDelegate(Action callback = null)
7070
{
7171
ilGen.EmitCall(OpCodes.Call, _reflectionInfo, null);
7272
callback?.Invoke();

src/Cosmos.Core/Cosmos/Reflection/Reflectors/MethodReflectors/MethodReflector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected virtual Func<object, object[], object> CreateInvoker()
7979
}
8080
});
8181

82-
Func<object, object[], object> CreateDelegate(Action? callback = null)
82+
Func<object, object[], object> CreateDelegate(Action callback = null)
8383
{
8484
ilGen.EmitCall(OpCodes.Callvirt, _reflectionInfo, null);
8585
callback?.Invoke();

src/Cosmos.Core/Cosmos/Reflection/TypePairOf.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static TypePairOf Create<TSource, TTarget>()
6666
return new TypePairOf(typeof(TSource), typeof(TTarget));
6767
}
6868

69-
public override bool Equals(object? obj)
69+
public override bool Equals(object obj)
7070
{
7171
if (ReferenceEquals(null, obj))
7272
{

src/Cosmos.Core/Cosmos/Text/Internals/Base32.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text;
2-
3-
namespace Cosmos.Text.Internals;
1+
namespace Cosmos.Text.Internals;
42
/*
53
* Reference to:
64
* https://github.com/KvanTTT/BaseNcoding/blob/master/BaseNcoding/Base32.cs
@@ -23,6 +21,9 @@ public Base32(string alphabet = DefaultAlphabet, char special = DefaultSpecial,
2321

2422
public override string Encode(byte[] data)
2523
{
24+
if (data is null || data.Length == 0)
25+
return string.Empty;
26+
2627
var dataLength = data.Length;
2728
var result = new StringBuilder((dataLength + 4) / 5 * 8);
2829

@@ -107,7 +108,7 @@ public override byte[] Decode(string data)
107108
unchecked
108109
{
109110
if (string.IsNullOrEmpty(data))
110-
return InternalArray.ForEmpty<byte>();
111+
return Array.Empty<byte>();
111112

112113
int additionalBytes = 0, diff, tempLen;
113114

src/Cosmos.Core/Cosmos/Text/Internals/Base64.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text;
2-
3-
namespace Cosmos.Text.Internals;
1+
namespace Cosmos.Text.Internals;
42
/*
53
* Reference to:
64
* https://github.com/KvanTTT/BaseNcoding/blob/master/BaseNcoding/Base32.cs
@@ -23,6 +21,9 @@ public Base64(string alphabet = DefaultAlphabet, char special = DefaultSpecial,
2321

2422
public override string Encode(byte[] data)
2523
{
24+
if (data is null || data.Length == 0)
25+
return string.Empty;
26+
2627
var resultLength = (data.Length + 2) / 3 * 4;
2728
var result = new char[resultLength];
2829

@@ -78,7 +79,7 @@ public override byte[] Decode(string data)
7879
unchecked
7980
{
8081
if (string.IsNullOrEmpty(data))
81-
return new byte[0];
82+
return Array.Empty<byte>();
8283

8384
var lastSpecialInd = data.Length;
8485
while (data[lastSpecialInd - 1] == Special)

src/Cosmos.Core/Cosmos/Text/Internals/Base91.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text;
2-
3-
namespace Cosmos.Text.Internals;
1+
namespace Cosmos.Text.Internals;
42

53
/*
64
* Reference to:
@@ -28,6 +26,9 @@ public Base91(string alphabet = DefaultAlphabet, char special = DefaultSpecial,
2826

2927
public override string Encode(byte[] data)
3028
{
29+
if (data is null || data.Length == 0)
30+
return string.Empty;
31+
3132
var result = new StringBuilder(data.Length);
3233

3334
int ebq = 0, en = 0;
@@ -72,6 +73,9 @@ public override byte[] Decode(string data)
7273
{
7374
unchecked
7475
{
76+
if (string.IsNullOrEmpty(data))
77+
return Array.Empty<byte>();
78+
7579
int dbq = 0, dn = 0, dv = -1;
7680

7781
var result = new List<byte>(data.Length);

0 commit comments

Comments
 (0)