Skip to content

Commit

Permalink
dedupe code
Browse files Browse the repository at this point in the history
  • Loading branch information
MierenManz committed Nov 13, 2023
1 parent fc2a01b commit ed34658
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/compound/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type Options,
type Packed,
} from "../types/mod.ts";
import { getBiggestAlignment } from "../util.ts";

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

constructor(input: T) {
// Find biggest alignment
const byteAlignment = Object.values(input)
.reduce((acc, x) => Math.max(acc, x.byteAlignment), 0);
super(byteAlignment);
super(getBiggestAlignment(input));
this.#record = Object.entries(input);
}

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

type Fn<T> = (value: T) => number;

Expand All @@ -19,11 +20,12 @@ export class TaggedUnion<
#variantFinder: Fn<V>;
#discriminant: AlignedType<number>;

constructor(input: T, variantFinder: Fn<V>, discriminantCodec: AlignedType<number> = u8) {
// Find biggest alignment
const byteAlignment = Object.values(input)
.reduce((acc, x) => Math.max(acc, x.byteAlignment), 0);
super(byteAlignment);
constructor(
input: T,
variantFinder: Fn<V>,
discriminantCodec: AlignedType<number> = u8,
) {
super(getBiggestAlignment(input));
this.#record = input;
this.#variantFinder = variantFinder;
this.#discriminant = discriminantCodec;
Expand Down
6 changes: 2 additions & 4 deletions src/compound/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type Options,
type Packed,
} from "../types/mod.ts";
import { getBiggestAlignment } from "../util.ts";

export class Tuple<
T extends [...AlignedType<unknown>[]],
Expand All @@ -12,10 +13,7 @@ export class Tuple<
#tupleTypes: T;

constructor(types: T) {
// Find biggest alignment
const byteAlignment = Object.values(types)
.reduce((acc, x) => Math.max(acc, x.byteAlignment), 0);
super(byteAlignment);
super(getBiggestAlignment(types));
this.#tupleTypes = types;
}

Expand Down
10 changes: 10 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { AlignedType } from "./mod.ts";

/**
* The endianess of your machine, true if little endian and false if big endian.
*/
Expand All @@ -9,3 +11,11 @@ export const isLittleEndian = (() => {

export const align = (unaligned: number, alignment: number) =>
(unaligned + alignment - 1) & ~(alignment - 1);

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

export const getBiggestAlignment = (
input: ArrayOrRecord<AlignedType<unknown>>,
) =>
Object.values(input)
.reduce((acc, x) => Math.max(acc, x.byteAlignment), 0);

0 comments on commit ed34658

Please sign in to comment.