diff --git a/src/SharpCompress/Archives/AbstractArchive.cs b/src/SharpCompress/Archives/AbstractArchive.cs index 0443684d..63e67e69 100644 --- a/src/SharpCompress/Archives/AbstractArchive.cs +++ b/src/SharpCompress/Archives/AbstractArchive.cs @@ -23,8 +23,8 @@ public abstract class AbstractArchive : IArchive, IAsyncArchive Type = type; ReaderOptions = sourceStream.ReaderOptions; _sourceStream = sourceStream; - _lazyVolumes = new LazyReadOnlyCollection(() => LoadVolumes(_sourceStream)); - _lazyEntries = new LazyReadOnlyCollection(() => LoadEntries(Volumes)); + _lazyVolumes = new LazyReadOnlyCollection(LoadVolumes(_sourceStream)); + _lazyEntries = new LazyReadOnlyCollection(LoadEntries(Volumes)); _lazyVolumesAsync = new LazyAsyncReadOnlyCollection( LoadVolumesAsync(_sourceStream) ); @@ -37,8 +37,8 @@ public abstract class AbstractArchive : IArchive, IAsyncArchive { Type = type; ReaderOptions = new(); - _lazyVolumes = new LazyReadOnlyCollection(() => Enumerable.Empty()); - _lazyEntries = new LazyReadOnlyCollection(() => Enumerable.Empty()); + _lazyVolumes = new LazyReadOnlyCollection(Enumerable.Empty()); + _lazyEntries = new LazyReadOnlyCollection(Enumerable.Empty()); _lazyVolumesAsync = new LazyAsyncReadOnlyCollection( AsyncEnumerableEx.Empty() ); diff --git a/src/SharpCompress/LazyReadOnlyCollection.cs b/src/SharpCompress/LazyReadOnlyCollection.cs index a83fd73c..eee60bc7 100644 --- a/src/SharpCompress/LazyReadOnlyCollection.cs +++ b/src/SharpCompress/LazyReadOnlyCollection.cs @@ -1,4 +1,4 @@ - +#nullable disable using System; using System.Collections; @@ -9,14 +9,10 @@ namespace SharpCompress; internal sealed class LazyReadOnlyCollection : ICollection { private readonly List _backing = new(); - private readonly Func> _factory; - private IEnumerator? _source; + private readonly IEnumerator _source; private bool _fullyLoaded; - - public LazyReadOnlyCollection(IEnumerable factory) => _factory = () => factory; - - public LazyReadOnlyCollection(Func> factory) => _factory = factory; + public LazyReadOnlyCollection(IEnumerable source) => _source = source.GetEnumerator(); private class LazyLoader : IEnumerator { @@ -47,7 +43,7 @@ internal sealed class LazyReadOnlyCollection : ICollection #region IEnumerator Members - object IEnumerator.Current => Current!; + object IEnumerator.Current => Current; public bool MoveNext() { @@ -56,10 +52,6 @@ internal sealed class LazyReadOnlyCollection : ICollection _index++; return true; } - if (_lazyReadOnlyCollection._source == null) - { - _lazyReadOnlyCollection._source = _lazyReadOnlyCollection._factory().GetEnumerator(); - } if (!_lazyReadOnlyCollection._fullyLoaded && _lazyReadOnlyCollection._source.MoveNext()) { _lazyReadOnlyCollection._backing.Add(_lazyReadOnlyCollection._source.Current);