Skip to content

Commit

Permalink
Remove Array.toUnsafeBufferPointer
Browse files Browse the repository at this point in the history
This commit removes the `Array.toUnsafeBufferPointer` usage from the
documentation.
This was a method that we used to have but removed in a previous commit.

Swift pointers have lifetimes that can be shorter than the lifetime of
the object that they are pointing to, so the returned pointer could
unpredictably become a dangling pointer.

The documentation has been updated with an alternative approach to
passing a Swift buffer pointer to Rust.
  • Loading branch information
chinedufn committed Jan 26, 2024
1 parent d527f32 commit 0614ba7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions book/src/built-in/vec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ func testMakeAVec () {

func testMakeAnotherVec () {
let initial: [Int16] = [3, 5, 7]
let vec: RustVec = get_vec_from_rust(initial.toUnsafeBufferPointer())
initial.withUnsafeBufferPointer({ initalPtr in
let vec: RustVec = get_vec_from_rust(initialPtr)

XCTAssertEqual(vec.len(), 3);
XCTAssertEqual(vec.len(), 3);

for (index, value) in vec.enumerate() {
XCTAssertEqual(value, initial[index])
}
for (index, value) in vec.enumerate() {
XCTAssertEqual(value, initial[index])
}
});
}
```

0 comments on commit 0614ba7

Please sign in to comment.