swift-nio icon indicating copy to clipboard operation
swift-nio copied to clipboard

Add `InlineArray` helpers to `ByteBuffer`

Open MahdiBM opened this issue 9 months ago • 1 comments

Motivation:

Useful for parsing packets, for example ipv4: InlineArray<4, UInt8> and ipv6: InlineArray<16, UInt8>.

Modifications:

For now I've only added a readInlineArray function. I know some other functions are missing, such as writeInlineArray. I wanted to first open up a discussion and see if these changes are acceptable. I can add those functions too if required, in this PR or other PRs.

Result:

Users can read ByteBuffer into stack-allocated memory, which can be more performant than the other alternatives like Array, or more convenient than reading as a tuple like (UInt8, UInt8, UInt8, UInt8).

Caveats:

Swift 6.2 is required so I've used #if compiler(>=6.2). Furthermore, InlineArray is marked as available on macOS(9999) since the Swift team have yet to update that mark, although InlineArray is planned for Swift 6.2 per the proposal.

Edit: to be clear things work fine on Linux and that's where I've been using this same readInlineArray function that I've proposed.

MahdiBM avatar May 24 '25 12:05 MahdiBM

I'd highly assume a 6.2 snapshot with InlineArray available on macOS would be released before the WWDC. We can wait till then just so the PR is not "weird" in the sense that it works on Linux and not Darwin. We can also merge this and I'll modify the availability checks in another PR as soon as I can confirm the 6.2 macOS snapshots have InlineArray available.

MahdiBM avatar May 26 '25 19:05 MahdiBM

@MahdiBM and @Lukasa is this ready for prime time?

Joannis avatar Sep 17 '25 15:09 Joannis

I'l need to add the availability annotations and we should be good. Currently on a mini vacation, I'll see if I find some spare time to do this.

MahdiBM avatar Sep 18 '25 08:09 MahdiBM

@Lukasa Review please 🙂.

MahdiBM avatar Sep 21 '25 20:09 MahdiBM

@Lukasa anything I can help with? 🙂

MahdiBM avatar Sep 24 '25 04:09 MahdiBM

No, this got delayed because of the CI hiccups that caused the prior two jobs to fail.

Lukasa avatar Sep 24 '25 08:09 Lukasa