Skip to content

Add ArrayPoolBufferWriter<T>.DangerousGetArray() API #615

@Sergio0694

Description

@Sergio0694

Overview

Related to #614. The ArrayPoolBufferWriter<T> lacks the DangerousGetArray() API which MemoryOwner<T> and SpanOwner<T> have. We should add it there too to make it easier and clearer how to get the underlying array from a writer.

API breakdown

namespace CommunityToolkit.HighPerformance.Buffers;

public sealed class ArrayPoolBufferWriter<T> : IBuffer<T>, IMemoryOwner<T>
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public ArraySegment<T> DangerousGetArray();
}

Usage example

ArraySegment<byte> segment = bufferWriter.DangerousGetArray();

stream.Write(segment.Array!, segment.Offset, segment.Count);

Breaking change?

No

Alternatives

Use MemoryMarshal.TryGetArray. That's less clear and less discoverable, so not ideal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request 📬A request for new changes to improve functionalityhigh-performance 🚂Issues/PRs for the HighPerformance package

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions