Commit a92c758b authored by Ben Lambell's avatar Ben Lambell

Add support for serializing enumerables

parent 9512207e
/*
* Copyright (c) 2017 Ben Lambell.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
namespace Recurse.Persist.Test
{
public interface IEnumeratorState<T>
{
T Value { get; }
IEnumeratorState<T> GetNext();
}
}
......@@ -15,15 +15,15 @@ namespace Recurse.Persist.Test
[Serializable]
public class SerializableEnumerator<T> : IEnumerator<T>
{
private readonly SerializableEnumeratorState<T> _start;
private readonly IEnumeratorState<T> _start;
private SerializableEnumeratorState<T> _current;
private IEnumeratorState<T> _current;
public T Current => _current != null ? _current.Value : default(T);
object IEnumerator.Current => Current;
public SerializableEnumerator(Func<SerializableEnumeratorState<T>> start)
public SerializableEnumerator(Func<IEnumeratorState<T>> start)
{
_start = new SerializableEnumeratorState<T>(default(T), start);
_current = _start;
......@@ -36,7 +36,7 @@ namespace Recurse.Persist.Test
public bool MoveNext()
{
_current = _current?.Next?.Invoke();
_current = _current?.GetNext();
return _current != null;
}
......
......@@ -12,20 +12,22 @@ using Recurse.Persist.Serializables;
namespace Recurse.Persist.Test
{
[Serializable]
public class SerializableEnumeratorState<T>
public class SerializableEnumeratorState<T> : IEnumeratorState<T>
{
public T Value { get; }
private readonly SerializableFunc<IEnumeratorState<T>> _getNext;
public SerializableFunc<SerializableEnumeratorState<T>> Next { get; }
public T Value { get; }
public SerializableEnumeratorState(T value, Func<SerializableEnumeratorState<T>> next) : this(value)
public SerializableEnumeratorState(T value, Func<IEnumeratorState<T>> next) : this(value)
{
Next = new SerializableFunc<SerializableEnumeratorState<T>>(next);
_getNext = new SerializableFunc<IEnumeratorState<T>>(next);
}
public SerializableEnumeratorState(T value)
{
Value = value;
}
public IEnumeratorState<T> GetNext() => _getNext?.Invoke();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment