Skip to content

Conversation

@yigityazicilar
Copy link
Contributor

@yigityazicilar yigityazicilar commented Jun 16, 2025

Motivation:

With the new Swift version (6.2) we get access to spans for safely interacting with the contiguous memory owned by other types. Allow users write bytes to the ByteBuffer with RawSpans.

Modifications:

  • ByteBuffer-core: Overload the setBytes, _setBytes, and _setBytesAssumingUniqueBufferAccess to use RawSpan instead of UnsafeRawBufferPointer
  • ByteBuffer-aux: Overload the writeBytes to use RawSpan instead of UnsafeRawBufferPointer

Result:

Users can now write the contiguous memory of other types into ByteBuffer using the new RawSpan overloads, removing the need for unsafe pointer handling.

Motivation:

With the new Swift version (6.2) we get access to spans for safely interacting with the contiguous memory owned by other types. Allow users write bytes to the ByteBuffer with RawSpans.

Modifications:

* ByteBuffer-core: Overload the setBytes, _setBytes, and _setBytesAssumingUnique
BufferAccess to use RawSpan instead of UnsafeRawBufferPointer
* ByteBuffer-aux: Overload the writeBytes to use RawSpan instead of UnsafeRawBuf
ferPointer

Result:

Users can now write the contiguous memory of other types into ByteBuffer using the new RawSpan overloads, removing the need for unsafe pointer handling.
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.

Shiny, this looks great. Thanks @yigityazicilar!

@Lukasa Lukasa added the 🆕 semver/minor Adds new public API. label Jun 16, 2025
@Lukasa Lukasa enabled auto-merge (squash) June 16, 2025 16:47
@Lukasa Lukasa merged commit cddbde5 into apple:main Jun 16, 2025
42 checks passed
@discardableResult
@inlinable
@available(macOS 26, iOS 26, tvOS 26, watchOS 26, visionOS 26, *)
public mutating func writeBytes(_ bytes: RawSpan) -> Int {
Copy link
Member

Choose a reason for hiding this comment

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

Should we mark this as consuming?

Copy link
Contributor

Choose a reason for hiding this comment

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

There's no need: we don't consume it. We copy the bytes out.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, and also Span is copyable, so consuming doesn't have much of a practical effect either. It can, in the best case, elide a copy of the Span object, but that's just two words so it's not really worth the hassle.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah Span is Copyable so this might give the compiler a nudge to optimize it a bit better. At least that's what I was thinking.

Copy link
Member

Choose a reason for hiding this comment

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

If anything, it should be borrowing, right? We're happy to just borrow this -- now as per @Lukasa's point: not much to win here of course.

@discardableResult
@inlinable
@available(macOS 26, iOS 26, tvOS 26, watchOS 26, visionOS 26, *)
public mutating func setBytes(_ bytes: RawSpan, at index: Int) -> Int {
Copy link
Member

Choose a reason for hiding this comment

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

Same here?

@yigityazicilar yigityazicilar deleted the bytebuffer-rawspan branch June 20, 2025 21:56
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.

4 participants