createEncoder
Call Signature
createEncoder<
TFrom
,TSize
>(encoder
):FixedSizeEncoder
<TFrom
,TSize
>
Creates an Encoder
by filling in the missing encode
function using the provided write
function and
either the fixedSize
property (/api/functions/for FixedSizeEncoders) or
the getSizeFromValue
function (/api/functions/for VariableSizeEncoders).
Instead of manually implementing encode
, this utility leverages the existing write
function
and the size helpers to generate a complete encoder. The provided encode
method will allocate
a new Uint8Array
of the correct size and use write
to populate it.
Type Parameters
Type Parameter | Description |
---|---|
TFrom | The type of the value to encode. |
TSize extends number | The fixed size of the encoded value in bytes (for fixed-size encoders). |
Parameters
Parameter | Type | Description |
---|---|---|
encoder | Omit <FixedSizeEncoder <TFrom , TSize >, "encode" > | An encoder object that implements write , but not encode . - If the encoder has a fixedSize property, it is treated as a FixedSizeEncoder. - Otherwise, it is treated as a VariableSizeEncoder. |
Returns
FixedSizeEncoder
<TFrom
, TSize
>
A fully functional Encoder
with both write
and encode
methods.
Examples
Creating a custom fixed-size encoder.
Creating a custom variable-size encoder:
Remarks
Note that, while createEncoder
is useful for defining more complex encoders, it is more common to compose
encoders together using the various helpers and primitives of the @solana/codecs
package.
Here are some alternative examples using codec primitives instead of createEncoder
.
See
- Encoder
- FixedSizeEncoder
- VariableSizeEncoder
- getStructEncoder
- getU32Encoder
- getUtf8Encoder
- addEncoderSizePrefix
Call Signature
createEncoder<
TFrom
>(encoder
):VariableSizeEncoder
<TFrom
>
Creates an Encoder
by filling in the missing encode
function using the provided write
function and
either the fixedSize
property (/api/functions/for FixedSizeEncoders) or
the getSizeFromValue
function (/api/functions/for VariableSizeEncoders).
Instead of manually implementing encode
, this utility leverages the existing write
function
and the size helpers to generate a complete encoder. The provided encode
method will allocate
a new Uint8Array
of the correct size and use write
to populate it.
Type Parameters
Type Parameter | Description |
---|---|
TFrom | The type of the value to encode. |
Parameters
Parameter | Type | Description |
---|---|---|
encoder | Omit <VariableSizeEncoder <TFrom >, "encode" > | An encoder object that implements write , but not encode . - If the encoder has a fixedSize property, it is treated as a FixedSizeEncoder. - Otherwise, it is treated as a VariableSizeEncoder. |
Returns
VariableSizeEncoder
<TFrom
>
A fully functional Encoder
with both write
and encode
methods.
Examples
Creating a custom fixed-size encoder.
Creating a custom variable-size encoder:
Remarks
Note that, while createEncoder
is useful for defining more complex encoders, it is more common to compose
encoders together using the various helpers and primitives of the @solana/codecs
package.
Here are some alternative examples using codec primitives instead of createEncoder
.
See
- Encoder
- FixedSizeEncoder
- VariableSizeEncoder
- getStructEncoder
- getU32Encoder
- getUtf8Encoder
- addEncoderSizePrefix
Call Signature
createEncoder<
TFrom
>(encoder
):Encoder
<TFrom
>
Creates an Encoder
by filling in the missing encode
function using the provided write
function and
either the fixedSize
property (/api/functions/for FixedSizeEncoders) or
the getSizeFromValue
function (/api/functions/for VariableSizeEncoders).
Instead of manually implementing encode
, this utility leverages the existing write
function
and the size helpers to generate a complete encoder. The provided encode
method will allocate
a new Uint8Array
of the correct size and use write
to populate it.
Type Parameters
Type Parameter | Description |
---|---|
TFrom | The type of the value to encode. |
Parameters
Parameter | Type | Description |
---|---|---|
encoder | Omit <FixedSizeEncoder <TFrom , number >, "encode" > | Omit <VariableSizeEncoder <TFrom >, "encode" > | An encoder object that implements write , but not encode . - If the encoder has a fixedSize property, it is treated as a FixedSizeEncoder. - Otherwise, it is treated as a VariableSizeEncoder. |
Returns
Encoder
<TFrom
>
A fully functional Encoder
with both write
and encode
methods.
Examples
Creating a custom fixed-size encoder.
Creating a custom variable-size encoder:
Remarks
Note that, while createEncoder
is useful for defining more complex encoders, it is more common to compose
encoders together using the various helpers and primitives of the @solana/codecs
package.
Here are some alternative examples using codec primitives instead of createEncoder
.