Table of Contents

Struct RawMessagePack

Namespace
Nerdbank.MessagePack
Assembly
Nerdbank.MessagePack.dll

Represents a sequence of raw msgpack bytes.

[MessagePackConverter(typeof(RawMessagePackConverter))]
public struct RawMessagePack : IEquatable<RawMessagePack>
Implements
Inherited Members

Remarks

This struct is useful as the type for some field or property that should be serialize or deserialized separately from its surrounding data. For example an RPC protocol may have an envelope around user data, such that the envelope and the user data should have distinct serialization rules. The envelope could use this RawMessagePack in order to facilitate this by allowing pre-serialization and deferred deserialization of user data.

The MessagePackConverter<T> for this type will always copy the memory from the buffers being read so that this struct has an independent lifetime.

Constructors

RawMessagePack(ReadOnlySequence<byte>)

Initializes a new instance of the RawMessagePack struct.

public RawMessagePack(ReadOnlySequence<byte> msgpack)

Parameters

msgpack ReadOnlySequence<byte>

The actual sequence of msgpack bytes.

Properties

IsOwned

Gets a value indicating whether the MsgPack is owned by this instance, and thus can be relied on to be immutable and not recycled by others.

public readonly bool IsOwned { get; }

Property Value

bool

MsgPack

Gets a sequence of raw MessagePack bytes.

public readonly ReadOnlySequence<byte> MsgPack { get; }

Property Value

ReadOnlySequence<byte>

Methods

Equals(RawMessagePack)

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

public readonly bool Equals(RawMessagePack other)

Parameters

other RawMessagePack

An object to compare with this object.

Returns

bool

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

ToOwned()

Produces a self-sustaining copy of this struct that will outlive whatever the original source buffer was from which this was created.

public RawMessagePack ToOwned()

Returns

RawMessagePack

A copy of the data that is guaranteed to be immutable. When IsOwned is already true, this will be returned instead of making a redundant copy.

Remarks

This is useful when its owner wants the data to live longer than the underlying buffers from which it was created.

This struct mutates itself as well to retain possession of the cloned data, so that multiple calls to this method do not result in redundant copies. This will only be effective when the caller stores the struct in a mutable location. If this struct is stored in a readonly field for instance, the local copy cannot be mutated, though the returned copy will actually be self-owned.

Operators

implicit operator ReadOnlySequence<byte>(RawMessagePack)

Implicitly converts a RawMessagePack to a ReadOnlySequence<T> of bytes.

public static implicit operator ReadOnlySequence<byte>(RawMessagePack msgpack)

Parameters

msgpack RawMessagePack

The RawMessagePack instance.

Returns

ReadOnlySequence<byte>

The ReadOnlySequence<T> of bytes contained in the RawMessagePack.

implicit operator RawMessagePack(ReadOnlySequence<byte>)

Implicitly converts a ReadOnlySequence<T> of bytes to a RawMessagePack.

public static implicit operator RawMessagePack(ReadOnlySequence<byte> msgpack)

Parameters

msgpack ReadOnlySequence<byte>

The sequence of MessagePack bytes.

Returns

RawMessagePack

A new instance of RawMessagePack containing the provided bytes.

implicit operator RawMessagePack(byte[])

Implicitly converts an array of bytes to a RawMessagePack.

public static implicit operator RawMessagePack(byte[] msgpack)

Parameters

msgpack byte[]

The memory containing MessagePack bytes.

Returns

RawMessagePack

A new instance of RawMessagePack containing the provided bytes.

implicit operator RawMessagePack(Memory<byte>)

Implicitly converts a Memory<T> of bytes to a RawMessagePack.

public static implicit operator RawMessagePack(Memory<byte> msgpack)

Parameters

msgpack Memory<byte>

The memory containing MessagePack bytes.

Returns

RawMessagePack

A new instance of RawMessagePack containing the provided bytes.

implicit operator RawMessagePack(ReadOnlyMemory<byte>)

Implicitly converts a ReadOnlyMemory<T> of bytes to a RawMessagePack.

public static implicit operator RawMessagePack(ReadOnlyMemory<byte> msgpack)

Parameters

msgpack ReadOnlyMemory<byte>

The memory containing MessagePack bytes.

Returns

RawMessagePack

A new instance of RawMessagePack containing the provided bytes.