Skip to content

Conversation

@adam-fowler
Copy link
Contributor

Add support for treating WebSocketFrame reserved bits as an option set

Motivation:

I would like to do mask operations on the reserved bits of the WebSocketFrame eg check none are set, check only bits in a mask are set. With the current public interface it is only possible to check the state of one bit at a time.

Modifications:

Add WebSocketFrame.ReservedBits OptionSet
Add computed member WebSocketFrame.reservedBits

Result:

I can now check the status of multiple bits in one operation eg
frame.reservedBits.isEmpty
frame.reservedBits.itersection([.rsv1, .rsv2]).isEmpty

@adam-fowler adam-fowler changed the title Add WebSocketFrame.reservedBits Add WebSocketFrame.reservedBits OptionSet Nov 12, 2024
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mostly looks good, thanks Adam! A quick tweak on the diff.

public struct ReservedBits: OptionSet, Sendable {
public var rawValue: UInt8

public init(rawValue: UInt8) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this @inlinable and all the static vars as well. That unlocks the meaningful performance improvement.

Copy link
Contributor Author

@adam-fowler adam-fowler Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed the get/set accessors on firstByte are not @inlinable. Is it worthwhile tagging all of those? eg fin, opcode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tagged them anyway

@Lukasa Lukasa added the 🆕 semver/minor Adds new public API. label Nov 15, 2024
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one, thanks Adam!

@Lukasa Lukasa enabled auto-merge (squash) November 15, 2024 17:22
auto-merge was automatically disabled November 15, 2024 17:26

Head branch was pushed to by a user without write access

@adam-fowler
Copy link
Contributor Author

aargh swift-format

@Lukasa Lukasa enabled auto-merge (squash) November 15, 2024 17:29
@Lukasa Lukasa merged commit d836385 into apple:main Nov 15, 2024
41 of 42 checks passed
@adam-fowler adam-fowler deleted the ws-reserved-bits branch November 15, 2024 19:20
Lukasa pushed a commit to Lukasa/swift-nio that referenced this pull request Nov 22, 2024
Add support for treating WebSocketFrame reserved bits as an option set

### Motivation:

I would like to do mask operations on the reserved bits of the
WebSocketFrame eg check none are set, check only bits in a mask are set.
With the current public interface it is only possible to check the state
of one bit at a time.

### Modifications:

Add `WebSocketFrame.ReservedBits` OptionSet
Add computed member `WebSocketFrame.reservedBits`

### Result:

I can now check the status of multiple bits in one operation eg
`frame.reservedBits.isEmpty`
`frame.reservedBits.itersection([.rsv1, .rsv2]).isEmpty`

(cherry picked from commit d836385)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants