Skip to content

Commit

Permalink
#72 Pure.DI not compatible with .NET 9
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Nov 5, 2024
1 parent 73e5912 commit 5c300c7
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/IVariator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Pure.DI.Core;

// ReSharper disable once IdentifierTypo
internal interface IVariator<T>
where T: class
{
bool TryGetNextVariants(
IEnumerable<IEnumerator<T>> variations,
Predicate<T> hasVariantsPredicate,
[NotNullWhen(true)] out IReadOnlyCollection<T>? variants);
}
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/ImplementationVariantsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public IEnumerable<DpImplementation> Build(DpImplementation implementation)
var variants =
implementation.Methods.Select(method => CreateVariants(method, ImplementationVariantKind.Method))
.Concat(Enumerable.Repeat(CreateVariants(implementation.Constructor, ImplementationVariantKind.Ctor), 1))
.Select(i => i.GetEnumerator())
.Select(i => new SafeEnumerator<ImplementationVariant>(i.GetEnumerator()))
.ToArray();

try
{
while (implementationVariator.TryGetNextVariants(variants, _ => true, out var curVariants))
while (implementationVariator.TryGetNextVariants(variants, out var curVariants))
{
cancellationToken.ThrowIfCancellationRequested();
yield return curVariants.Aggregate(
Expand Down
2 changes: 1 addition & 1 deletion src/Pure.DI.Core/Core/Models/ImplementationVariant.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Pure.DI.Core.Models;

internal readonly record struct ImplementationVariant(ImplementationVariantKind Kind, DpMethod Method);
internal record ImplementationVariant(ImplementationVariantKind Kind, DpMethod Method);
4 changes: 1 addition & 3 deletions src/Pure.DI.Core/Core/ProcessingNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Pure.DI.Core;

internal readonly struct ProcessingNode : IEquatable<ProcessingNode>
internal class ProcessingNode : IEquatable<ProcessingNode>
{
public readonly bool HasNode = false;
public readonly DependencyNode Node;
private readonly Lazy<bool> _isMarkerBased;
private readonly Lazy<ImmutableArray<InjectionInfo>> _injections;
Expand All @@ -15,7 +14,6 @@ public ProcessingNode(
ISet<Injection> contracts,
IMarker marker)
{
HasNode = true;
Node = node;
Contracts = contracts;

Expand Down
35 changes: 35 additions & 0 deletions src/Pure.DI.Core/Core/SafeEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma warning disable CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes).
namespace Pure.DI.Core;

internal class SafeEnumerator<T>(IEnumerator<T> source): IEnumerator<T>
where T: class
{
private T? _current;
private bool _result;

public T? Current
{
get
{
if (!_result)
{
return _current;
}

_current = source.Current;
return _current;
}
}

object? IEnumerator.Current => Current;

public bool MoveNext()
{
_result = source.MoveNext();
return _result;
}

public void Reset() => source.Reset();

public void Dispose() => source.Dispose();
}
4 changes: 2 additions & 2 deletions src/Pure.DI.Core/Core/VariationalDependencyGraphBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal sealed class VariationalDependencyGraphBuilder(
var maxIterations = globalOptions.MaxIterations;
DependencyGraph? first = default;
var maxAttempts = 0x2000;
while (variator.TryGetNextVariants(variants, node => !node.HasNode, out var nodes))
while (variator.TryGetNextVariants(variants, out var nodes))
{
if (maxAttempts-- == 0)
{
Expand Down Expand Up @@ -131,7 +131,7 @@ internal sealed class VariationalDependencyGraphBuilder(
[SuppressMessage("ReSharper", "NotDisposedResourceIsReturned")]
private static IEnumerable<Variation> CreateVariants(IEnumerable<ProcessingNode> nodes) =>
nodes.GroupBy(i => i.Node.Binding)
.Select(i => i.GetEnumerator());
.Select(i => new SafeEnumerator<ProcessingNode>(i.GetEnumerator()));

private static IEnumerable<DependencyNode> SortByPriority(IEnumerable<DependencyNode> nodes) =>
nodes.GroupBy(i => i.Binding)
Expand Down
18 changes: 12 additions & 6 deletions src/Pure.DI.Core/Core/Variator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@
namespace Pure.DI.Core;

internal sealed class Variator<T> : IVariator<T>
where T: class
{
public bool TryGetNextVariants(
IEnumerable<IEnumerator<T>> variations,
Predicate<T> hasVariantsPredicate,
[NotNullWhen(true)] out IReadOnlyCollection<T>? variants)
{
var hasNext = false;
var curVariants = new List<T>();
foreach (var enumerator in variations)
{
if (!hasNext && enumerator.MoveNext())
if (enumerator.Current is null)
{
hasNext = true;
curVariants.Add(enumerator.Current);
enumerator.MoveNext();
var current = enumerator.Current;
if (current is not null)
{
curVariants.Add(current);
hasNext = true;
}

continue;
}

if (hasVariantsPredicate(enumerator.Current))
if (!hasNext)
{
enumerator.MoveNext();
hasNext = enumerator.MoveNext();
}

curVariants.Add(enumerator.Current);
Expand Down

0 comments on commit 5c300c7

Please sign in to comment.