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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
