denosaurs / denosaurs/byte_type

Feat(util): `offsetOf` and `offsetOfPacked` utility functions

Open
#51 0 comments 0 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
TypeScript
Stars
45
Forks
7
PR merge metrics
No merged PRs in 30d

Description

[NeKzor](https://github.com/NeKzor)
> a util like offsetOf would be useful so I can remove my current hack.

[MierenManz](https://github.com/MierenManz)
> @NeKzor What is your current hack and what should offsetOf do? I assume it's something like this?
```ts
const codec = new SizedStruct({ byte: u8, word: u16 });
console.log(offsetOf(codec)) // Logs `{ aligned: 4, packed: 3 }`
```

[NeKzor](https://github.com/NeKzor)
> I better not post my hack because I implemented it incorrectly lol
> It would get the offset of a field inside a struct inspired by [C++](https://en.cppreference.com/w/cpp/types/offsetof) and [Rust](https://doc.rust-lang.org/nightly/core/mem/macro.offset_of.html). Something like this:
```ts
offsetOf({ byte: u8, word: u32le }, 'word'); // 4
offsetOfPacked({ byte: u8, word: u32le }, 'word'); // 1
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.