@@ -381,6 +381,49 @@ extension Unsafe${Mutable}RawBufferPointer {
381381 return baseAddress!. load ( fromByteOffset: offset, as: T . self)
382382 }
383383
384+ /// Returns a new instance of the given type, constructed from the raw memory
385+ /// at the specified offset.
386+ ///
387+ /// This function only supports loading trivial types.
388+ /// A trivial type does not contain any reference-counted property
389+ /// within its in-memory stored representation.
390+ /// The memory at `offset` bytes into the buffer must be laid out
391+ /// identically to the in-memory representation of `T`.
392+ ///
393+ /// You can use this method to create new values from the buffer pointer's
394+ /// underlying bytes. The following example creates two new `Int32`
395+ /// instances from the memory referenced by the buffer pointer `someBytes`.
396+ /// The bytes for `a` are copied from the first four bytes of `someBytes`,
397+ /// and the bytes for `b` are copied from the next four bytes.
398+ ///
399+ /// let a = someBytes.load(as: Int32.self)
400+ /// let b = someBytes.load(fromByteOffset: 4, as: Int32.self)
401+ ///
402+ /// The memory to read for the new instance must not extend beyond the buffer
403+ /// pointer's memory region---that is, `offset + MemoryLayout<T>.size` must
404+ /// be less than or equal to the buffer pointer's `count`.
405+ ///
406+ /// - Parameters:
407+ /// - offset: The offset, in bytes, into the buffer pointer's memory at
408+ /// which to begin reading data for the new instance. The buffer pointer
409+ /// plus `offset` must be properly aligned for accessing an instance of
410+ /// type `T`. The default is zero.
411+ /// - type: The type to use for the newly constructed instance. The memory
412+ /// must be initialized to a value of a type that is layout compatible
413+ /// with `type`.
414+ /// - Returns: A new instance of type `T`, copied from the buffer pointer's
415+ /// memory.
416+ @_alwaysEmitIntoClient
417+ public func loadUnaligned< T> (
418+ fromByteOffset offset: Int = 0 ,
419+ as type: T . Type
420+ ) -> T {
421+ _debugPrecondition ( offset >= 0 , " ${Self}.load with negative offset " )
422+ _debugPrecondition ( offset + MemoryLayout < T > . size <= self . count,
423+ " ${Self}.load out of bounds " )
424+ return baseAddress!. loadUnaligned ( fromByteOffset: offset, as: T . self)
425+ }
426+
384427% if mutable:
385428 /// Stores a value's bytes into the buffer pointer's raw memory at the
386429 /// specified byte offset.
0 commit comments