瀏覽代碼

Merge pull request #105950 from Joy-less/Fix-array-span-constructors

Fix array span constructors in C#
Thaddeus Crews 4 周之前
父節點
當前提交
8b4b93a82e
共有 1 個文件被更改,包括 29 次插入32 次删除
  1. 29 32
      modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs

+ 29 - 32
modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs

@@ -6,6 +6,7 @@ using System.Linq;
 using System.Runtime.CompilerServices;
 using Godot.NativeInterop;
 using System.Diagnostics;
+using System.ComponentModel;
 
 #nullable enable
 
@@ -83,96 +84,92 @@ namespace Godot.Collections
         /// Constructs a new <see cref="Array"/> from the given span's elements.
         /// </summary>
         /// <exception cref="ArgumentNullException">
-        /// The <paramref name="array"/> is <see langword="null"/>.
+        /// The <paramref name="span"/> is <see langword="null"/>.
         /// </exception>
         /// <returns>A new Godot Array.</returns>
-        public Array(Span<StringName> array)
+        public Array(scoped ReadOnlySpan<StringName> span)
         {
-            if (array == null)
-                throw new ArgumentNullException(nameof(array));
-
             NativeValue = (godot_array.movable)NativeFuncs.godotsharp_array_new();
             _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
 
-            int length = array.Length;
+            int length = span.Length;
 
             Resize(length);
 
             for (int i = 0; i < length; i++)
-                this[i] = array[i];
+                this[i] = span[i];
         }
 
+        /// <inheritdoc cref="Array(ReadOnlySpan{StringName})"/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Array(scoped Span<StringName> span) : this((ReadOnlySpan<StringName>)span) { }
+
         /// <summary>
         /// Constructs a new <see cref="Array"/> from the given span's elements.
         /// </summary>
         /// <exception cref="ArgumentNullException">
-        /// The <paramref name="array"/> is <see langword="null"/>.
+        /// The <paramref name="span"/> is <see langword="null"/>.
         /// </exception>
         /// <returns>A new Godot Array.</returns>
-        public Array(Span<NodePath> array)
+        public Array(scoped ReadOnlySpan<NodePath> span)
         {
-            if (array == null)
-                throw new ArgumentNullException(nameof(array));
-
             NativeValue = (godot_array.movable)NativeFuncs.godotsharp_array_new();
             _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
 
-            int length = array.Length;
+            int length = span.Length;
 
             Resize(length);
 
             for (int i = 0; i < length; i++)
-                this[i] = array[i];
+                this[i] = span[i];
         }
 
+        /// <inheritdoc cref="Array(ReadOnlySpan{NodePath})"/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Array(scoped Span<NodePath> span) : this((ReadOnlySpan<NodePath>)span) { }
+
         /// <summary>
         /// Constructs a new <see cref="Array"/> from the given span's elements.
         /// </summary>
         /// <exception cref="ArgumentNullException">
-        /// The <paramref name="array"/> is <see langword="null"/>.
+        /// The <paramref name="span"/> is <see langword="null"/>.
         /// </exception>
         /// <returns>A new Godot Array.</returns>
-        public Array(Span<Rid> array)
+        public Array(scoped ReadOnlySpan<Rid> span)
         {
-            if (array == null)
-                throw new ArgumentNullException(nameof(array));
-
             NativeValue = (godot_array.movable)NativeFuncs.godotsharp_array_new();
             _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
 
-            int length = array.Length;
+            int length = span.Length;
 
             Resize(length);
 
             for (int i = 0; i < length; i++)
-                this[i] = array[i];
+                this[i] = span[i];
         }
 
-        // We must use ReadOnlySpan instead of Span here as this can accept implicit conversions
-        // from derived types (e.g.: Node[]). Implicit conversion from Derived[] to Base[] are
-        // fine as long as the array is not mutated. However, Span does this type checking at
-        // instantiation, so it's not possible to use it even when not mutating anything.
+        /// <inheritdoc cref="Array(ReadOnlySpan{Rid})"/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Array(scoped Span<Rid> span) : this((ReadOnlySpan<Rid>)span) { }
+
         /// <summary>
         /// Constructs a new <see cref="Array"/> from the given ReadOnlySpan's elements.
         /// </summary>
         /// <exception cref="ArgumentNullException">
-        /// The <paramref name="array"/> is <see langword="null"/>.
+        /// The <paramref name="span"/> is <see langword="null"/>.
         /// </exception>
         /// <returns>A new Godot Array.</returns>
-        public Array(ReadOnlySpan<GodotObject> array)
+        public Array(scoped ReadOnlySpan<GodotObject> span)
         {
-            if (array == null)
-                throw new ArgumentNullException(nameof(array));
-
             NativeValue = (godot_array.movable)NativeFuncs.godotsharp_array_new();
             _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
 
-            int length = array.Length;
+            int length = span.Length;
 
             Resize(length);
 
             for (int i = 0; i < length; i++)
-                this[i] = array[i];
+                this[i] = span[i];
         }
 
         private Array(godot_array nativeValueToOwn)