Skip to content

[API Proposal] Create ReadOnlySpanMarshaller<T, TUnmanaged>.ManagedToUnmanagedOut marshaller #101136

Closed

Description

@jtschuster

Background and motivation

To address #96525, we can provide a marshaller for ReadOnlySpan<T> for ManagedToUnmanagedOut direction. We have marshallers for UnmanagedToManagedOut and ManagedToUnmanagedIn. This API wasn't created previously due to the assumption that the managed ReadOnlySpan<T> would need to be written to in generated marshaling code, but that isn't strictly necessary for stateful marshallers. This API would allow LibraryImport methods to return ReadOnlySpan<T> without creating a new marshaller or specifying the marshaller type in the method definition.

API Proposal

  [System.Runtime.InteropServices.Marshalling.ContiguousCollectionMarshallerAttribute]
  [System.Runtime.InteropServices.Marshalling.CustomMarshallerAttribute(typeof(System.ReadOnlySpan<>), System.Runtime.InteropServices.Marshalling.MarshalMode.ManagedToUnmanagedIn, typeof(System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller<,>.ManagedToUnmanagedIn))]
+ [System.Runtime.InteropServices.Marshalling.CustomMarshallerAttribute(typeof(System.ReadOnlySpan<>), System.Runtime.InteropServices.Marshalling.MarshalMode.ManagedToUnmanagedOut, typeof(System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller<,>.ManagedToUnmanagedOut))]
  [System.Runtime.InteropServices.Marshalling.CustomMarshallerAttribute(typeof(System.ReadOnlySpan<>), System.Runtime.InteropServices.Marshalling.MarshalMode.UnmanagedToManagedOut, typeof(System.Runtime.InteropServices.Marshalling.ReadOnlySpanMarshaller<,>.UnmanagedToManagedOut))]
  public static unsafe class ReadOnlySpanMarshaller<T, TUnmanagedElement> where TUnmanagedElement : unmanaged
  {
+     public struct ManagedToUnmanagedOut
+     {
+         public void FromUnmanaged(TUnmanagedElement* unmanaged) { }
+         public ReadOnlySpan<T> ToManaged() { }
+         public ReadOnlySpan<TUnmanagedElement> GetUnmanagedValuesSource(int numElements) { }
+         public Span<T> GetManagedValuesDestination(int numElements) { }
+         public void Free() { }
+     }
}

API Usage

The API would primarily be used in code generated by the LibraryImport generator to unmarshal a native array into a managed ReadOnlySpan<T> when a method returns a ReadOnlySpan<T> or has an out parameter of type ReadOnlySpan<T>.

[LibraryImport("DllName")]
[return: MarshalUsing(CountElementName = nameof(length))]
public static partial ReadOnlySpan<int> GetRange(int start, int end, out int length);

Alternative Designs

A workaround is to create a LibraryImport that returns another built-in collection type and create a wrapper that converts the returned collection into a ReadOnlySpan<T>, or use hand-written marshalling for the method.

Risk

Minimal. We already provide marshallers for ReadOnlySpan<T> to pass a parameter in, and we have marshallers for other collections to return a value from a native method, so this is not introducing a new concept.

Activity

  1. added
    api-suggestion
    on Apr 16, 2024
  2. self-assigned this
    on Apr 16, 2024
  3. added
    untriaged
    on Apr 16, 2024
  4. dotnet-policy-service commented on Apr 16, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/interop-contrib
    See info in area-owners.md if you want to be subscribed.

  5. removed
    untriaged
    on Apr 16, 2024
  6. added
    api-ready-for-review
    and removed
    api-suggestion
    on Apr 16, 2024
  7. added this to the 9.0.0 milestone on Apr 16, 2024
  8. added
    blocking
    on Jun 8, 2024
  9. terrajobst commented on Jun 11, 2024

    @terrajobst
    Contributor

    Video

    • Looks good as proposed
    namespace System.Runtime.InteropServices.Marshalling;
    
    [CustomMarshaller(typeof(System.ReadOnlySpan<>), MarshalMode.ManagedToUnmanagedOut, typeof(ReadOnlySpanMarshaller<,>.ManagedToUnmanagedOut))]
    public partial class ReadOnlySpanMarshaller<T, TUnmanagedElement> where TUnmanagedElement : unmanaged
    {
        public struct ManagedToUnmanagedOut
        {
            public void FromUnmanaged(TUnmanagedElement* unmanaged);
            public ReadOnlySpan<T> ToManaged();
            public ReadOnlySpan<TUnmanagedElement> GetUnmanagedValuesSource(int numElements);
            public Span<T> GetManagedValuesDestination(int numElements);
            public void Free();
        }
    }
  10. added
    api-approved
    and removed
    api-ready-for-review
    on Jun 11, 2024
  11. jtschuster commented on Jun 17, 2024

    @jtschuster
    MemberAuthor

    Closed via #101095

  12. locked and limited conversation to collaborators on Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Runtime.InteropServicesblocking

Type

No type

Projects

  • Status
    No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions