From d7faa4092ece590d688ec38f74d959b3c9fab0fb Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 24 Sep 2019 21:32:09 +0100 Subject: [PATCH 1/3] Add CollectionsMarshal --- .../System.Private.CoreLib.Shared.projitems | 1 + .../shared/System/Collections/Generic/List.cs | 2 ++ .../InteropServices/CollectionsMarshal.cs | 25 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 90d68d4156f2..5e8a46b1375e 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -644,6 +644,7 @@ + diff --git a/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs b/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs index 62993e71e555..25506e189d96 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs @@ -165,6 +165,8 @@ public T this[int index] } } + internal Span AsSpan() => new Span(_items, 0, _size); + private static bool IsCompatibleObject(object? value) { // Non-null values are fine. Only accept nulls if T is a class or Nullable. diff --git a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs new file mode 100644 index 000000000000..29ffe6d86ee5 --- /dev/null +++ b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; + +namespace System.Runtime.InteropServices +{ + /// + /// An unsafe class that provides a set of methods to access the underlying data representations of collections. + /// + public static class CollectionsMarshal + { + /// + /// Get a view over a 's data. + /// Items should not be added or removed from the while the is in use. + /// + public static Span AsSpan(List list) => list.AsSpan(); + /// + /// Get a view over a 's data. + /// Items should not be added or removed from the while the is in use. + /// + public static ReadOnlySpan AsReadOnlySpan(List list) => list.AsSpan(); + } +} From 143c599a3a96dd34ea57c850a8ed3947d782a8fb Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 24 Sep 2019 22:48:54 +0100 Subject: [PATCH 2/3] Feedback --- .../shared/System/Collections/Generic/List.cs | 6 ++---- .../System/Runtime/InteropServices/CollectionsMarshal.cs | 7 +++++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs b/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs index 25506e189d96..ce372c255676 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs @@ -23,8 +23,8 @@ public class List : IList, IList, IReadOnlyList { private const int DefaultCapacity = 4; - private T[] _items; // Do not rename (binary serialization) - private int _size; // Do not rename (binary serialization) + internal T[] _items; // Do not rename (binary serialization) + internal int _size; // Do not rename (binary serialization) private int _version; // Do not rename (binary serialization) #pragma warning disable CA1825 // avoid the extra generic instantiation for Array.Empty() @@ -165,8 +165,6 @@ public T this[int index] } } - internal Span AsSpan() => new Span(_items, 0, _size); - private static bool IsCompatibleObject(object? value) { // Non-null values are fine. Only accept nulls if T is a class or Nullable. diff --git a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs index 29ffe6d86ee5..0cd06ebd93fb 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs @@ -15,11 +15,14 @@ public static class CollectionsMarshal /// Get a view over a 's data. /// Items should not be added or removed from the while the is in use. /// - public static Span AsSpan(List list) => list.AsSpan(); + public static Span AsSpan(List list) + => new Span(list._items, 0, list._size); + /// /// Get a view over a 's data. /// Items should not be added or removed from the while the is in use. /// - public static ReadOnlySpan AsReadOnlySpan(List list) => list.AsSpan(); + public static ReadOnlySpan AsReadOnlySpan(List list) + => new ReadOnlySpan(list._items, 0, list._size); } } From fa9345ad93da2da3c4fa85f0b4c80a5ff579cb2f Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 24 Sep 2019 23:13:31 +0100 Subject: [PATCH 3/3] Feedback --- .../System/Runtime/InteropServices/CollectionsMarshal.cs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs index 0cd06ebd93fb..fe9df260ac6b 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs @@ -17,12 +17,5 @@ public static class CollectionsMarshal /// public static Span AsSpan(List list) => new Span(list._items, 0, list._size); - - /// - /// Get a view over a 's data. - /// Items should not be added or removed from the while the is in use. - /// - public static ReadOnlySpan AsReadOnlySpan(List list) - => new ReadOnlySpan(list._items, 0, list._size); } }