Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions Sources/Testing/ABI/Encoded/ABI.EncodedAttachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ extension ABI.EncodedAttachment: Attachable {
_bytes?.rawValue.count
}

/// An error type that is thrown when ``ABI/EncodedAttachment`` cannot satisfy
/// a request for the underlying attachment's bytes.
fileprivate struct BytesUnavailableError: Error {}

borrowing func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
if let bytes = _bytes?.rawValue {
return try bytes.withUnsafeBytes(body)
Expand Down Expand Up @@ -135,9 +131,3 @@ extension ABI.EncodedAttachment: Attachable {
_preferredName ?? suggestedName
}
}

extension ABI.EncodedAttachment.BytesUnavailableError: CustomStringConvertible {
var description: String {
"The attachment's content could not be deserialized."
}
}
30 changes: 30 additions & 0 deletions Sources/Testing/Attachments/Attachable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public protocol Attachable: ~Copyable {
/// }
borrowing func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R

@_lifetime(borrow self)
borrowing func bytes(for attachment: borrowing Attachment<Self>) throws -> RawSpan

/// Generate a preferred name for the given attachment.
///
/// - Parameters:
Expand All @@ -101,6 +104,18 @@ public protocol Attachable: ~Copyable {
borrowing func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String
}

// MARK: -

/// An error type that is thrown when the testing library cannot satisfy a
/// request for an attachment's bytes.
struct BytesUnavailableError: Error {}

extension BytesUnavailableError: CustomStringConvertible {
var description: String {
"The attachment's content could not be deserialized."
}
}

// MARK: - Default implementations

/// @Metadata {
Expand All @@ -123,6 +138,11 @@ extension Attachable where Self: ~Copyable {
public borrowing func preferredName(for attachment: borrowing Attachment<Self>, basedOn suggestedName: String) -> String {
suggestedName
}

@_lifetime(borrow self)
public borrowing func bytes(for attachment: borrowing Attachment<Self>) throws -> RawSpan {
throw BytesUnavailableError()
}
}

/// @Metadata {
Expand Down Expand Up @@ -202,6 +222,11 @@ extension ContiguousArray<UInt8>: Attachable {
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
try withUnsafeBytes(body)
}

@_lifetime(borrow self)
public borrowing func bytes(for attachment: borrowing Attachment<Self>) throws -> RawSpan {
span.bytes
}
}

/// @Metadata {
Expand All @@ -216,6 +241,11 @@ extension ArraySlice<UInt8>: Attachable {
public func withUnsafeBytes<R>(for attachment: borrowing Attachment<Self>, _ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
try withUnsafeBytes(body)
}

@_lifetime(borrow self)
public borrowing func bytes(for attachment: borrowing Attachment<Self>) throws -> RawSpan {
span.bytes
}
}

/// @Metadata {
Expand Down
14 changes: 14 additions & 0 deletions Sources/Testing/Attachments/Attachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ extension Attachment where AttachableValue: ~Copyable {
@inlinable public borrowing func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) throws -> R {
try attachableValue.withUnsafeBytes(for: self, body)
}

@_lifetime(borrow self)
private func _makeBytes(from attachableValue: borrowing AttachableValue) throws -> RawSpan {
let result = try attachableValue.bytes(for: self)
return _overrideLifetime(result, copying: self)
}

@_spi(Experimental)
public var bytes: RawSpan {
@_lifetime(borrow self)
borrowing get throws {
try _makeBytes(from: _storage.value)
}
}
}

#if !SWT_NO_FILE_IO
Expand Down
Loading