Skip to content

Commit

Permalink
Make ByteBuffer.debugDescription suitable for structural display (#…
Browse files Browse the repository at this point in the history
…2495)

### Motivation

`CustomDebugStringConvertible `/`debugDescription` name and
documentation is confusing and should be changed but it is today
expected to be suitable to be used as part of a structured display e.g.
to be printed as part of an `Array`s description, a `struct` or `enum`
with associated values. Therefore it should have no unpaired
parentheses, no unescaped quotes, no top-level commas and no new lines.

### Modifications
let `debugDescription` simply contain the same contents as
`description`. We can't remove the property or the conformance without
breaking API.

### Results
`ByteBuffer` has a proper string representation suitable for being
displayed in an `Array`, as the property of a `struct` or an associated
value of an `enum`.

We can add a new property/method once #2475 landed e.g.
```swift
extension ByteBuffer {
    struct PrintFormat {
        static let hex: Self
        static let decimal: Self
        ...
    }
    func descriptionWithContents(format: PrintFormat = .hex, maxBytes: Int = 1024) {
        ...
    }
}
```

Co-authored-by: Franz Busch <[email protected]>
  • Loading branch information
dnadoba and FranzBusch authored Sep 9, 2024
1 parent 216975d commit d2713ab
Showing 1 changed file with 1 addition and 11 deletions.
12 changes: 1 addition & 11 deletions Sources/NIOCore/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -971,18 +971,8 @@ extension ByteBuffer: CustomStringConvertible, CustomDebugStringConvertible {
"[\(self.hexDump(format: .compact(maxBytes: 64)))](\(self.readableBytes) bytes)"
}

/// A `String` describing this `ByteBuffer` including length and the bytes it contains (partially).
///
/// For a `ByteBuffer` initialised with `hello world` the description would be the following:
///
/// [68656c6c6f20776f726c64](11 bytes)
///
/// Buffers larger that 64 bytes will get truncated when printing out.
/// The format of the description is not API.
///
/// - returns: A description of this `ByteBuffer`.
public var debugDescription: String {
"[\(self.hexDump(format: .compact(maxBytes: 64)))](\(self.readableBytes) bytes)"
String(describing: self)
}
}

Expand Down

0 comments on commit d2713ab

Please sign in to comment.