UnsafeMutableRawBufferPointer
声明
@frozen struct UnsafeMutableRawBufferPointer
概述
您可以在低级操作中使用UnsafeMutableRawBufferPointer
实例来消除唯一性检查和释放模式边界检查。边界检查始终在调试模式下执行。
UnsafeMutableRawBufferPointer
实例是内存区域中原始字节的视图。内存中的每个字节都被视为一个UInt8
值,与该内存中保存的值的类型无关。通过原始缓冲区读取和写入内存是无类型操作。访问此集合的字节不会将底层内存绑定到 UInt8
。
除了集合接口,UnsafeMutableRawBufferPointer
实例还支持 UnsafeMutableRawPointer
提供的以下方法,包括调试模式下的边界检查:
-
load(fromByteOffset:as:)
-
storeBytes(of:toByteOffset:as:)
-
copyMemory(from:)
要通过类型化操作访问底层内存,内存必须绑定到普通类型。
UnsafeMutableRawBufferPointer 语义
UnsafeMutableRawBufferPointer
实例是内存视图,并不拥有它引用的内存。复制UnsafeMutableRawBufferPointer
类型的变量或常量不会复制底层内存。但是,使用 UnsafeMutableRawBufferPointer
实例初始化另一个集合会将字节从引用的内存复制到新集合中。
以下示例使用 someBytes
(一个 UnsafeMutableRawBufferPointer
实例)来演示分配缓冲区指针和使用缓冲区指针作为另一个集合元素的源之间的区别。在这里,对 destBytes
的赋值创建了一个新的、非拥有的缓冲区指针,覆盖了 someBytes
引用的内存的前 n
字节——没有任何内容被复制:
var destBytes = someBytes[0..<n]
接下来,destBytes
引用的字节被复制到 byteArray
一个新的 [UInt8]
数组中,然后将 someBytes
的其余部分附加到 byteArray
:
var byteArray: [UInt8] = Array(destBytes)
byteArray += someBytes[n..<someBytes.count]
分配给 UnsafeMutableRawBufferPointer
实例的范围下标会将字节复制到内存中。 someBytes
引用的内存的下一个 n
字节复制到此代码中:
destBytes[0..<n] = someBytes[n..<(n + n)]
可用版本
相关用法
- Swift UnsafeMutableRawBufferPointer shuffle()用法及代码示例
- Swift UnsafeMutableRawBufferPointer compactMap(_:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer contains(where:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer allSatisfy(_:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer map(_:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer starts(with:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer isEmpty用法及代码示例
- Swift UnsafeMutableRawBufferPointer sorted()用法及代码示例
- Swift UnsafeMutableRawBufferPointer indices用法及代码示例
- Swift UnsafeMutableRawBufferPointer randomElement(using:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer reduce(_:_:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer sort(by:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer lexicographicallyPrecedes(_:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer forEach(_:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer suffix(from:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer partition(by:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer first用法及代码示例
- Swift UnsafeMutableRawBufferPointer flatMap(_:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer firstIndex(where:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer reversed()用法及代码示例
- Swift UnsafeMutableRawBufferPointer min()用法及代码示例
- Swift UnsafeMutableRawBufferPointer prefix(_:)用法及代码示例
- Swift UnsafeMutableRawBufferPointer reverse()用法及代码示例
- Swift UnsafeMutableRawBufferPointer filter(_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 UnsafeMutableRawBufferPointer。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。