Skip to content

Commit

Permalink
If Foundation is available, mark Buffer as ContiguousBytes
Browse files Browse the repository at this point in the history
Useful in other contexts.
  • Loading branch information
helje5 committed Dec 12, 2024
1 parent 84292e7 commit 6a4971e
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions Sources/MacroCore/Buffer/BufferData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// Macro
//
// Created by Helge Heß.
// Copyright © 2020-2023 ZeeZide GmbH. All rights reserved.
// Copyright © 2020-2024 ZeeZide GmbH. All rights reserved.
//

#if canImport(Foundation)

import struct Foundation.Data
import struct Foundation.Data
import protocol Foundation.ContiguousBytes
import NIOCore
import NIOFoundationCompat

Expand All @@ -17,16 +18,32 @@ public extension Buffer {
/**
* Initialize the Buffer with the contents of the given `Data`. Copies the
* bytes.
*
* - Parameters:
* - data: The bytes to copy into the buffer.
*/
@inlinable init(_ data: Data) {
@inlinable
init(_ data: Data) {
self.init(capacity: data.count)
byteBuffer.writeBytes(data)
}

@inlinable var data : Data {
@inlinable
var data : Data {
return byteBuffer.getData(at : byteBuffer.readerIndex,
length : byteBuffer.readableBytes) ?? Data()
}
}

extension Buffer: ContiguousBytes {

@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R)
rethrows -> R
{
return try byteBuffer.readableBytesView.withUnsafeBytes(body)
}

}

#endif // canImport(Foundation)

0 comments on commit 6a4971e

Please sign in to comment.