Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
MierenManz committed Dec 12, 2023
1 parent dc59a56 commit e81cf1e
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 68 deletions.
12 changes: 6 additions & 6 deletions src/array/array.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AlignedType, type Options } from "../types/mod.ts";
import { UnsizedType, type Options } from "../types/mod.ts";

export class ArrayType<T> extends AlignedType<T[]> {
constructor(readonly type: AlignedType<T>, readonly length: number) {
export class ArrayType<T> extends UnsizedType<T[]> {
constructor(readonly type: UnsizedType<T>, readonly length: number) {
super(type.byteAlignment);
}

Expand All @@ -25,7 +25,7 @@ export class ArrayType<T> extends AlignedType<T[]> {

for (let i = 0; i < this.length; i++) {
result[i] = this.type.read(dt, options);
// No need for the increment offset. This is handled by the `type.readPacked` function
// No need for the increment offset. This is handled by the `type.read` function
}

return result as T[];
Expand All @@ -43,7 +43,7 @@ export class ArrayType<T> extends AlignedType<T[]> {

for (let i = 0; i < this.length; i++) {
this.type.writePacked(value[i], dt, options);
// No need for the increment offset. This is handled by the `type.writeUnaligned` function
// No need for the increment offset. This is handled by the `type.writePacked` function
}
}

Expand All @@ -59,7 +59,7 @@ export class ArrayType<T> extends AlignedType<T[]> {

for (let i = 0; i < this.length; i++) {
this.type.write(value[i], dt, options);
// No need for the increment offset. This is handled by the `type.writeUnaligned` function
// No need for the increment offset. This is handled by the `type.write` function
}
}
}
8 changes: 4 additions & 4 deletions src/compound/struct.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { AlignedType, type InnerType, type Options } from "../types/mod.ts";
import { UnsizedType, type InnerType, type Options } from "../types/mod.ts";
import { getBiggestAlignment } from "../util.ts";

export class Struct<
T extends Record<string, AlignedType<unknown>>,
T extends Record<string, UnsizedType<unknown>>,
V extends { [K in keyof T]: InnerType<T[K]> } = {
[K in keyof T]: InnerType<T[K]>;
},
> extends AlignedType<V> {
#record: Array<[string, AlignedType<unknown>]>;
> extends UnsizedType<V> {
#record: Array<[string, UnsizedType<unknown>]>;

constructor(input: T) {
super(getBiggestAlignment(input));
Expand Down
14 changes: 7 additions & 7 deletions src/compound/tagged_union.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { u8 } from "../primitives/mod.ts";
import {
AlignedType,
UnsizedType,
type InnerType,
type Options,
type ValueOf,
Expand All @@ -12,31 +12,31 @@ type FindDiscriminant<V, D extends number | string> = (variant: V) => D;
type Keys<T> = Exclude<keyof T, symbol>;

export class TaggedUnion<
T extends Record<string | number, AlignedType<unknown>>,
T extends Record<string | number, UnsizedType<unknown>>,
V extends ValueOf<{ [K in keyof T]: InnerType<T[K]> }> = ValueOf<
{ [K in keyof T]: InnerType<T[K]> }
>,
> extends AlignedType<V> {
> extends UnsizedType<V> {
#record: T;
#variantFinder: FindDiscriminant<V, Keys<T>>;
#discriminant: AlignedType<string | number>;
#discriminant: UnsizedType<string | number>;

constructor(
input: T,
variantFinder: FindDiscriminant<V, Keys<T>>,
discriminant: Keys<T> extends string ? AlignedType<string> : never,
discriminant: Keys<T> extends string ? UnsizedType<string> : never,
);

constructor(
input: T,
variantFinder: FindDiscriminant<V, Keys<T>>,
discriminant?: Keys<T> extends number ? AlignedType<number> : never,
discriminant?: Keys<T> extends number ? UnsizedType<number> : never,
);

constructor(
input: T,
variantFinder: FindDiscriminant<V, Keys<T>>,
discriminant: AlignedType<string | number> = u8,
discriminant: UnsizedType<string | number> = u8,
) {
super(getBiggestAlignment(input));
this.#record = input;
Expand Down
6 changes: 3 additions & 3 deletions src/compound/tuple.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AlignedType, type InnerType, type Options } from "../types/mod.ts";
import { UnsizedType, type InnerType, type Options } from "../types/mod.ts";
import { getBiggestAlignment } from "../util.ts";

export class Tuple<
T extends [...AlignedType<unknown>[]],
T extends [...UnsizedType<unknown>[]],
V extends [...unknown[]] = { [I in keyof T]: InnerType<T[I]> },
> extends AlignedType<V> {
> extends UnsizedType<V> {
#tupleTypes: T;

constructor(types: T) {
Expand Down
4 changes: 2 additions & 2 deletions src/string/cstring.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AlignedType, type Options } from "../types/mod.ts";
import { UnsizedType, type Options } from "../types/mod.ts";
import { TEXT_DECODER, TEXT_ENCODER } from "./_common.ts";

export class CString extends AlignedType<string> {
export class CString extends UnsizedType<string> {
constructor() {
super(1);
}
Expand Down
35 changes: 0 additions & 35 deletions src/types/aligned.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/types/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./_common.ts";
export { AlignedType } from "./aligned.ts";
export { SizedType } from "./sized.ts";
export { UnsizedType } from "./unsized.ts";
8 changes: 4 additions & 4 deletions src/types/sized.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { type Aligned, AlignedType } from "./aligned.ts";
import { type Unsized, UnsizedType } from "./unsized.ts";
import type { Options } from "./_common.ts";

interface Sized<T> extends Aligned<T> {
interface Sized<T> extends Unsized<T> {
readonly byteSize: number;
}

export abstract class SizedType<T> extends AlignedType<T> implements Sized<T> {
constructor(readonly byteSize: number, byteAlignment: number) {
export abstract class SizedType<T> extends UnsizedType<T> implements Sized<T> {
constructor(readonly byteSize: number, byteAlignment: number = 1) {
super(byteAlignment);
}

Expand Down
22 changes: 22 additions & 0 deletions src/types/unsized.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@

import {align} from "../util.ts";
import type { Options } from "./_common.ts";

export interface Unsized<T> {
readonly byteAlignment: number;

readPacked(dt: DataView, options?: Options): T;
writePacked(value: T, dt: DataView, options?: Options): void;
}

export abstract class UnsizedType<T> implements Unsized<T> {
constructor(readonly byteAlignment: number) {}

abstract readPacked(dt: DataView, options?: Options): T;
abstract writePacked(value: T, dt: DataView, options?: Options): void;

/** In most cases you don't need to reimplement read. as long as your `readPacked` is correct */
read(dt: DataView, options: Options = { byteOffset: 0 }): T {
this.alignOffset(options);
return this.readPacked(dt, options);
}

/** In most cases you don't need to reimplement write. as long as your `writePacked` is correct */
write(value: T, dt: DataView, options: Options = { byteOffset: 0 }): void {
this.alignOffset(options);
this.writePacked(value, dt, options);
}

protected alignOffset(options: Options) {
options.byteOffset = align(options.byteOffset, this.byteAlignment);
}

protected incrementOffset(options: Options, byteSize: number) {
options.byteOffset += byteSize;
}
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AlignedType } from "./mod.ts";
import type { UnsizedType } from "./mod.ts";

/**
* The endianess of your machine, true if little endian and false if big endian.
Expand All @@ -15,7 +15,7 @@ export const align = (unaligned: number, alignment: number) =>
type ArrayOrRecord<T> = T[] | Record<string | number, T>;

export const getBiggestAlignment = (
input: ArrayOrRecord<AlignedType<unknown>>,
input: ArrayOrRecord<UnsizedType<unknown>>,
) =>
Object.values(input)
.reduce((acc, x) => Math.max(acc, x.byteAlignment), 0);
4 changes: 2 additions & 2 deletions src/varint/i32_leb128.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AlignedType, type Options } from "../types/mod.ts";
import { UnsizedType, type Options } from "../types/mod.ts";
import { CONTINUE_BIT, SEGMENT_BITS } from "./_common.ts";

export class I32Leb128 extends AlignedType<number> {
export class I32Leb128 extends UnsizedType<number> {
constructor() {
super(1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/varint/i64_leb128.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AlignedType, type Options } from "../types/mod.ts";
import { UnsizedType, type Options } from "../types/mod.ts";
import { CONTINUE_BIT, SEGMENT_BITS } from "./_common.ts";

const SEGMENT_BITS_N = BigInt(SEGMENT_BITS);
Expand All @@ -9,7 +9,7 @@ const U32_VIEW = new Uint32Array(AB);
const I64_VIEW = new BigInt64Array(AB);
const U64_VIEW = new BigUint64Array(AB);

export class I64Leb128 extends AlignedType<bigint> {
export class I64Leb128 extends UnsizedType<bigint> {
constructor() {
super(1);
}
Expand Down

0 comments on commit e81cf1e

Please sign in to comment.