Skip to content

Commit

Permalink
fix box eq
Browse files Browse the repository at this point in the history
  • Loading branch information
2A5F committed Jan 22, 2024
1 parent c1cf4ac commit 51def9b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Benchmark/BenchmarkAHashString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BenchmarkAHashString
{
[Params("asd", "1234567890", "1234567890_1234567890_1234567890_1234567890",
"1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890_1234567890")]
public string Str;
public string? Str;

#if NET8_0_OR_GREATER
[Benchmark]
Expand Down
2 changes: 1 addition & 1 deletion BetterCollections/BetterCollections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.3.1</Version>
<Version>0.3.2</Version>
<PackageProjectUrl>https://github.com/libsugar/BetterCollections</PackageProjectUrl>
<Description>Some better collection implementations than the standard library</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
22 changes: 20 additions & 2 deletions BetterCollections/Memories/Box.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@

namespace BetterCollections.Memories;

public static class Box
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Box<T> Make<T>(T value) => new(value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlyBox<T> MakeReadOnly<T>(T value) => new(value);
}

public static class BoxEx
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Box<T> Box<T>(this T value) => new(value);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlyBox<T> ReadOnlyBox<T>(this T value) => new(value);
}

public class Box<T>(T Value) : IRef<T>, IReadOnlyRef<T>, IStrongBox,
IEquatable<T>, IEquatable<Box<T>>,
IComparable<T>, IComparable<Box<T>>
Expand All @@ -26,7 +44,7 @@ public class Box<T>(T Value) : IRef<T>, IReadOnlyRef<T>, IStrongBox,

public bool Equals(T? other) => EqualityComparer<T>.Default.Equals(Value, other!);

public bool Equals(Box<T>? other) => ReferenceEquals(other, null) && Equals(other!.Value);
public bool Equals(Box<T>? other) => !ReferenceEquals(other, null) && Equals(other.Value);


public override bool Equals(object? obj) => obj is Box<T> box ? Equals(box) : obj is T v && Equals(v);
Expand Down Expand Up @@ -100,7 +118,7 @@ public class ReadOnlyBox<T>(T Value) : IReadOnlyRef<T>,

public bool Equals(T? other) => EqualityComparer<T>.Default.Equals(Value, other!);

public bool Equals(ReadOnlyBox<T>? other) => ReferenceEquals(other, null) && Equals(other!.Value);
public bool Equals(ReadOnlyBox<T>? other) => !ReferenceEquals(other, null) && Equals(other.Value);


public override bool Equals(object? obj) => obj is ReadOnlyBox<T> box ? Equals(box) : obj is T v && Equals(v);
Expand Down

0 comments on commit 51def9b

Please sign in to comment.