Skip to content
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

refactor(util): Rename utility types #49

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions src/compound/sized_struct.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { calculateTotalSize, getBiggestAlignment } from "../util.ts";
import { type InnerType, type Options, SizedType } from "../types/mod.ts";
import { type InnerType, type Options, SizedType } from "../mod.ts";
import { alignmentOf, sizeOf } from "../util.ts";
import { Struct } from "./mod.ts";

type ReadFn<R> = (dt: DataView, options: Options) => R;
Expand Down Expand Up @@ -48,7 +48,7 @@ export class SizedStruct<
#inner: Partial<Struct<T, V>>;

constructor(input: T, jitEnabled: boolean = true) {
super(calculateTotalSize(input), getBiggestAlignment(input));
super(sizeOf(input), alignmentOf(input));
if (jitEnabled) {
this.#inner = {
read: createReadMethod(input, false),
Expand Down
4 changes: 2 additions & 2 deletions src/compound/struct.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type InnerType, type Options, UnsizedType } from "../types/mod.ts";
import { getBiggestAlignment } from "../util.ts";
import { alignmentOf } from "../util.ts";

export class Struct<
T extends Record<string, UnsizedType<unknown>>,
Expand All @@ -10,7 +10,7 @@ export class Struct<
#record: Array<[string, UnsizedType<unknown>]>;

constructor(input: T) {
super(getBiggestAlignment(input));
super(alignmentOf(input));
this.#record = Object.entries(input);
}

Expand Down
4 changes: 2 additions & 2 deletions src/compound/tagged_union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
UnsizedType,
type ValueOf,
} from "../types/mod.ts";
import { getBiggestAlignment } from "../util.ts";
import { alignmentOf } from "../util.ts";

type FindDiscriminant<V, D extends number | string> = (variant: V) => D;

Expand Down Expand Up @@ -39,7 +39,7 @@ export class TaggedUnion<
variantFinder: FindDiscriminant<V, Keys<T>>,
discriminant: UnsizedType<string | number> = u8,
) {
super(getBiggestAlignment(input));
super(alignmentOf(input));
this.#record = input;
this.#variantFinder = variantFinder;
this.#discriminant = discriminant;
Expand Down
4 changes: 2 additions & 2 deletions src/compound/tuple.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type InnerType, type Options, UnsizedType } from "../types/mod.ts";
import { getBiggestAlignment } from "../util.ts";
import { alignmentOf } from "../util.ts";

export class Tuple<
T extends [...UnsizedType<unknown>[]],
Expand All @@ -8,7 +8,7 @@ export class Tuple<
#tupleTypes: T;
#length: number;
constructor(types: T) {
super(getBiggestAlignment(types));
super(alignmentOf(types));
this.#tupleTypes = types;
this.#length = types.length;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compound/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
UnsizedType,
type ValueOf,
} from "../types/mod.ts";
import { getBiggestAlignment } from "../util.ts";
import { alignmentOf } from "../util.ts";

type FindDiscriminant<V, D extends number> = (variant: V) => D;

Expand All @@ -24,7 +24,7 @@ export class Union<
input: T,
variantFinder: FindDiscriminant<V, Keys<T>>,
) {
super(getBiggestAlignment(input));
super(alignmentOf(input));
this.#record = input;
this.#variantFinder = variantFinder;
}
Expand Down
20 changes: 16 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,29 @@ export const isLittleEndian: boolean = (() => {
export const align = (unaligned: number, alignment: number): number =>
(unaligned + alignment - 1) & ~(alignment - 1);

type ArrayOrRecord<T> = T[] | Record<string | number, T>;
type ArrayOrRecord<T> = Array<T> | Record<string | number, T>;

/** Find and returns the biggest alignment out of a record / array of types */
export const getBiggestAlignment = (
/** Find and returns the biggest alignment of a record/array of types */
export const alignmentOf = (
input: ArrayOrRecord<UnsizedType<unknown>>,
): number =>
Object.values(input)
.reduce((acc, x) => Math.max(acc, x.byteAlignment), 0);
/**
* Find and returns the biggest alignment of a record/array of types
* @deprecated
*/
export const getBiggestAlignment = alignmentOf;

export const calculateTotalSize = (
/** Find and return the unaligned size of a record/array of types */
export const sizeOf = (
input: ArrayOrRecord<SizedType<unknown>>,
): number =>
Object.values(input)
.reduce((acc, x) => acc + x.byteSize, 0);

/**
* Find and return the unaligned size of a record/array of types
* @deprecated
*/
export const calculateTotalSize = sizeOf;