Show / Hide Table of Contents

Struct SpanWriter<T>

Represents simple memory writer backed by Span<T>.

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

The type of elements in the span.

Constructors

View Source

SpanWriter(Span<T>)

Initializes a new memory writer.

Declaration
public SpanWriter(Span<T> span)
Parameters
Type Name Description
Span<T> span

The span used to write elements.

Properties

View Source

Current

Gets the element at the current position in the underlying memory block.

Declaration
public readonly ref T Current { get; }
Property Value
Type Description
T
Exceptions
Type Condition
InvalidOperationException

The position of this writer is out of range.

View Source

FreeCapacity

Gets the available space in the underlying span.

Declaration
public readonly int FreeCapacity { get; }
Property Value
Type Description
int
View Source

RemainingSpan

Gets the remaining part of the span.

Declaration
public readonly Span<T> RemainingSpan { get; }
Property Value
Type Description
Span<T>
View Source

Span

Gets underlying span.

Declaration
public readonly Span<T> Span { get; }
Property Value
Type Description
Span<T>
View Source

WrittenCount

Gets the number of occupied elements in the underlying span.

Declaration
public int WrittenCount { readonly get; set; }
Property Value
Type Description
int
View Source

WrittenSpan

Gets the span overwritten elements.

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

The segment of underlying span containing written elements.

Methods

View Source

Add()

Adds single element and returns a reference to it.

Declaration
public ref T Add()
Returns
Type Description
T

The reference to the added element.

Exceptions
Type Condition
InternalBufferOverflowException

Remaining space in the underlying span is not enough to place the item.

View Source

Add(T)

Puts single element into the underlying span.

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

The item to place.

Exceptions
Type Condition
InternalBufferOverflowException

Remaining space in the underlying span is not enough to place the item.

View Source

Advance(int)

Advances the position of this writer.

Declaration
public void Advance(int count)
Parameters
Type Name Description
int count

The number of written elements.

Exceptions
Type Condition
ArgumentOutOfRangeException

count is greater than the available space in the rest of the memory block.

View Source

Reset()

Sets writer position to the first element.

Declaration
public void Reset()
View Source

Rewind(int)

Moves the writer back the specified number of items.

Declaration
public void Rewind(int count)
Parameters
Type Name Description
int count

The number of items.

Exceptions
Type Condition
ArgumentOutOfRangeException

count is less than zero or greater than WrittenCount.

View Source

Slide(int)

Obtains the portion of underlying span and marks it as written.

Declaration
public Span<T> Slide(int count)
Parameters
Type Name Description
int count

The size of the segment.

Returns
Type Description
Span<T>

The portion of the underlying span.

Exceptions
Type Condition
ArgumentOutOfRangeException

count is negative or greater than FreeCapacity.

View Source

SlideToEnd()

Obtains the tail of the remaining buffer and advances to its end.

Declaration
public Span<T> SlideToEnd()
Returns
Type Description
Span<T>

The tail of the remaining buffer.

View Source

ToString()

Gets the textual representation of the written content.

Declaration
public override readonly string ToString()
Returns
Type Description
string

The textual representation of the written content.

Overrides
ValueType.ToString()
View Source

TryAdd(T)

Puts single element into the underlying span.

Declaration
public bool TryAdd(T item)
Parameters
Type Name Description
T item

The item to place.

Returns
Type Description
bool

true if item has been placed successfully; false if remaining space in the underlying span is not enough to place the item.

View Source

TrySlide(int, out Span<T>)

Obtains the portion of underlying span and marks it as written.

Declaration
public bool TrySlide(int count, out Span<T> segment)
Parameters
Type Name Description
int count

The size of the segment.

Span<T> segment

The portion of the underlying span.

Returns
Type Description
bool

true if segment is obtained successfully; false if remaining space in the underlying span is not enough to place count elements.

Exceptions
Type Condition
ArgumentOutOfRangeException

count is negative.

View Source

TryWrite(scoped ReadOnlySpan<T>)

Copies the elements to the underlying span.

Declaration
public bool TryWrite(scoped ReadOnlySpan<T> input)
Parameters
Type Name Description
ReadOnlySpan<T> input

The span to copy from.

Returns
Type Description
bool

true if all elements are copied successfully; false if remaining space in the underlying span is not enough to place all elements from input.

View Source

TryWrite<TArg>(delegate*<TArg, Span<T>, out int, bool>, TArg)

Attempts to write a portion of data.

Declaration
[CLSCompliant(false)]
public bool TryWrite<TArg>(delegate*<TArg, Span<T>, out int, bool> action, TArg arg)
Parameters
Type Name Description
delegate*<TArg, Span<T>, out int, bool> action

The action responsible for writing elements.

TArg arg

The state to be passed to the action.

Returns
Type Description
bool

true if all elements are written successfully; otherwise, false.

Type Parameters
Name Description
TArg

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

Exceptions
Type Condition
ArgumentNullException

action is zero.

View Source

Write(scoped ReadOnlySpan<T>)

Copies the elements to the underlying span.

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

The span of elements to copy from.

Returns
Type Description
int

The number of written elements.

View Source

Write<TArg>(delegate*<TArg, Span<T>, void>, TArg, int)

Writes a portion of data.

Declaration
[CLSCompliant(false)]
public void Write<TArg>(delegate*<TArg, Span<T>, void> action, TArg arg, int count)
Parameters
Type Name Description
delegate*<TArg, Span<T>, void> action

The action responsible for writing elements.

TArg arg

The state to be passed to the action.

int count

The number of the elements to be written.

Type Parameters
Name Description
TArg

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

Exceptions
Type Condition
ArgumentNullException

action is zero.

ArgumentOutOfRangeException

count is negative or greater than FreeCapacity.

Operators

View Source

operator +=(scoped ReadOnlySpan<T>)

Copies the elements to the underlying span.

Declaration
public void operator +=(scoped ReadOnlySpan<T> input)
Parameters
Type Name Description
ReadOnlySpan<T> input

The span to copy from.

View Source

operator +=(T)

Puts single element into the underlying span.

Declaration
public void operator +=(T item)
Parameters
Type Name Description
T item

The item to place.

View Source

operator checked +=(scoped ReadOnlySpan<T>)

Copies the elements to the underlying span.

Declaration
public void operator checked +=(scoped ReadOnlySpan<T> input)
Parameters
Type Name Description
ReadOnlySpan<T> input

The span to copy from.

View Source

operator checked +=(T)

Puts single element into the underlying span.

Declaration
public void operator checked +=(T item)
Parameters
Type Name Description
T item

The item to place.

Exceptions
Type Condition
InternalBufferOverflowException

Remaining space in the underlying span is not enough to place the item.

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾