Skip to content

Commit

Permalink
Use delegate instead of delegate pointer in variant generic conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
paulloz committed Jun 26, 2024
1 parent 128e473 commit 5761934
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
6 changes: 3 additions & 3 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,16 +1052,16 @@ public sealed class Array<[MustBeVariant] T> :
IEnumerable<T>,
IGenericGodotArray
{
private static godot_variant ToVariantFunc(in Array<T> godotArray) =>
private static godot_variant ToVariantFunc(scoped in Array<T> godotArray) =>
VariantUtils.CreateFromArray(godotArray);

private static Array<T> FromVariantFunc(in godot_variant variant) =>
VariantUtils.ConvertToArray<T>(variant);

static unsafe Array()
{
VariantUtils.GenericConversion<Array<T>>.ToVariantCb = &ToVariantFunc;
VariantUtils.GenericConversion<Array<T>>.FromVariantCb = &FromVariantFunc;
VariantUtils.GenericConversion<Array<T>>.ToVariantCb = ToVariantFunc;
VariantUtils.GenericConversion<Array<T>>.FromVariantCb = FromVariantFunc;
}

private readonly Array _underlyingArray;
Expand Down
6 changes: 3 additions & 3 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,16 @@ public class Dictionary<[MustBeVariant] TKey, [MustBeVariant] TValue> :
IReadOnlyDictionary<TKey, TValue>,
IGenericGodotDictionary
{
private static godot_variant ToVariantFunc(in Dictionary<TKey, TValue> godotDictionary) =>
private static godot_variant ToVariantFunc(scoped in Dictionary<TKey, TValue> godotDictionary) =>
VariantUtils.CreateFromDictionary(godotDictionary);

private static Dictionary<TKey, TValue> FromVariantFunc(in godot_variant variant) =>
VariantUtils.ConvertToDictionary<TKey, TValue>(variant);

static unsafe Dictionary()
{
VariantUtils.GenericConversion<Dictionary<TKey, TValue>>.ToVariantCb = &ToVariantFunc;
VariantUtils.GenericConversion<Dictionary<TKey, TValue>>.FromVariantCb = &FromVariantFunc;
VariantUtils.GenericConversion<Dictionary<TKey, TValue>>.ToVariantCb = ToVariantFunc;
VariantUtils.GenericConversion<Dictionary<TKey, TValue>>.FromVariantCb = FromVariantFunc;
}

private readonly Dictionary _underlyingDict;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ public partial class VariantUtils

internal static class GenericConversion<T>
{
internal delegate godot_variant ToVariantConverter(scoped in T from);
internal delegate T FromVariantConverter(in godot_variant from);

public static unsafe godot_variant ToVariant(scoped in T from) =>
#pragma warning disable CS9088 // the delegate pointer cannot be marked scoped, but it should be
ToVariantCb != null ? ToVariantCb(from) : throw UnsupportedType<T>();
#pragma warning restore CS9088
ToVariantCb != null ? ToVariantCb(from) : throw UnsupportedType<T>();

public static unsafe T FromVariant(in godot_variant variant) =>
FromVariantCb != null ? FromVariantCb(variant) : throw UnsupportedType<T>();

// ReSharper disable once StaticMemberInGenericType
internal static unsafe delegate*<in T, godot_variant> ToVariantCb;
internal static ToVariantConverter? ToVariantCb;

// ReSharper disable once StaticMemberInGenericType
internal static unsafe delegate*<in godot_variant, T> FromVariantCb;
internal static FromVariantConverter? FromVariantCb;

static GenericConversion()
{
Expand Down

0 comments on commit 5761934

Please sign in to comment.