Show / Hide Table of Contents

Struct BufferWriterSlim<T>

Represents stack-allocated buffer builder.

Inherited Members
ValueType.Equals(Object)
ValueType.GetHashCode()
ValueType.ToString()
Object.Equals(Object, Object)
Object.GetType()
Object.ReferenceEquals(Object, Object)
Namespace: DotNext.Buffers
Assembly: DotNext.dll
Syntax
public struct BufferWriterSlim<T>
Type Parameters
Name Description
T

The type of the elements in the memory.

Remarks

This type is similar to PooledArrayBufferWriter<T> and PooledBufferWriter<T> classes but it tries to avoid on-heap allocation. Moreover, it can use pre-allocated stack memory as a initial buffer used for writing. If builder requires more space then pooled memory used.

Constructors

| Improve this Doc View Source

BufferWriterSlim(Span<T>, Boolean, MemoryAllocator<T>)

Initializes growable builder.

Declaration
public BufferWriterSlim(Span<T> buffer, bool copyOnOverflow, MemoryAllocator<T> allocator = null)
Parameters
Type Name Description
Span<T> buffer

Pre-allocated buffer used by this builder.

Boolean copyOnOverflow

true to copy pre-allocated buffer to pooled memory on overflow; false to keep head of the written content in the pre-allocated buffer.

MemoryAllocator<T> allocator

The memory allocator used to rent the memory blocks.

Remarks

The builder created with this constructor is growable. However, additional memory will not be requested using allocator while buffer space is sufficient. If allocator is null then Shared is used for memory pooling. WrittenSpan property is supported only if copyOnOverflow is true. Otherwise, it's not possible to represent written content as contiguous memory block.

Properties

| Improve this Doc View Source

Capacity

Gets the total amount of space within the underlying memory.

Declaration
public int Capacity { get; }
Property Value
Type Description
Int32
| Improve this Doc View Source

FreeCapacity

Gets the amount of space available that can still be written into without forcing the underlying buffer to grow.

Declaration
public int FreeCapacity { get; }
Property Value
Type Description
Int32
| Improve this Doc View Source

Item[Int32]

Gets the element at the specified zero-based index within this builder.

Declaration
public T this[int index] { get; }
Parameters
Type Name Description
Int32 index

he zero-based index of the element.

Property Value
Type Description
T

The element at the specified index.

Exceptions
Type Condition
ArgumentOutOfRangeException

index is less than zero or greater than or equal to WrittenCount.

| Improve this Doc View Source

WrittenCount

Gets the amount of data written to the underlying memory so far.

Declaration
public int WrittenCount { get; }
Property Value
Type Description
Int32
| Improve this Doc View Source

WrittenSpan

Gets span over constructed memory block.

Declaration
public ReadOnlySpan<T> WrittenSpan { get; }
Property Value
Type Description
ReadOnlySpan<T>

The constructed memory block.

Exceptions
Type Condition
NotSupportedException

If this builder was constructed using BufferWriterSlim(Span<T>, Boolean, MemoryAllocator<T>) constructor and copyOnOverflow parameter is false.

Methods

| Improve this Doc View Source

Add(T)

Adds single element to this builder.

Declaration
public void Add(T item)
Parameters
Type Name Description
T item

The item to be added.

Exceptions
Type Condition
InsufficientMemoryException

Pre-allocated initial buffer size is not enough to place item to it and this builder is not growable.

| Improve this Doc View Source

Clear(Boolean)

lears the data written to the underlying buffer.

Declaration
public void Clear(bool reuseBuffer)
Parameters
Type Name Description
Boolean reuseBuffer

true to reuse the internal buffer; false to destroy the internal buffer.

| Improve this Doc View Source

CopyTo(ref SpanWriter<T>)

Copies written content.

Declaration
public int CopyTo(ref SpanWriter<T> output)
Parameters
Type Name Description
SpanWriter<T> output

The memory writer.

Returns
Type Description
Int32

The actual number of copied elements.

| Improve this Doc View Source

CopyTo(IBufferWriter<T>)

Copies written content to the specified buffer writer.

Declaration
public void CopyTo(IBufferWriter<T> output)
Parameters
Type Name Description
IBufferWriter<T> output

The buffer writer.

| Improve this Doc View Source

CopyTo(Span<T>)

Copies written content to the specified span.

Declaration
public int CopyTo(Span<T> output)
Parameters
Type Name Description
Span<T> output

The span of elemenents to modify.

Returns
Type Description
Int32

The actual number of copied elements.

Remarks

The output span can be larger or smaller than WrittenCount.

| Improve this Doc View Source

CopyTo<TArg>(ReadOnlySpanAction<T, TArg>, TArg)

Transfers written content to the specified callback.

Declaration
public void CopyTo<TArg>(ReadOnlySpanAction<T, TArg> action, TArg arg)
Parameters
Type Name Description
ReadOnlySpanAction<T, TArg> action

The callback accepting written content. Can be called more than once.

TArg arg

The argument to be passed to the callback.

Type Parameters
Name Description
TArg

The type of the argument to be passed to the callback.

| Improve this Doc View Source

Dispose()

Releases internal buffer used by this builder.

Declaration
public void Dispose()
| Improve this Doc View Source

Write(ReadOnlySpan<T>)

Writes elements to this buffer.

Declaration
public void Write(ReadOnlySpan<T> input)
Parameters
Type Name Description
ReadOnlySpan<T> input

The span of elements to be written.

Exceptions
Type Condition
InsufficientMemoryException

Pre-allocated initial buffer size is not enough to place input elements to it and this builder is not growable.

OverflowException

The size of the internal buffer becomes greater than MaxValue.

See Also

PooledArrayBufferWriter<T>
PooledBufferWriter<T>
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX