Skip to content

Commit 14783cd

Browse files
committed
Fix array span constructors
1 parent 32eafc1 commit 14783cd

File tree

1 file changed

+29
-32
lines changed
  • modules/mono/glue/GodotSharp/GodotSharp/Core

1 file changed

+29
-32
lines changed

modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Runtime.CompilerServices;
77
using Godot.NativeInterop;
88
using System.Diagnostics;
9+
using System.ComponentModel;
910

1011
#nullable enable
1112

@@ -83,96 +84,92 @@ public Array(Variant[] array)
8384
/// Constructs a new <see cref="Array"/> from the given span's elements.
8485
/// </summary>
8586
/// <exception cref="ArgumentNullException">
86-
/// The <paramref name="array"/> is <see langword="null"/>.
87+
/// The <paramref name="span"/> is <see langword="null"/>.
8788
/// </exception>
8889
/// <returns>A new Godot Array.</returns>
89-
public Array(Span<StringName> array)
90+
public Array(scoped ReadOnlySpan<StringName> span)
9091
{
91-
if (array == null)
92-
throw new ArgumentNullException(nameof(array));
93-
9492
NativeValue = (godot_array.movable)NativeFuncs.godotsharp_array_new();
9593
_weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
9694

97-
int length = array.Length;
95+
int length = span.Length;
9896

9997
Resize(length);
10098

10199
for (int i = 0; i < length; i++)
102-
this[i] = array[i];
100+
this[i] = span[i];
103101
}
104102

103+
/// <inheritdoc cref="Array(ReadOnlySpan{StringName})"/>
104+
[EditorBrowsable(EditorBrowsableState.Never)]
105+
public Array(scoped Span<StringName> span) : this((ReadOnlySpan<StringName>)span) { }
106+
105107
/// <summary>
106108
/// Constructs a new <see cref="Array"/> from the given span's elements.
107109
/// </summary>
108110
/// <exception cref="ArgumentNullException">
109-
/// The <paramref name="array"/> is <see langword="null"/>.
111+
/// The <paramref name="span"/> is <see langword="null"/>.
110112
/// </exception>
111113
/// <returns>A new Godot Array.</returns>
112-
public Array(Span<NodePath> array)
114+
public Array(scoped ReadOnlySpan<NodePath> span)
113115
{
114-
if (array == null)
115-
throw new ArgumentNullException(nameof(array));
116-
117116
NativeValue = (godot_array.movable)NativeFuncs.godotsharp_array_new();
118117
_weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
119118

120-
int length = array.Length;
119+
int length = span.Length;
121120

122121
Resize(length);
123122

124123
for (int i = 0; i < length; i++)
125-
this[i] = array[i];
124+
this[i] = span[i];
126125
}
127126

127+
/// <inheritdoc cref="Array(ReadOnlySpan{NodePath})"/>
128+
[EditorBrowsable(EditorBrowsableState.Never)]
129+
public Array(scoped Span<NodePath> span) : this((ReadOnlySpan<NodePath>)span) { }
130+
128131
/// <summary>
129132
/// Constructs a new <see cref="Array"/> from the given span's elements.
130133
/// </summary>
131134
/// <exception cref="ArgumentNullException">
132-
/// The <paramref name="array"/> is <see langword="null"/>.
135+
/// The <paramref name="span"/> is <see langword="null"/>.
133136
/// </exception>
134137
/// <returns>A new Godot Array.</returns>
135-
public Array(Span<Rid> array)
138+
public Array(scoped ReadOnlySpan<Rid> span)
136139
{
137-
if (array == null)
138-
throw new ArgumentNullException(nameof(array));
139-
140140
NativeValue = (godot_array.movable)NativeFuncs.godotsharp_array_new();
141141
_weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
142142

143-
int length = array.Length;
143+
int length = span.Length;
144144

145145
Resize(length);
146146

147147
for (int i = 0; i < length; i++)
148-
this[i] = array[i];
148+
this[i] = span[i];
149149
}
150150

151-
// We must use ReadOnlySpan instead of Span here as this can accept implicit conversions
152-
// from derived types (e.g.: Node[]). Implicit conversion from Derived[] to Base[] are
153-
// fine as long as the array is not mutated. However, Span does this type checking at
154-
// instantiation, so it's not possible to use it even when not mutating anything.
151+
/// <inheritdoc cref="Array(ReadOnlySpan{Rid})"/>
152+
[EditorBrowsable(EditorBrowsableState.Never)]
153+
public Array(scoped Span<Rid> span) : this((ReadOnlySpan<Rid>)span) { }
154+
155155
/// <summary>
156156
/// Constructs a new <see cref="Array"/> from the given ReadOnlySpan's elements.
157157
/// </summary>
158158
/// <exception cref="ArgumentNullException">
159-
/// The <paramref name="array"/> is <see langword="null"/>.
159+
/// The <paramref name="span"/> is <see langword="null"/>.
160160
/// </exception>
161161
/// <returns>A new Godot Array.</returns>
162-
public Array(ReadOnlySpan<GodotObject> array)
162+
public Array(scoped ReadOnlySpan<GodotObject> span)
163163
{
164-
if (array == null)
165-
throw new ArgumentNullException(nameof(array));
166-
167164
NativeValue = (godot_array.movable)NativeFuncs.godotsharp_array_new();
168165
_weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this);
169166

170-
int length = array.Length;
167+
int length = span.Length;
171168

172169
Resize(length);
173170

174171
for (int i = 0; i < length; i++)
175-
this[i] = array[i];
172+
this[i] = span[i];
176173
}
177174

178175
private Array(godot_array nativeValueToOwn)

0 commit comments

Comments
 (0)