Skip to content

Protocol

ContiguousBytes

iOS 8.0+ iPadOS 8.0+ Mac Catalyst 8.0+ macOS 10.10+ tvOS 9.0+ watchOS 2.0+
A protocol that declares the type offers direct access to the underlying raw bytes in a contiguous manner.
protocol ContiguousBytes : ~Copyable, ~Escapable

Overview

This protocol predates the introduction of the RawSpan type in the standard library. Types that conform to this protocol will generally provide a property (often called bytes) that produces a RawSpan, which is easier to use than the closure-based APIs in this protocol. Therefore, new code should generally be written to use RawSpan rather than ContiguousBytes.

Existing functions that use ContiguousBytes to accept raw bytes can be generalized to also support directly passing a RawSpan or Span<UInt8> by suppressing the implicit Copyable and Escapable requirements:

func encrypt<Bytes: ContiguousBytes>(_ bytes: Bytes) -> [UInt8]
        where Bytes: ~Copyable, Bytes: ~Escapable {
    return bytes.withBytes { rawSpan in ... }
}

Topics

Instance Methods

Relationships

Conforming Types