Skip to content

Commit 6c93600

Browse files
authored
Add implicit operator to Span<T> to allow conversion from array (#265)
1 parent bfcba6a commit 6c93600

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

Tests/NFUnitTestTypes/UnitTestsReadOnlySpanByte.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,16 @@ public void ConstructorsOfAllKindsTests()
365365

366366
Assert.AreEqual(span.Length, 8, $"ReadOnlySpan should have length of 8");
367367
Assert.IsFalse(span.IsEmpty, "ReadOnlySpan should NOT be IsEmpty");
368+
369+
// ReadOnlySpan from array initializer
370+
span = new byte[] { 0xAA, 0xBB, 0xCC, 0xDD };
371+
372+
Assert.AreEqual(span.Length, 4, "ReadOnlySpan from array initializer should have length of 4");
373+
Assert.IsFalse(span.IsEmpty, "ReadOnlySpan from array initializer should NOT be IsEmpty");
374+
Assert.AreEqual(span[0], (byte)0xAA, "First element should be 0xAA");
375+
Assert.AreEqual(span[1], (byte)0xBB, "Second element should be 0xBB");
376+
Assert.AreEqual(span[2], (byte)0xCC, "Third element should be 0xCC");
377+
Assert.AreEqual(span[3], (byte)0xDD, "Fourth element should be 0xDD");
368378
}
369379

370380
[TestMethod]

Tests/NFUnitTestTypes/UnitTestsSpanByte.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,17 @@ public void ConstructorsOfAllKindsTests()
362362

363363
Assert.AreEqual(span.Length, 8, $"SpanByte should have length of 8");
364364
Assert.IsFalse(span.IsEmpty, "SpanByte should NOT be IsEmpty");
365+
366+
// Span<byte> from array initializer
367+
span = new byte[] { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19 };
368+
369+
Assert.AreEqual(span.Length, 10, $"SpanByte should have length of 10");
370+
Assert.IsFalse(span.IsEmpty, "Span<byte> should NOT be IsEmpty");
371+
372+
Assert.AreEqual(span[0], (byte)0x10, "First element must be value 0x10");
373+
Assert.AreEqual(span[1], (byte)0x11, "Second element must be value 0x11");
374+
Assert.AreEqual(span[5], (byte)0x15, "Sixth element must be value 0x15");
375+
Assert.AreEqual(span[9], (byte)0x19, "Last element must be value 0x19");
365376
}
366377

367378
[TestMethod]

nanoFramework.CoreLibrary/System/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
[assembly: AssemblyProduct(".NET nanoFramework mscorlib")]
1111
[assembly: AssemblyCopyright("Copyright (c) .NET Foundation and Contributors")]
1212

13-
[assembly: AssemblyNativeVersion("100.22.0.2")]
13+
[assembly: AssemblyNativeVersion("100.22.0.3")]

nanoFramework.CoreLibrary/System/Span.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ public ref T this[int index]
164164
/// </remarks>
165165
public static bool operator !=(Span<T> left, Span<T> right) => !(left == right);
166166

167+
/// <summary>
168+
/// Defines an implicit conversion of an array to a <see cref="Span{T}"/>
169+
/// </summary>
170+
/// <param name="array">The array to convert to a <see cref="Span{T}"/>.</param>
171+
public static implicit operator Span<T>(T[]? array) => new Span<T>(array);
172+
167173
/// <summary>
168174
/// Returns an empty <see cref="Span{T}"/> object.
169175
/// </summary>

0 commit comments

Comments
 (0)