diff --git a/packages/adb/src/commands/framebuffer.ts b/packages/adb/src/commands/framebuffer.ts index 7ed16235..64b4dd70 100644 --- a/packages/adb/src/commands/framebuffer.ts +++ b/packages/adb/src/commands/framebuffer.ts @@ -35,7 +35,7 @@ export const AdbFrameBufferV1 = .uint32('alpha_length') .uint8ClampedArray('data', { lengthField: 'size' }); -export type AdbFrameBufferV1 = typeof AdbFrameBufferV1['deserializedType']; +export type AdbFrameBufferV1 = typeof AdbFrameBufferV1['TDeserializeResult']; export const AdbFrameBufferV2 = new Struct({ littleEndian: true }) @@ -54,7 +54,7 @@ export const AdbFrameBufferV2 = .uint32('alpha_length') .uint8ClampedArray('data', { lengthField: 'size' }); -export type AdbFrameBufferV2 = typeof AdbFrameBufferV2['deserializedType']; +export type AdbFrameBufferV2 = typeof AdbFrameBufferV2['TDeserializeResult']; export type AdbFrameBuffer = AdbFrameBufferV1 | AdbFrameBufferV2; diff --git a/packages/adb/src/commands/sync/list.ts b/packages/adb/src/commands/sync/list.ts index 499488c8..53cb0f05 100644 --- a/packages/adb/src/commands/sync/list.ts +++ b/packages/adb/src/commands/sync/list.ts @@ -11,7 +11,7 @@ export const AdbSyncEntryResponse = .string('name', { lengthField: 'nameLength' }) .extra({ id: AdbSyncResponseId.Entry as const }); -export type AdbSyncEntryResponse = typeof AdbSyncEntryResponse['deserializedType']; +export type AdbSyncEntryResponse = typeof AdbSyncEntryResponse['TDeserializeResult']; const ResponseTypes = { [AdbSyncResponseId.Entry]: AdbSyncEntryResponse, diff --git a/packages/adb/src/commands/sync/stat.ts b/packages/adb/src/commands/sync/stat.ts index 3833060e..9fa2c48c 100644 --- a/packages/adb/src/commands/sync/stat.ts +++ b/packages/adb/src/commands/sync/stat.ts @@ -29,7 +29,7 @@ export const AdbSyncLstatResponse = } }); -export type AdbSyncLstatResponse = typeof AdbSyncLstatResponse['deserializedType']; +export type AdbSyncLstatResponse = typeof AdbSyncLstatResponse['TDeserializeResult']; export enum AdbSyncStatErrorCode { EACCES = 13, @@ -78,7 +78,7 @@ export const AdbSyncStatResponse = } }); -export type AdbSyncStatResponse = typeof AdbSyncStatResponse['deserializedType']; +export type AdbSyncStatResponse = typeof AdbSyncStatResponse['TDeserializeResult']; const StatResponseType = { [AdbSyncResponseId.Stat]: AdbSyncStatResponse, diff --git a/packages/adb/src/packet.ts b/packages/adb/src/packet.ts index 22b3223a..70750519 100644 --- a/packages/adb/src/packet.ts +++ b/packages/adb/src/packet.ts @@ -25,9 +25,9 @@ const AdbPacketStruct = .fields(AdbPacketHeader) .arrayBuffer('payload', { lengthField: 'payloadLength' }); -export type AdbPacket = typeof AdbPacketStruct['deserializedType']; +export type AdbPacket = typeof AdbPacketStruct['TDeserializeResult']; -export type AdbPacketInit = Omit; +export type AdbPacketInit = Omit; export namespace AdbPacket { export async function read(backend: AdbBackend): Promise { diff --git a/packages/demo/src/routes/scrcpy/server/client.ts b/packages/demo/src/routes/scrcpy/server/client.ts index 9a99fcfd..da4385a8 100644 --- a/packages/demo/src/routes/scrcpy/server/client.ts +++ b/packages/demo/src/routes/scrcpy/server/client.ts @@ -159,7 +159,7 @@ const VideoPacket = export const NoPts = BigInt(-1); -export type VideoPacket = typeof VideoPacket['deserializedType']; +export type VideoPacket = typeof VideoPacket['TDeserializeResult']; const ClipboardMessage = new Struct() diff --git a/packages/demo/src/routes/scrcpy/server/message.ts b/packages/demo/src/routes/scrcpy/server/message.ts index 04b34ada..2649433e 100644 --- a/packages/demo/src/routes/scrcpy/server/message.ts +++ b/packages/demo/src/routes/scrcpy/server/message.ts @@ -18,7 +18,7 @@ export const ScrcpySimpleControlMessage = new Struct() .uint8('type', placeholder()); -export type ScrcpySimpleControlMessage = typeof ScrcpySimpleControlMessage['initType']; +export type ScrcpySimpleControlMessage = typeof ScrcpySimpleControlMessage['TInit']; export enum AndroidMotionEventAction { Down, @@ -48,7 +48,7 @@ export const ScrcpyInjectTouchControlMessage = .uint16('pressure') .uint32('buttons'); -export type ScrcpyInjectTouchControlMessage = typeof ScrcpyInjectTouchControlMessage['initType']; +export type ScrcpyInjectTouchControlMessage = typeof ScrcpyInjectTouchControlMessage['TInit']; export const ScrcpyInjectTextControlMessage = new Struct() @@ -57,7 +57,7 @@ export const ScrcpyInjectTextControlMessage = .string('text', { lengthField: 'length' }); export type ScrcpyInjectTextControlMessage = - typeof ScrcpyInjectTextControlMessage['initType']; + typeof ScrcpyInjectTextControlMessage['TInit']; export enum AndroidKeyEventAction { Down = 0, @@ -106,7 +106,7 @@ export const ScrcpyInjectKeyCodeControlMessage = .uint32('metaState'); export type ScrcpyInjectKeyCodeControlMessage = - typeof ScrcpyInjectKeyCodeControlMessage['initType']; + typeof ScrcpyInjectKeyCodeControlMessage['TInit']; export type ScrcpyControlMessage = ScrcpySimpleControlMessage | diff --git a/packages/struct/README.md b/packages/struct/README.md index b3f980d4..e4cce16f 100644 --- a/packages/struct/README.md +++ b/packages/struct/README.md @@ -57,7 +57,7 @@ const buffer = MyStruct.serialize({ - [Custom field type](#custom-field-type) - [`Struct#field`](#structfield) - [`StructFieldDefinition`](#structfielddefinition) - - [`valueType`/`omitInitKeyType`](#valuetypeomitinitkeytype) + - [`TValue`/`TOmitInitKey`](#tvaluetomitinitkey) - [`getSize`](#getsize) - [`create`](#create) - [`deserialize`](#deserialize-1) @@ -570,8 +570,8 @@ field< name: TName, definition: TDefinition ): Struct< - TFields & Record, - TOmitInitKey | TDefinition['omitInitKeyType'], + TFields & Record, + TOmitInitKey | TDefinition['TOmitInitKey'], TExtra, TPostDeserialized >; @@ -615,7 +615,7 @@ A `StructFieldDefinition` describes type, size and runtime semantics of a field. It's an `abstract` class, means it lacks some method implementations, so it shouldn't be constructed. -#### `valueType`/`omitInitKeyType` +#### `TValue`/`TOmitInitKey` These two fields are used to provide type information to TypeScript. Their values will always be `undefined`, but having correct types is enough. You don't need to care about them. @@ -688,8 +688,8 @@ If this field's size can change based on some criteria, one must override `getSi #### `get`/`set` ```ts -get(): TDefinition['valueType']; -set(value: TDefinition['valueType']): void; +get(): TDefinition['TValue']; +set(value: TDefinition['TValue']): void; ``` Defines how to get or set this field's value. By default, it store its value in `value` field. diff --git a/packages/struct/src/basic/definition.ts b/packages/struct/src/basic/definition.ts index a29ec0bc..b782f7c5 100644 --- a/packages/struct/src/basic/definition.ts +++ b/packages/struct/src/basic/definition.ts @@ -23,20 +23,20 @@ export abstract class StructFieldDefinition< TValue = unknown, TOmitInitKey extends PropertyKey = never, > { + /** + * When `T` is a type initiated `StructFieldDefinition`, + * use `T['TValue']` to retrieve its `TValue` type parameter. + */ + public readonly TValue!: TValue; + + /** + * When `T` is a type initiated `StructFieldDefinition`, + * use `T['TOmitInitKey']` to retrieve its `TOmitInitKey` type parameter. + */ + public readonly TOmitInitKey!: TOmitInitKey; + public readonly options: TOptions; - /** - * When `T` is a type initiated `StructFieldDefinition`, - * use `T['valueType']` to retrieve its `TValue` type parameter. - */ - public readonly valueType!: TValue; - - /** - * When `T` is a type initiated `StructFieldDefinition`, - * use `T['omitInitKeyType']` to retrieve its `TOmitInitKey` type parameter. - */ - public readonly omitInitKeyType!: TOmitInitKey; - public constructor(options: TOptions) { this.options = options; } diff --git a/packages/struct/src/basic/field-value.ts b/packages/struct/src/basic/field-value.ts index 34c80266..f89c9f30 100644 --- a/packages/struct/src/basic/field-value.ts +++ b/packages/struct/src/basic/field-value.ts @@ -23,14 +23,14 @@ export abstract class StructFieldValue< /** Gets the associated `Struct` instance */ public readonly struct: StructValue; - protected value: TDefinition['valueType']; + protected value: TDefinition['TValue']; public constructor( definition: TDefinition, options: Readonly, context: StructSerializationContext, struct: StructValue, - value: TDefinition['valueType'], + value: TDefinition['TValue'], ) { this.definition = definition; this.options = options; @@ -51,14 +51,14 @@ export abstract class StructFieldValue< /** * When implemented in derived classes, returns the current value of this field */ - public get(): TDefinition['valueType'] { + public get(): TDefinition['TValue'] { return this.value; } /** * When implemented in derived classes, update the current value of this field */ - public set(value: TDefinition['valueType']): void { + public set(value: TDefinition['TValue']): void { this.value = value; } diff --git a/packages/struct/src/struct.ts b/packages/struct/src/struct.ts index dfd78263..e49a3e9f 100644 --- a/packages/struct/src/struct.ts +++ b/packages/struct/src/struct.ts @@ -28,9 +28,9 @@ type AddFieldDescriptor< Identity>, + Evaluate>, // Merge two `TOmitInitKey`s - TOmitInitKey | TDefinition['omitInitKeyType'], + TOmitInitKey | TDefinition['TOmitInitKey'], TExtra, TPostDeserialized >>; @@ -56,7 +56,7 @@ interface ArrayBufferLikeFieldCreator< < TName extends PropertyKey, TType extends ArrayBufferLikeFieldType, - TTypeScriptType = TType['valueType'], + TTypeScriptType = TType['TTypeScriptType'], >( name: TName, type: TType, @@ -81,7 +81,7 @@ interface ArrayBufferLikeFieldCreator< TName extends PropertyKey, TType extends ArrayBufferLikeFieldType, TOptions extends VariableLengthArrayBufferLikeFieldOptions, - TTypeScriptType = TType['valueType'], + TTypeScriptType = TType['TTypeScriptType'], >( name: TName, type: TType, @@ -112,7 +112,7 @@ interface ArrayBufferTypeFieldDefinitionCreator< > { < TName extends PropertyKey, - TTypeScriptType = TType['valueType'], + TTypeScriptType = TType['TTypeScriptType'], >( name: TName, options: FixedLengthArrayBufferLikeFieldOptions, @@ -133,7 +133,7 @@ interface ArrayBufferTypeFieldDefinitionCreator< TName extends PropertyKey, TLengthField extends LengthField, TOptions extends VariableLengthArrayBufferLikeFieldOptions, - TTypeScriptType = TType['valueType'], + TTypeScriptType = TType['TTypeScriptType'], >( name: TName, options: TOptions, @@ -154,7 +154,7 @@ interface ArrayBufferTypeFieldDefinitionCreator< export type StructPostDeserialized = (this: TFields, object: TFields) => TPostDeserialized; -export type StructDeserializedType = +export type StructDeserializedResult = TPostDeserialized extends undefined ? Overwrite : TPostDeserialized; export class Struct< @@ -162,16 +162,16 @@ export class Struct< TOmitInitKey extends PropertyKey = never, TExtra extends object = {}, TPostDeserialized = undefined, - > implements StructLike>{ - public readonly fieldsType!: TFields; + > implements StructLike>{ + public readonly TFields!: TFields; - public readonly omitInitType!: TOmitInitKey; + public readonly TOmitInitKey!: TOmitInitKey; - public readonly extraType!: TExtra; + public readonly TExtra!: TExtra; - public readonly initType!: Evaluate>; + public readonly TInit!: Evaluate>; - public readonly deserializedType!: StructDeserializedType; + public readonly TDeserializeResult!: StructDeserializedResult; public readonly options: Readonly; @@ -229,9 +229,9 @@ export class Struct< public fields>( other: TOther ): Struct< - TFields & TOther['fieldsType'], - TOmitInitKey | TOther['omitInitType'], - TExtra & TOther['extraType'], + TFields & TOther['TFields'], + TOmitInitKey | TOther['TOmitInitKey'], + TExtra & TOther['TExtra'], TPostDeserialized > { for (const field of other._fields) { @@ -245,7 +245,7 @@ export class Struct< private number< TName extends PropertyKey, TType extends NumberFieldType = NumberFieldType, - TTypeScriptType = TType['valueType'] + TTypeScriptType = TType['TTypeScriptType'] >( name: TName, type: TType, @@ -262,7 +262,7 @@ export class Struct< */ public int8< TName extends PropertyKey, - TTypeScriptType = (typeof NumberFieldType)['Uint8']['valueType'] + TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType'] >( name: TName, _typescriptType?: TTypeScriptType, @@ -279,7 +279,7 @@ export class Struct< */ public uint8< TName extends PropertyKey, - TTypeScriptType = (typeof NumberFieldType)['Uint8']['valueType'] + TTypeScriptType = (typeof NumberFieldType)['Uint8']['TTypeScriptType'] >( name: TName, _typescriptType?: TTypeScriptType, @@ -296,7 +296,7 @@ export class Struct< */ public int16< TName extends PropertyKey, - TTypeScriptType = (typeof NumberFieldType)['Uint16']['valueType'] + TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType'] >( name: TName, _typescriptType?: TTypeScriptType, @@ -313,7 +313,7 @@ export class Struct< */ public uint16< TName extends PropertyKey, - TTypeScriptType = (typeof NumberFieldType)['Uint16']['valueType'] + TTypeScriptType = (typeof NumberFieldType)['Uint16']['TTypeScriptType'] >( name: TName, _typescriptType?: TTypeScriptType, @@ -330,7 +330,7 @@ export class Struct< */ public int32< TName extends PropertyKey, - TTypeScriptType = (typeof NumberFieldType)['Int32']['valueType'] + TTypeScriptType = (typeof NumberFieldType)['Int32']['TTypeScriptType'] >( name: TName, _typescriptType?: TTypeScriptType, @@ -347,7 +347,7 @@ export class Struct< */ public uint32< TName extends PropertyKey, - TTypeScriptType = (typeof NumberFieldType)['Uint32']['valueType'] + TTypeScriptType = (typeof NumberFieldType)['Uint32']['TTypeScriptType'] >( name: TName, typescriptType?: TTypeScriptType, @@ -366,7 +366,7 @@ export class Struct< */ public int64< TName extends PropertyKey, - TTypeScriptType = (typeof NumberFieldType)['Int64']['valueType'] + TTypeScriptType = (typeof NumberFieldType)['Int64']['TTypeScriptType'] >( name: TName, _typescriptType?: TTypeScriptType, @@ -385,7 +385,7 @@ export class Struct< */ public uint64< TName extends PropertyKey, - TTypeScriptType = (typeof NumberFieldType)['Uint64']['valueType'] + TTypeScriptType = (typeof NumberFieldType)['Uint64']['TTypeScriptType'] >( name: TName, _typescriptType?: TTypeScriptType, @@ -524,7 +524,7 @@ export class Struct< public async deserialize( context: StructDeserializationContext - ): Promise> { + ): Promise> { const value = new StructValue(); Object.defineProperties(value.value, this._extra); diff --git a/packages/struct/src/types/array-buffer.ts b/packages/struct/src/types/array-buffer.ts index 1b0a7b55..062715b1 100644 --- a/packages/struct/src/types/array-buffer.ts +++ b/packages/struct/src/types/array-buffer.ts @@ -10,7 +10,7 @@ import { StructDeserializationContext, StructFieldDefinition, StructFieldValue, * specified by user when creating field definitions. */ export abstract class ArrayBufferLikeFieldType { - public readonly valueType!: TTypeScriptType; + public readonly TTypeScriptType!: TTypeScriptType; /** * When implemented in derived classes, converts the type-specific `value` to an `ArrayBuffer` @@ -105,7 +105,7 @@ export abstract class ArrayBufferLikeFieldDefinition< TOmitInitKey extends PropertyKey = never, > extends StructFieldDefinition< TOptions, - TType['valueType'], + TType['TTypeScriptType'], TOmitInitKey >{ public readonly type: TType; @@ -126,7 +126,7 @@ export abstract class ArrayBufferLikeFieldDefinition< options: Readonly, context: StructSerializationContext, struct: StructValue, - value: TType['valueType'], + value: TType['TTypeScriptType'], arrayBuffer?: ArrayBuffer, ): ArrayBufferLikeFieldValue { return new ArrayBufferLikeFieldValue(this, options, context, struct, value, arrayBuffer); @@ -161,14 +161,14 @@ export class ArrayBufferLikeFieldValue< options: Readonly, context: StructSerializationContext, struct: StructValue, - value: TDefinition['valueType'], + value: TDefinition['TValue'], arrayBuffer?: ArrayBuffer, ) { super(definition, options, context, struct, value); this.arrayBuffer = arrayBuffer; } - public set(value: TDefinition['valueType']): void { + public set(value: TDefinition['TValue']): void { super.set(value); this.arrayBuffer = undefined; } diff --git a/packages/struct/src/types/number.ts b/packages/struct/src/types/number.ts index a7de542a..aa29aec4 100644 --- a/packages/struct/src/types/number.ts +++ b/packages/struct/src/types/number.ts @@ -7,23 +7,7 @@ export type DataViewSetters = { [TKey in keyof DataView]: TKey extends `set${string}` ? TKey : never }[keyof DataView]; export class NumberFieldType { - public static readonly Int8 = new NumberFieldType(1, 'getInt8', 'setInt8'); - - public static readonly Uint8 = new NumberFieldType(1, 'getUint8', 'setUint8'); - - public static readonly Int16 = new NumberFieldType(2, 'getInt16', 'setInt16'); - - public static readonly Uint16 = new NumberFieldType(2, 'getUint16', 'setUint16'); - - public static readonly Int32 = new NumberFieldType(4, 'getInt32', 'setInt32'); - - public static readonly Uint32 = new NumberFieldType(4, 'getUint32', 'setUint32'); - - public static readonly Int64 = new NumberFieldType(8, 'getBigInt64', 'setBigInt64'); - - public static readonly Uint64 = new NumberFieldType(8, 'getBigUint64', 'setBigUint64'); - - public readonly valueType!: TTypeScriptType; + public readonly TTypeScriptType!: TTypeScriptType; public readonly size: number; @@ -40,11 +24,27 @@ export class NumberFieldType(1, 'getInt8', 'setInt8'); + + public static readonly Uint8 = new NumberFieldType(1, 'getUint8', 'setUint8'); + + public static readonly Int16 = new NumberFieldType(2, 'getInt16', 'setInt16'); + + public static readonly Uint16 = new NumberFieldType(2, 'getUint16', 'setUint16'); + + public static readonly Int32 = new NumberFieldType(4, 'getInt32', 'setInt32'); + + public static readonly Uint32 = new NumberFieldType(4, 'getUint32', 'setUint32'); + + public static readonly Int64 = new NumberFieldType(8, 'getBigInt64', 'setBigInt64'); + + public static readonly Uint64 = new NumberFieldType(8, 'getBigUint64', 'setBigUint64'); } export class NumberFieldDefinition< TType extends NumberFieldType = NumberFieldType, - TTypeScriptType = TType['valueType'], + TTypeScriptType = TType['TTypeScriptType'], > extends StructFieldDefinition< void, TTypeScriptType diff --git a/packages/struct/src/types/variable-length-array-buffer.ts b/packages/struct/src/types/variable-length-array-buffer.ts index 3b445dbe..49fb7477 100644 --- a/packages/struct/src/types/variable-length-array-buffer.ts +++ b/packages/struct/src/types/variable-length-array-buffer.ts @@ -35,7 +35,7 @@ export class VariableLengthArrayBufferLikeFieldDefinition< options: Readonly, context: StructSerializationContext, struct: StructValue, - value: TType['valueType'], + value: TType['TTypeScriptType'], arrayBuffer?: ArrayBuffer ): VariableLengthArrayBufferLikeStructFieldValue { return new VariableLengthArrayBufferLikeStructFieldValue( @@ -61,7 +61,7 @@ export class VariableLengthArrayBufferLikeStructFieldValue< options: Readonly, context: StructSerializationContext, struct: StructValue, - value: TDefinition['valueType'], + value: TDefinition['TValue'], arrayBuffer?: ArrayBuffer, ) { super(definition, options, context, struct, value, arrayBuffer);