/** * | Type | Header | Body | * |--------+--------------------------------------------------+-----------------------------------| * | array | | item-0 | item-1 | ... | item-N | * | struct | | field-0 | field-1 | ... | field-N | * | fixvec | items-count | item-0 | item-1 | ... | item-N | * | dynvec | full-size | offset-0 | offset-1 | ... | offset-N | item-0 | item-1 | ... | item-N | * | table | full-size | offset-0 | offset-1 | ... | offset-N | filed-0 | field-1 | ... | field-N | * | option | | item or none (zero bytes) | * | union | item-type-id | item | */ import { BytesCodec, Fixed, FixedBytesCodec, PackParam, UnpackResult } from "../base"; type NullableKeys> = { [K in keyof O]-?: [O[K] & (undefined | null)] extends [never] ? never : K; }[keyof O]; type NonNullableKeys> = { [K in keyof O]-?: [O[K] & (undefined | null)] extends [never] ? K : never; }[keyof O]; type PartialNullable> = Partial>> & Pick>; /** * A codec for struct and table of Molecule */ export type ObjectLayoutCodec> = BytesCodec; }>, PartialNullable<{ [key in keyof T]: PackParam; }>>; /** * A codec for option of Molecule */ export interface OptionLayoutCodec extends BytesCodec | undefined> { pack: (packable?: PackParam) => Uint8Array; } /** * A code for array and vector of Molecule */ export type ArrayLayoutCodec = BytesCodec>, Array>>; /** * A molecule codec for ` */ export type UnionLayoutCodec> = BytesCodec<{ [key in keyof T]: { type: key; value: UnpackResult; }; }[keyof T], { [key in keyof T]: { type: key; value: PackParam; }; }[keyof T]>; /** * The array is a fixed-size type: it has a fixed-size inner type and a fixed length. * The size of an array is the size of inner type times the length. * @param itemCodec the fixed-size array item codec * @param itemCount */ export declare function array(itemCodec: T, itemCount: number): ArrayLayoutCodec & Fixed; /** * Struct is a fixed-size type: all fields in struct are fixed-size and it has a fixed quantity of fields. * The size of a struct is the sum of all fields' size. * @param shape a object contains all fields' codec * @param fields the shape's keys. It provide an order for serialization/deserialization. */ export declare function struct>(shape: T, fields: (keyof T)[]): ObjectLayoutCodec & Fixed; /** * Vector with fixed size item codec * @param itemCodec fixed-size vector item codec */ export declare function fixvec(itemCodec: T): ArrayLayoutCodec; /** * Vector with dynamic size item codec * @param itemCodec the vector item codec. It can be fixed-size or dynamic-size. * For example, you can create a recursive vector with this. */ export declare function dynvec(itemCodec: T): ArrayLayoutCodec; /** * General vector codec, if `itemCodec` is fixed size type, it will create a fixvec codec, otherwise a dynvec codec will be created. * @param itemCodec */ export declare function vector(itemCodec: T): ArrayLayoutCodec; /** * Table is a dynamic-size type. It can be considered as a dynvec but the length is fixed. * @param shape The table shape, item codec can be dynamic size * @param fields the shape's keys. Also provide an order for pack/unpack. */ export declare function table>(shape: T, fields: (keyof T)[]): ObjectLayoutCodec; /** * Union is a dynamic-size type. * Serializing a union has two steps: * - Serialize an item type id in bytes as a 32 bit unsigned integer in little-endian. The item type id is the index of the inner items, and it's starting at 0. * - Serialize the inner item. * @param itemCodec the union item record * @param fields the union item keys, can be an array or an object with custom id * @example * // without custom id * union({ cafe: Uint8, bee: Uint8 }, ['cafe', 'bee']) * // with custom id * union({ cafe: Uint8, bee: Uint8 }, { cafe: 0xcafe, bee: 0xbee }) */ export declare function union>(itemCodec: T, fields: (keyof T)[] | Record): UnionLayoutCodec; /** * Option is a dynamic-size type. * Serializing an option depends on whether it is empty or not: * - if it's empty, there is zero bytes (the size is 0). * - if it's not empty, just serialize the inner item (the size is same as the inner item's size). * @param itemCodec */ export declare function option(itemCodec: T): OptionLayoutCodec; export {}; //# sourceMappingURL=layout.d.ts.map