Table of Contents

Struct SequenceLease<T>

Namespace
CryptoHives.Foundation.Memory.Buffers
Assembly
CryptoHives.Foundation.Memory.dll

A payload and the disposable that ends its life, carried together as a single value so the pair can travel through a pipeline without the consumer knowing what produced it.

public readonly struct SequenceLease<T> : ISequenceOwner<T>, IDisposable, IEquatable<SequenceLease<T>>

Type Parameters

T

The element type of the sequence.

Implements
Inherited Members

Remarks

This is a struct deliberately: it exists to move a payload across a scope boundary without paying for a handle to do it. Obtain one from LeaseSequence() or LeaseSequence(), where the producer itself rides along as the disposable — so disposing the lease returns the producer, which returns its buffers.

Nothing is transferred and nothing is allocated: the sequence is the one the producer already holds, and the producer stays alive for as long as the lease does. The trade is that a pooled producer stays out of circulation until the payload is finished with.

Use it in a limited scope, as with ObjectOwner<T>:

using SequenceLease<byte> payload = BuildPayload();
Send(payload.Sequence);

Do not copy it. Two copies disposed means the producer is disposed twice. Do not cast it to ISequenceOwner<T> or IDisposable unless a boxing allocation is acceptable — that is the one way to spend the allocation this type exists to avoid. Storing it in a Channel, a list or an async method's locals is fine; those hold the struct inline.

The default value is a valid empty lease: no payload, no owner, and disposing it does nothing.

Constructors

SequenceLease(ReadOnlySequence<T>, IDisposable?)

Initializes a new instance of the SequenceLease<T> struct.

public SequenceLease(ReadOnlySequence<T> sequence, IDisposable? owner)

Parameters

sequence ReadOnlySequence<T>

The payload.

owner IDisposable

The object whose disposal releases the payload, usually the producer that yielded sequence. May be null for a payload that owns nothing.

Properties

IsEmpty

Gets whether the owner holds no elements.

public bool IsEmpty { get; }

Property Value

bool

Length

Gets the total number of elements in Sequence.

public long Length { get; }

Property Value

long

Sequence

Gets the sequence spanning the owned buffers.

public ReadOnlySequence<T> Sequence { get; }

Property Value

ReadOnlySequence<T>

Remarks

Valid until this owner is disposed.

Methods

Dispose()

Disposes the owner, releasing the payload.

public void Dispose()

Remarks

Not idempotent in itself — it simply forwards to the owner, so whether a second call is safe is the owner's business. Both producers in this package tolerate it.

Equals(SequenceLease<T>)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(SequenceLease<T> other)

Parameters

other SequenceLease<T>

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

Operators

operator ==(SequenceLease<T>, SequenceLease<T>)

Determines whether two leases refer to the same payload and owner.

public static bool operator ==(SequenceLease<T> left, SequenceLease<T> right)

Parameters

left SequenceLease<T>

The first lease.

right SequenceLease<T>

The second lease.

Returns

bool

true if they are equal.

operator !=(SequenceLease<T>, SequenceLease<T>)

Determines whether two leases refer to different payloads or owners.

public static bool operator !=(SequenceLease<T> left, SequenceLease<T> right)

Parameters

left SequenceLease<T>

The first lease.

right SequenceLease<T>

The second lease.

Returns

bool

true if they are not equal.