-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7952bd
commit 8a52ae9
Showing
4 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { u8 } from "../primitives/mod.ts"; | ||
import { | ||
type InnerType, | ||
type Options, | ||
UnsizedType, | ||
type ValueOf, | ||
} from "../types/mod.ts"; | ||
import { getBiggestAlignment } from "../util.ts"; | ||
|
||
type FindDiscriminant<V, D extends number> = (variant: V) => D; | ||
|
||
type Keys<T> = Exclude<keyof T, symbol | string>; | ||
|
||
/** Union for when the inner type's do write their own discriminant */ | ||
export class Union< | ||
T extends Record<number, UnsizedType<unknown>>, | ||
V extends ValueOf<{ [K in keyof T]: InnerType<T[K]> }>, | ||
> extends UnsizedType<V> { | ||
#record: T; | ||
#variantFinder: FindDiscriminant<V, Keys<T>>; | ||
#discriminant = u8; | ||
|
||
constructor( | ||
input: T, | ||
variantFinder: FindDiscriminant<V, Keys<T>>, | ||
) { | ||
super(getBiggestAlignment(input)); | ||
this.#record = input; | ||
this.#variantFinder = variantFinder; | ||
} | ||
|
||
readPacked(dt: DataView, options: Options = { byteOffset: 0 }): V { | ||
const discriminant = this.#discriminant.readPacked(dt, { | ||
byteOffset: options.byteOffset, | ||
}); | ||
const codec = this.#record[discriminant]; | ||
if (!codec) throw new TypeError("Unknown discriminant"); | ||
return codec.readPacked(dt, options) as V; | ||
} | ||
|
||
read(dt: DataView, options: Options = { byteOffset: 0 }): V { | ||
const discriminant = this.#discriminant.read(dt, { | ||
byteOffset: options.byteOffset, | ||
}); | ||
const codec = this.#record[discriminant]; | ||
if (!codec) throw new TypeError("Unknown discriminant"); | ||
return codec.readPacked(dt, options) as V; | ||
} | ||
|
||
writePacked( | ||
variant: V, | ||
dt: DataView, | ||
options: Options = { byteOffset: 0 }, | ||
): void { | ||
const discriminant = this.#variantFinder(variant); | ||
const codec = this.#record[discriminant]; | ||
if (!codec) throw new TypeError("Unknown discriminant"); | ||
codec.writePacked(variant, dt, options); | ||
} | ||
|
||
write(variant: V, dt: DataView, options: Options = { byteOffset: 0 }): void { | ||
const discriminant = this.#variantFinder(variant); | ||
const codec = this.#record[discriminant]; | ||
if (!codec) throw new TypeError("Unknown discriminant"); | ||
codec.write(variant, dt, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { u32le, u8 } from "../mod.ts"; | ||
import { assertEquals, assertThrows } from "../../test_deps.ts"; | ||
import { Union } from "./union.ts"; | ||
|
||
Deno.test({ | ||
name: "Union", | ||
fn: async (t) => { | ||
const ab = new ArrayBuffer(8); | ||
const dt = new DataView(ab); | ||
const type = new Union({ | ||
0: u32le, | ||
1: u8, | ||
2: u8, | ||
}, (a) => a === 32 ? 0 : 1); | ||
|
||
await t.step("Read", () => { | ||
dt.setUint8(0, 1); | ||
dt.setUint8(1, 11); | ||
dt.setUint8(2, 22); | ||
dt.setUint8(4, 33); | ||
const result = type.read(dt); | ||
assertEquals(result, 1); | ||
}); | ||
|
||
await t.step("Read Packed", () => { | ||
dt.setUint8(0, 1); | ||
dt.setUint8(1, 11); | ||
dt.setUint8(2, 22); | ||
dt.setUint8(4, 33); | ||
const result = type.readPacked(dt); | ||
assertEquals(result, 1); | ||
}); | ||
|
||
dt.setBigUint64(0, 0n); | ||
|
||
await t.step("Write", () => { | ||
type.write(32, dt); | ||
assertEquals(new Uint32Array(ab), Uint32Array.of(32, 0)); | ||
}); | ||
|
||
dt.setBigUint64(0, 0n); | ||
|
||
await t.step("Write Packed", () => { | ||
type.writePacked(32, dt); | ||
assertEquals( | ||
new Uint8Array(ab).subarray(0, 5), | ||
Uint8Array.of(32, 0, 0, 0, 0), | ||
); | ||
}); | ||
|
||
await t.step("OOB Read", () => { | ||
assertThrows(() => { | ||
type.read(dt, { byteOffset: 9 }); | ||
}, RangeError); | ||
}); | ||
}, | ||
}); |