Skip to content

[API Proposal]: Add read/write span methods to UnmanagedMemoryAccessor  #107148

@masonwheeler

Description

@masonwheeler

Background and motivation

#27831 added ReadSpan<T> and WriteSpan<T> methods to SafeBuffer, with the explicit intent of surfacing them in UnmanagedMemoryAccessor in a later PR:

The motivation for this is for a later API proposal to add the same methods to UnmanagedMemoryAccessor which will work for memory mapped views.

This was merged 4 years ago, but the later PR appears to have never happened.

API Proposal

Add the following methods to UnmanagedMemoryAccessor:

+        public void ReadSpan<T>(ulong byteOffset, Span<T> buffer) where T : struct
+        public void WriteSpan<T>(ulong byteOffset, ReadOnlySpan<T> data) where T : struct

Their implementations would be similar to the existing ReadArray<T> and WriteArray<T> methods, doing verification and then calling the corresponding methods on _buffer.

API Usage

string text = "Hello, World!";
myMemoryMappedViewAccessor.WriteSpan(MY_OFFSET, text.AsSpan());

Alternative Designs

No response

Risks

None that I can see; the underlying APIs were developed specifically for this purpose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Memoryneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions