[Pitch] UnsafePointer.advanced(to:)

I just ran into a use case for this as well, though I agree with others in this thread that "advanced" may not be the best name. A simple subscript works great

extension UnsafePointer {
    
    subscript<T>(_ keyPath: KeyPath<Pointee, T>) -> UnsafePointer<T> {
        let raw = UnsafeRawPointer(self)
        // If a key path is not directly-addressable I consider it programmer error
        let offset = MemoryLayout<Pointee>.offset(of: keyPath)!
        return raw.advanced(by: offset).assumingMemoryBound(to: T.self)
    }
    
}
1 Like