Repository: apple/swift-nio
File: Sources/NIOCore/ByteBuffer-core.swift (Swift)

Code before:
    public var readableBytesSpan: RawSpan {
        @_lifetime(borrow self)
        borrowing get {
            let range = Range<Int>(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex))
            return _overrideLifetime(RawSpan(_unsafeBytes: self._slicedStorageBuffer[range]), borrowing: self)
        }
    }

    /// Provides mutable access to the readable bytes of this buffer.
    @inlinable
    @available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
    public var mutableReadableBytesSpan: MutableRawSpan {
        @_lifetime(&self)
        mutating get {
            self._copyStorageAndRebaseIfNeeded()
            let range = Range<Int>(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex))
            return _overrideLifetime(MutableRawSpan(_unsafeBytes: self._slicedStorageBuffer[range]), mutating: &self)
        }
    }

    /// Enables high-performance low-level appending into the writable section of this buffer.
    @inlinable
    @available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
    public mutating func write(
        minimumWritableBytes: Int,
        initializingWith initializer: (inout OutputRawSpan) throws -> Void
    ) rethrows {
        try self.writeWithUnsafeMutableBytes(minimumWritableBytes: minimumWritableBytes) { ptr in
            var span = OutputRawSpan(buffer: ptr, initializedCount: 0)
            try initializer(&span)
            return span.byteCount
        }
    }
    #endif

    /// Yields the bytes currently writable (`bytesWritable` = `capacity` - `writerIndex`). Before reading those bytes you must first
    /// write to them otherwise you will trigger undefined behaviour. The writer index will remain unchanged.
    ///
    /// - Note: In almost all cases you should use `writeWithUnsafeMutableBytes` which will move the write pointer instead of this method
    ///
    /// - warning: Do not escape the pointer from the closure for later use.
    ///
    /// - Parameters:
    ///   - body: The closure that will accept the yielded bytes and return the number of bytes written.
    /// - Returns: The number of bytes written.
    @inlinable
    public mutating func withUnsafeMutableWritableBytes<T>(
        _ body: (UnsafeMutableRawBufferPointer) throws -> T
    ) rethrows -> T {
        self._copyStorageAndRebaseIfNeeded()
        return try body(.init(rebasing: self._slicedStorageBuffer.dropFirst(self.writerIndex)))

Review comment (suggestion):
```suggestion
        initializingWith initializer: (_ span: inout OutputRawSpan) throws -> Void
```

Code after:
    public var readableBytesSpan: RawSpan {
        @_lifetime(borrow self)
        borrowing get {
            let range = Range<Int>(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex))
            return _overrideLifetime(RawSpan(_unsafeBytes: self._slicedStorageBuffer[range]), borrowing: self)
        }
    }

    /// Provides mutable access to the readable bytes of this buffer.
    @inlinable
    @available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
    public var mutableReadableBytesSpan: MutableRawSpan {
        @_lifetime(&self)
        mutating get {
            self._copyStorageAndRebaseIfNeeded()
            let range = Range<Int>(uncheckedBounds: (lower: self.readerIndex, upper: self.writerIndex))
            return _overrideLifetime(MutableRawSpan(_unsafeBytes: self._slicedStorageBuffer[range]), mutating: &self)
        }
    }

    /// Enables high-performance low-level appending into the writable section of this buffer.
    ///
    /// The writer index will be advanced by the number of bytes written into the
    /// `OutputRawSpan`.
    ///
    /// - parameters:
    ///     - minimumWritableBytes: The minimum initial space to allocate for the buffer.
    ///     - initializer: The initializer that will be invoked to initialize the allocated memory.
    @inlinable
    @available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
    public mutating func writeWithOutputRawSpan<ErrorType: Error>(
        minimumWritableBytes: Int,
        initializingWith initializer: (_ span: inout OutputRawSpan) throws(ErrorType) -> Void
    ) throws(ErrorType) {
        try self.writeWithUnsafeMutableBytes(minimumWritableBytes: minimumWritableBytes) { ptr throws(ErrorType) in
            var span = OutputRawSpan(buffer: ptr, initializedCount: 0)
            try initializer(&span)
            return span.byteCount
        }
    }
    #endif

    /// Yields the bytes currently writable (`bytesWritable` = `capacity` - `writerIndex`). Before reading those bytes you must first
    /// write to them otherwise you will trigger undefined behaviour. The writer index will remain unchanged.
    ///
    /// - Note: In almost all cases you should use `writeWithUnsafeMutableBytes` which will move the write pointer instead of this method
    ///
    /// - warning: Do not escape the pointer from the closure for later use.
    ///
    /// - Parameters:
    ///   - body: The closure that will accept the yielded bytes and return the number of bytes written.