Skip to content

Make WebGPU classes "nominal types" (not structural types) #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ declare global {
}

export class GPUAdapter {
// https://michalzalecki.com/nominal-typing-in-typescript/#approach-1-class-with-a-private-property
private __brand: void;
readonly name: string;
readonly extensions: GPUExtensionName[];
readonly limits: GPULimitsOut;
Expand All @@ -486,14 +488,17 @@ declare global {
}

export class GPUBindGroup implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

export class GPUBindGroupLayout implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

export class GPUBuffer implements GPUObjectBase {
private __brand: void;
label: string | undefined;

//readonly mapping: ArrayBuffer | null;
Expand All @@ -512,12 +517,14 @@ declare global {
}

export class GPUCommandBuffer implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

export interface GPUCommandBufferDescriptor extends GPUObjectDescriptorBase {}

export class GPUCommandEncoder implements GPUObjectBase {
private __brand: void;
label: string | undefined;

beginComputePass(
Expand Down Expand Up @@ -556,6 +563,7 @@ declare global {
export interface GPUComputePassDescriptor extends GPUObjectDescriptorBase {}

export class GPUComputePassEncoder implements GPUProgrammablePassEncoder {
private __brand: void;
label: string | undefined;

setBindGroup(
Expand All @@ -576,6 +584,7 @@ declare global {
}

export class GPUComputePipeline implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

Expand All @@ -589,12 +598,14 @@ declare global {

// SwapChain / CanvasContext
export class GPUCanvasContext {
private __brand: void;
configureSwapChain(descriptor: GPUSwapChainDescriptor): GPUSwapChain;

getSwapChainPreferredFormat(device: GPUDevice): Promise<GPUTextureFormat>;
}

export class GPUDevice extends EventTarget implements GPUObjectBase {
private __brand: void;
label: string | undefined;

readonly adapter: GPUAdapter;
Expand Down Expand Up @@ -638,13 +649,15 @@ declare global {
}

export class GPUFence implements GPUObjectBase {
private __brand: void;
label: string | undefined;

getCompletedValue(): number;
onCompletion(completionValue: number): Promise<void>;
}

export class GPUPipelineLayout implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

Expand All @@ -661,6 +674,7 @@ declare global {
}

export class GPUQueue implements GPUObjectBase {
private __brand: void;
label: string | undefined;

signal(fence: GPUFence, signalValue: number): void;
Expand Down Expand Up @@ -701,6 +715,7 @@ declare global {
}

export class GPURenderPassEncoder implements GPURenderEncoderBase {
private __brand: void;
label: string | undefined;

setBindGroup(
Expand Down Expand Up @@ -758,10 +773,12 @@ declare global {
export interface GPURenderBundleDescriptor extends GPUObjectDescriptorBase {}

export class GPURenderBundle implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

export class GPURenderBundleEncoder implements GPURenderEncoderBase {
private __brand: void;
label: string | undefined;

setBindGroup(
Expand Down Expand Up @@ -810,31 +827,37 @@ declare global {
}

export class GPURenderPipeline implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

export class GPUSampler implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

export class GPUShaderModule implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

export class GPUSwapChain implements GPUObjectBase {
private __brand: void;
label: string | undefined;

getCurrentTexture(): GPUTexture;
}

export class GPUTexture implements GPUObjectBase {
private __brand: void;
label: string | undefined;

createView(descriptor?: GPUTextureViewDescriptor): GPUTextureView;
destroy(): void;
}

export class GPUTextureView implements GPUObjectBase {
private __brand: void;
label: string | undefined;
}

Expand All @@ -844,6 +867,7 @@ declare global {
}

export class GPU {
private __brand: void;
requestAdapter(options?: GPURequestAdapterOptions): Promise<GPUAdapter>;
}

Expand All @@ -854,10 +878,12 @@ declare global {
export type GPUErrorFilter = "none" | "out-of-memory" | "validation";

export class GPUOutOfMemoryError {
private __brand: void;
constructor();
}

export class GPUValidationError {
private __brand: void;
constructor(message: string);
readonly message: string;
}
Expand All @@ -869,6 +895,7 @@ declare global {
// ****************************************************************************

export class GPUUncapturedErrorEvent extends Event {
private __brand: void;
constructor(
type: string,
gpuUncapturedErrorEventInitDict: GPUUncapturedErrorEventInit
Expand All @@ -881,6 +908,7 @@ declare global {
}

export class GPUDeviceLostInfo {
private __brand: void;
readonly message: string;
}
}