createCodec
Call Signature
Creates a Codec by filling in the missing encode and decode functions using the provided write and read functions.
This utility combines the behavior of createEncoder and createDecoder to produce a fully functional Codec.
The encode method is derived from the write function, while the decode method is derived from the read function.
If the fixedSize property is provided, a FixedSizeCodec will be created, otherwise
a VariableSizeCodec will be created.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TFrom | - | The type of the value to encode. |
TTo | TFrom | The type of the decoded value. |
TSize extends number | number | The fixed size of the encoded value in bytes (for fixed-size codecs). |
Parameters
| Parameter | Type | Description |
|---|---|---|
codec | Omit<FixedSizeCodec<TFrom, TTo, TSize>, "decode" | "encode"> | A codec object that implements write and read, but not encode or decode. - If the codec has a fixedSize property, it is treated as a FixedSizeCodec. - Otherwise, it is treated as a VariableSizeCodec. |
Returns
FixedSizeCodec<TFrom, TTo, TSize>
A fully functional Codec with write, read, encode, and decode methods.
Examples
Creating a custom fixed-size codec.
Creating a custom variable-size codec:
Remarks
This function effectively combines the behavior of createEncoder and createDecoder. If you only need to encode or decode (but not both), consider using those functions instead.
Here are some alternative examples using codec primitives instead of createCodec.
See
- Codec
- FixedSizeCodec
- VariableSizeCodec
- createEncoder
- createDecoder
- getStructCodec
- getU32Codec
- getUtf8Codec
- addCodecSizePrefix
Call Signature
Creates a Codec by filling in the missing encode and decode functions using the provided write and read functions.
This utility combines the behavior of createEncoder and createDecoder to produce a fully functional Codec.
The encode method is derived from the write function, while the decode method is derived from the read function.
If the fixedSize property is provided, a FixedSizeCodec will be created, otherwise
a VariableSizeCodec will be created.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TFrom | - | The type of the value to encode. |
TTo | TFrom | The type of the decoded value. |
Parameters
| Parameter | Type | Description |
|---|---|---|
codec | Omit<VariableSizeCodec<TFrom, TTo>, "decode" | "encode"> | A codec object that implements write and read, but not encode or decode. - If the codec has a fixedSize property, it is treated as a FixedSizeCodec. - Otherwise, it is treated as a VariableSizeCodec. |
Returns
VariableSizeCodec<TFrom, TTo>
A fully functional Codec with write, read, encode, and decode methods.
Examples
Creating a custom fixed-size codec.
Creating a custom variable-size codec:
Remarks
This function effectively combines the behavior of createEncoder and createDecoder. If you only need to encode or decode (but not both), consider using those functions instead.
Here are some alternative examples using codec primitives instead of createCodec.
See
- Codec
- FixedSizeCodec
- VariableSizeCodec
- createEncoder
- createDecoder
- getStructCodec
- getU32Codec
- getUtf8Codec
- addCodecSizePrefix
Call Signature
Creates a Codec by filling in the missing encode and decode functions using the provided write and read functions.
This utility combines the behavior of createEncoder and createDecoder to produce a fully functional Codec.
The encode method is derived from the write function, while the decode method is derived from the read function.
If the fixedSize property is provided, a FixedSizeCodec will be created, otherwise
a VariableSizeCodec will be created.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
TFrom | - | The type of the value to encode. |
TTo | TFrom | The type of the decoded value. |
Parameters
| Parameter | Type | Description |
|---|---|---|
codec | | Omit<FixedSizeCodec<TFrom, TTo, number>, "encode" | "decode"> | Omit<VariableSizeCodec<TFrom, TTo>, "encode" | "decode"> | A codec object that implements write and read, but not encode or decode. - If the codec has a fixedSize property, it is treated as a FixedSizeCodec. - Otherwise, it is treated as a VariableSizeCodec. |
Returns
Codec<TFrom, TTo>
A fully functional Codec with write, read, encode, and decode methods.
Examples
Creating a custom fixed-size codec.
Creating a custom variable-size codec:
Remarks
This function effectively combines the behavior of createEncoder and createDecoder. If you only need to encode or decode (but not both), consider using those functions instead.
Here are some alternative examples using codec primitives instead of createCodec.