Struct PooledSerializationResult
- Namespace
- Excalibur.Dispatch.Serialization
- Assembly
- Excalibur.Dispatch.dll
Represents a serialization result backed by a pooled buffer with shared disposal ownership.
public readonly struct PooledSerializationResult : IDisposable, IEquatable<PooledSerializationResult>
- Implements
- Inherited Members
- Extension Methods
Remarks
Value-Type Disposal Warning: This is a readonly struct implementing
IDisposable. Value-type semantics apply:
- Copying this struct creates a shallow copy sharing the same underlying buffer.
- Disposing any copy returns the buffer to the pool and ends the lifetime of every copy. The return happens exactly once however many copies are disposed, and from however many threads at once.
- Once any copy has been disposed, reading WrittenMemory, WrittenSpan or calling CopyTo(IBufferWriter<byte>) on any copy throws ObjectDisposedException. The data is never silently wrong: a read that comes too late fails rather than returning another renter's bytes. Length keeps reporting the length that was written and does not throw.
Best Practice: Use with using statement and avoid copying:
using var result = serializer.SerializeToPooledBuffer(message);
await stream.WriteAsync(result.WrittenMemory);
Properties
Length
Gets the number of bytes written.
public int Length { get; }
Property Value
WrittenMemory
Gets the written memory containing the serialized data.
public ReadOnlyMemory<byte> WrittenMemory { get; }
Property Value
- ReadOnlyMemory<byte>
The written memory containing the serialized data.
Exceptions
- ObjectDisposedException
Thrown when this result, or any copy of it, has already been disposed.
WrittenSpan
Gets the written span containing the serialized data.
public ReadOnlySpan<byte> WrittenSpan { get; }
Property Value
- ReadOnlySpan<byte>
The written span containing the serialized data.
Exceptions
- ObjectDisposedException
Thrown when this result, or any copy of it, has already been disposed.
Methods
CopyTo(IBufferWriter<byte>)
Copies the serialized data to the specified buffer writer.
public void CopyTo(IBufferWriter<byte> writer)
Parameters
writerIBufferWriter<byte>The writer to copy the serialized data into.
Exceptions
- ArgumentNullException
Thrown when
writeris null.- ObjectDisposedException
Thrown when this result, or any copy of it, has already been disposed.
Dispose()
Returns the pooled buffer to the pool. Safe to call on more than one copy, and from more than one thread: the buffer is returned exactly once.
public void Dispose()
Equals(PooledSerializationResult)
Determines whether the specified pooled serialization result is equal to the current pooled serialization result.
public bool Equals(PooledSerializationResult other)
Parameters
otherPooledSerializationResultThe pooled serialization result to compare with the current pooled serialization result.
Returns
- bool
true if the specified pooled serialization result is equal to the current pooled serialization result; otherwise, false.
Equals(object?)
Determines whether the specified object is equal to the current pooled serialization result.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current pooled serialization result.
Returns
- bool
true if the specified object is equal to the current pooled serialization result; otherwise, false.
GetHashCode()
Returns the hash code for this pooled serialization result.
public override int GetHashCode()
Returns
- int
A hash code for the current pooled serialization result.
Operators
operator ==(PooledSerializationResult, PooledSerializationResult)
Determines whether two pooled serialization results are equal.
public static bool operator ==(PooledSerializationResult left, PooledSerializationResult right)
Parameters
leftPooledSerializationResultThe first pooled serialization result to compare.
rightPooledSerializationResultThe second pooled serialization result to compare.
Returns
- bool
true if the pooled serialization results are equal; otherwise, false.
operator !=(PooledSerializationResult, PooledSerializationResult)
Determines whether two pooled serialization results are not equal.
public static bool operator !=(PooledSerializationResult left, PooledSerializationResult right)
Parameters
leftPooledSerializationResultThe first pooled serialization result to compare.
rightPooledSerializationResultThe second pooled serialization result to compare.
Returns
- bool
true if the pooled serialization results are not equal; otherwise, false.