public static void TryGetOwnedMemory_IndexLength() { int[] array = new int[10]; OwnedMemory <int> orignalOwner = new CustomMemoryForTest <int>(array); ReadOnlyMemory <int> memory = orignalOwner.Memory; for (int i = 0; i < array.Length; i++) { Assert.True(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out CustomMemoryForTest <int> customOwner, out int index, out int length)); Assert.Same(orignalOwner, customOwner); Assert.Equal(i, index); Assert.Equal(array.Length - i, length); } for (int i = 0; i < array.Length; i++) { Assert.True(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out OwnedMemory <int> owner, out int index, out int length)); Assert.Same(orignalOwner, owner); Assert.Equal(i, index); Assert.Equal(array.Length - i, length); } for (int i = 0; i < array.Length; i++) { Assert.False(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out OtherMemoryForTest <int> notOwner, out int index, out int length)); Assert.Null(notOwner); } }
public static void TryGetOwnedMemoryFromDefaultMemory() { ReadOnlyMemory <int> memory = default; Assert.False(MemoryMarshal.TryGetOwnedMemory(memory, out OwnedMemory <int> owner)); Assert.Null(owner); }
/// <summary> /// Creates an instance of <see cref="ReadOnlySequence{T}"/> from the <see cref="ReadOnlyMemory{T}"/>. /// Consumer is expected to manage lifetime of memory until <see cref="ReadOnlySequence{T}"/> is not used anymore. /// </summary> public ReadOnlySequence(ReadOnlyMemory <T> readOnlyMemory) { if (MemoryMarshal.TryGetOwnedMemory(readOnlyMemory, out OwnedMemory <T> ownedMemory, out int index, out int length)) { _sequenceStart = new SequencePosition(ownedMemory, ReadOnlySequence.OwnedMemoryToSequenceStart(index)); _sequenceEnd = new SequencePosition(ownedMemory, ReadOnlySequence.OwnedMemoryToSequenceEnd(length)); }
public static void TryGetOwnedMemoryFromDefaultMemory_IndexLength() { ReadOnlyMemory <int> memory = default; Assert.False(MemoryMarshal.TryGetOwnedMemory(memory, out OwnedMemory <int> owner, out int index, out int length)); Assert.Equal(0, index); Assert.Equal(0, length); Assert.Null(owner); }
public static void TryGetOwnedMemory() { int[] array = new int[10]; OwnedMemory <int> orignalOwner = new CustomMemoryForTest <int>(array); ReadOnlyMemory <int> memory = orignalOwner.Memory; Assert.True(MemoryMarshal.TryGetOwnedMemory(memory, out CustomMemoryForTest <int> customOwner)); Assert.Same(orignalOwner, customOwner); Assert.True(MemoryMarshal.TryGetOwnedMemory(memory, out OwnedMemory <int> owner)); Assert.Same(orignalOwner, owner); Assert.False(MemoryMarshal.TryGetOwnedMemory(memory, out OtherMemoryForTest <int> notOwner)); Assert.Null(notOwner); }