microsoft / microsoft/CsWinRT

Proposal: Use `Span` for `WindowsRuntimeBufferMarshal.TryGetDataUnsafe`

Open
#2,416 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
665
Forks
134
Avg merge
1d 3h
Merged PRs (30d)
32

Description

Summary

Replace the following methods:

namespace WindowsRuntime.InteropServices;

public static class WindowsRuntimeBufferMarshal
{
-    public static bool TryGetDataUnsafe([NotNullWhen(true)] IMemoryBufferReference? buffer, out byte* data, out uint capacity);
+    public static bool TryGetDataUnsafe([NotNullWhen(true)] IMemoryBufferReference? buffer, out Span<byte> data);
-    public static bool TryGetDataUnsafe([NotNullWhen(true)] IBuffer? buffer, out byte* data);
+    public static bool TryGetDataUnsafe([NotNullWhen(true)] IBuffer? buffer, out Span<byte> data);
}
Rationale

With the theme of unsafe evolution, the .NET runtime team is reducing the usages of raw pointers and replacing with safe equivalents like Span<T>. Span<T> and Memory<T> can hold unmanaged memory, and users who need raw pointers can use fixed or System.Runtime.CompilerServices.Unsafe to get it back.

Important Notes

It's important to decide the safeness of span-returning method under the new rule (RequiresUnsafe). Since it requires manual KeepAlive for the object, it should be considered unsafe (similar to CollectionsMarshal.AsSpan(List<T>)). Since most things under xxMarshal would be unsafe, this shouldn't be a problem.

Alternatively, returning a IMemoryOwner<byte> can achieve completely safe lifetime for the caller, but with more overhead and unnatural usage patterns.

Open Questions

Should the IBuffer case use Capacity or Length? Capacity is natural for writing and Length is natural for reading, should it be split into two overloads returning Span and ReadOnlySpan respectively?

Should the name be change to drop the Unsafe suffix to match BCL naming convention? The term "marshal" already implies unsafeness.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing WindowsRuntime.InteropServices.WindowsRuntimeBufferMarshal.TryGetDataUnsafe for both IMemoryBufferReference and IBuffer overloads, then read the linked unsafe-evolution proposal. Resolve the Span lifetime and safety implications, the IBuffer Capacity-versus-Length choice, and whether the Unsafe suffix should remain; done means the API design questions have a decided direction.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.