Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CustomDebugStringConvertible conformance to ByteBuffer #2418

Merged
merged 1 commit into from
May 3, 2023
Merged

Add CustomDebugStringConvertible conformance to ByteBuffer #2418

merged 1 commit into from
May 3, 2023

Conversation

gwynne
Copy link
Contributor

@gwynne gwynne commented May 3, 2023

Declare CustomDebugStringConvertible conformance for ByteBuffer, which already implements the conformance requirement.

Motivation:

ByteBuffer implements both description and debugDescription, but conforms only to CustomStringConvertible. As a result, String(describing: ByteBuffer()) and String(reflecting: ByteBuffer()) yield the same output, when it would be expected that the latter would include the additional "readable bytes (max 1k)" debug output returned by invoking debugDescription directly. To my knowledge, there is no drawback to adding this conformance.

Modifications:

Added CustomDebugStringConvertible to the list of protocol conformances on the extension which declares the description and debugDescription properties and the existing CustomStringConvertible conformance.

Result:

Given:

print(ByteBuffer().description)
print(String(describing: ByteBuffer()))
print(ByteBuffer().debugDescription)
print(String(reflecting: ByteBuffer()))

Before (slightly edited for brevity):

ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, ... }
ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, ... }
ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, ... }
readable bytes (max 1k): [ ]
ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, ... }

After (slightly edited for brevity):

ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, ... }
ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, ... }
ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, ... }
readable bytes (max 1k): [ ]
ByteBuffer { readerIndex: 0, writerIndex: 0, readableBytes: 0, ... }
readable bytes (max 1k): [ ]

@Lukasa Lukasa added the semver/minor Adds new public API. label May 3, 2023
Copy link
Member

@dnadoba dnadoba left a comment

Choose a reason for hiding this comment

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

Great catch! I actually always wondered why we are not printing part of the contents.

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 seems like a good enhancement, thanks!

@Lukasa Lukasa enabled auto-merge (squash) May 3, 2023 09:58
@Lukasa Lukasa merged commit 014812a into apple:main May 3, 2023
@gwynne gwynne deleted the patch-1 branch May 3, 2023 10:29
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.

3 participants