combineCodec
Call Signature
combineCodec<
TFrom
,TTo
,TSize
>(encoder
,decoder
):FixedSizeCodec
<TFrom
,TTo
,TSize
>
Combines an Encoder
and a Decoder
into a Codec
.
That is, given a Encoder<TFrom>
and a Decoder<TTo>
, this function returns a Codec<TFrom, TTo>
.
This allows for modular composition by keeping encoding and decoding logic separate
while still offering a convenient way to bundle them into a single Codec
.
This is particularly useful for library maintainers who want to expose Encoders
,
Decoders
, and Codecs
separately, enabling tree-shaking of unused logic.
The provided Encoder
and Decoder
must be compatible in terms of:
- Fixed Size: If both are fixed-size, they must have the same
fixedSize
value. - Variable Size: If either has a
maxSize
attribute, it must match the other.
If these conditions are not met, a SolanaError will be thrown.
Type Parameters
Type Parameter | Description |
---|---|
TFrom | The type of the value to encode. |
TTo | The type of the decoded value. |
TSize extends number | The fixed size of the encoded value in bytes (for fixed-size codecs). |
Parameters
Parameter | Type | Description |
---|---|---|
encoder | FixedSizeEncoder <TFrom , TSize > | The Encoder to combine. |
decoder | FixedSizeDecoder <TTo , TSize > | The Decoder to combine. |
Returns
FixedSizeCodec
<TFrom
, TTo
, TSize
>
A Codec
that provides both encode
and decode
methods.
Throws
SOLANA_ERROR__CODECS__ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH
Thrown if the encoder and decoder have mismatched size types (fixed vs. variable).SOLANA_ERROR__CODECS__ENCODER_DECODER_FIXED_SIZE_MISMATCH
Thrown if both are fixed-size but have differentfixedSize
values.SOLANA_ERROR__CODECS__ENCODER_DECODER_MAX_SIZE_MISMATCH
Thrown if themaxSize
attributes do not match.
Examples
Creating a fixed-size Codec
from an encoder and a decoder.
Creating a variable-size Codec
from an encoder and a decoder.
Remarks
The recommended pattern for defining codecs in libraries is to expose separate functions for the encoder, decoder, and codec. This allows users to import only what they need, improving tree-shaking efficiency.
See
Call Signature
combineCodec<
TFrom
,TTo
>(encoder
,decoder
):VariableSizeCodec
<TFrom
,TTo
>
Combines an Encoder
and a Decoder
into a Codec
.
That is, given a Encoder<TFrom>
and a Decoder<TTo>
, this function returns a Codec<TFrom, TTo>
.
This allows for modular composition by keeping encoding and decoding logic separate
while still offering a convenient way to bundle them into a single Codec
.
This is particularly useful for library maintainers who want to expose Encoders
,
Decoders
, and Codecs
separately, enabling tree-shaking of unused logic.
The provided Encoder
and Decoder
must be compatible in terms of:
- Fixed Size: If both are fixed-size, they must have the same
fixedSize
value. - Variable Size: If either has a
maxSize
attribute, it must match the other.
If these conditions are not met, a SolanaError will be thrown.
Type Parameters
Type Parameter | Description |
---|---|
TFrom | The type of the value to encode. |
TTo | The type of the decoded value. |
Parameters
Parameter | Type | Description |
---|---|---|
encoder | VariableSizeEncoder <TFrom > | The Encoder to combine. |
decoder | VariableSizeDecoder <TTo > | The Decoder to combine. |
Returns
VariableSizeCodec
<TFrom
, TTo
>
A Codec
that provides both encode
and decode
methods.
Throws
SOLANA_ERROR__CODECS__ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH
Thrown if the encoder and decoder have mismatched size types (fixed vs. variable).SOLANA_ERROR__CODECS__ENCODER_DECODER_FIXED_SIZE_MISMATCH
Thrown if both are fixed-size but have differentfixedSize
values.SOLANA_ERROR__CODECS__ENCODER_DECODER_MAX_SIZE_MISMATCH
Thrown if themaxSize
attributes do not match.
Examples
Creating a fixed-size Codec
from an encoder and a decoder.
Creating a variable-size Codec
from an encoder and a decoder.
Remarks
The recommended pattern for defining codecs in libraries is to expose separate functions for the encoder, decoder, and codec. This allows users to import only what they need, improving tree-shaking efficiency.
See
Call Signature
combineCodec<
TFrom
,TTo
>(encoder
,decoder
):Codec
<TFrom
,TTo
>
Combines an Encoder
and a Decoder
into a Codec
.
That is, given a Encoder<TFrom>
and a Decoder<TTo>
, this function returns a Codec<TFrom, TTo>
.
This allows for modular composition by keeping encoding and decoding logic separate
while still offering a convenient way to bundle them into a single Codec
.
This is particularly useful for library maintainers who want to expose Encoders
,
Decoders
, and Codecs
separately, enabling tree-shaking of unused logic.
The provided Encoder
and Decoder
must be compatible in terms of:
- Fixed Size: If both are fixed-size, they must have the same
fixedSize
value. - Variable Size: If either has a
maxSize
attribute, it must match the other.
If these conditions are not met, a SolanaError will be thrown.
Type Parameters
Type Parameter | Description |
---|---|
TFrom | The type of the value to encode. |
TTo | The type of the decoded value. |
Parameters
Parameter | Type | Description |
---|---|---|
encoder | Encoder <TFrom > | The Encoder to combine. |
decoder | Decoder <TTo > | The Decoder to combine. |
Returns
Codec
<TFrom
, TTo
>
A Codec
that provides both encode
and decode
methods.
Throws
SOLANA_ERROR__CODECS__ENCODER_DECODER_SIZE_COMPATIBILITY_MISMATCH
Thrown if the encoder and decoder have mismatched size types (fixed vs. variable).SOLANA_ERROR__CODECS__ENCODER_DECODER_FIXED_SIZE_MISMATCH
Thrown if both are fixed-size but have differentfixedSize
values.SOLANA_ERROR__CODECS__ENCODER_DECODER_MAX_SIZE_MISMATCH
Thrown if themaxSize
attributes do not match.
Examples
Creating a fixed-size Codec
from an encoder and a decoder.
Creating a variable-size Codec
from an encoder and a decoder.
Remarks
The recommended pattern for defining codecs in libraries is to expose separate functions for the encoder, decoder, and codec. This allows users to import only what they need, improving tree-shaking efficiency.