RefTuple

Ouverte
#1,128 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
45/100
Type d'issue
Fonctionnalité
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
csharp

Piste de recherche

Aucun fichier d’implémentation ni test n’est nommé dans l’issue. Commencez par localiser l’API publique du toolkit et les projets de test qui ciblent .NET 9 et C# 13, puis comparez la signature demandée de RefTuple<T1,T2> aux conventions existantes pour les tuples et les ref-structs. Le travail est considéré comme terminé lorsque l’API prend en charge la construction, la déconstruction et les arguments de type ref-struct présentés sans breaking change, avec des tests couvrant cette utilisation.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

feature request :mailbox_with_mail:
Overview

Adding RefTuple<...> types would allow ref structs to be used in tuples.

API breakdown
public ref struct RefTuple<T1,T2>
    where T1 : allows ref struct
    where T2 : allows ref struct
{
    public RefTuple(T1 t1, T2 t2);

    public T1 Item1 { get; set; }

    public T2 Item2 { get; set; }

    public readonly void Deconstruct(out T1 item1, out T2 item2);
}
Usage example

This is an example using pattern matching to select a reference to a field, and a paired value. The ref is wrapped in a Ref<T>, which as a ref struct cannot be stored in a normal tuple or ValueTuple.

// Get reference to selected register argument
RefTuple<Ref<GPRegister>, RegisterSet> pair = target switch
{
    // General Purpose Registers
    Argument.RS => new(new(ref _rs), RegisterSet.GeneralPurpose),
    Argument.RT => new(new(ref _rt), RegisterSet.GeneralPurpose),
    Argument.RD => new(new(ref _rd), RegisterSet.GeneralPurpose),
    // Float Registers
    Argument.FS => new(new(ref _rs), RegisterSet.FloatingPoints),
    Argument.FT => new(new(ref _rt), RegisterSet.FloatingPoints),
    Argument.FD => new(new(ref _rd), RegisterSet.FloatingPoints),
    // RT Register for coprocessors
    Argument.RT_Numbered => new(new(ref _rt), RegisterSet.Numbered),
    // Invalid target type
    _ => throw new ArgumentOutOfRangeException($"Argument of type '{target}' attempted to parse as a register.")
};


(Ref<GPRegister> regRef, RegisterSet set) = pair;
ref GPRegister reg = ref regRef.Value;
Breaking change?

No

Alternatives

For the code above, here's what it looks like using a switch statement instead of a switch expression.


// Get reference to selected register argument
ref GPRegister reg = ref _rs;
RegisterSet set = RegisterSet.GeneralPurpose;
switch (target)
{
    // General Purpose Registers
    case Argument.RS:
        reg = ref _rs;
        break;
    case Argument.RT:
        reg = ref _rt;
        break;
    case Argument.RD:
        reg = ref _rd;
        break;
    // Float Registers
    case Argument.FS:
        reg = ref _rs;
        set = RegisterSet.FloatingPoints;
        break;
    case Argument.FT:
        reg = ref _rt;
        set = RegisterSet.FloatingPoints;
        break;
    case Argument.FD:
        reg = ref _rd;
        set = RegisterSet.FloatingPoints;
        break;
    // RT Register for coprocessors
    case Argument.RT_Numbered:
        reg = ref _rt;
        set = RegisterSet.Numbered;
        break;
    // Invalid target type
    default:
        return ThrowHelper.ThrowArgumentOutOfRangeException<bool>($"Argument of type '{target}' attempted to parse as a register.");
}

Perfectly legitimate code, but the pattern matching is much more convenient to read.

Additional context

This requires .NET 9 and C# version 13

Help us help you

Yes, I'd like to be assigned to work on this item

Langage dominant
C#
Étoiles
3.8k
Forks
400
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de CommunityToolkit/dotnet

Toutes les issues de CommunityToolkit/dotnet

Issues similaires

Plus d'issues C#

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.