Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a pointee initialization method that doesn't use lambdas #1022

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Library/Hylo/Core/PointerToMutable.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,19 @@ public extension PointerToMutable where Pointee: Movable {
return base as* (remote sink Pointee)
}

/// Initialize the value at the address represented by `self` to `value`.
///
/// - Requires: the `MemoryLayout<Pointee>.size()` bytes starting at the address are uninitialized
/// and suitably aligned for `Pointee`.
public fun unsafe_initialize_pointee(_ value: sink Pointee) {
initialize<Pointee>(&(base as* (remote set Pointee)), to: value)
}

}

/// Initializes `x` to `y`.
///
/// - Note: This function is a workaround for the lack of `set` bindings (see #925).
fun initialize<T: Movable>(_ x: set T, to y: sink T) {
&x = y
}
15 changes: 12 additions & 3 deletions Tests/LibraryTests/TestCases/PointerToMutableTests.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,25 @@ fun test_advance() {
precondition(!(f == e))
}

fun test_pointee() {
fun test_initialize_pointee_lambda() {
let p = PointerToMutable<Int>.allocate(count: 1)
p.unsafe_initialize_pointee(fun (_ i: set Int) -> Void { &i = 42 })
let y = p.unsafe_pointee()
precondition(42 == y)
precondition(y == 42)
p.deallocate()
}

fun test_initialize_pointee_direct() {
let p = PointerToMutable<Int>.allocate(count: 1)
p.unsafe_initialize_pointee(42)
let y = p.unsafe_pointee()
precondition(y == 42)
p.deallocate()
}

public fun main() {
test_advance_by_bytes()
test_advance()
test_pointee()
test_initialize_pointee_lambda()
test_initialize_pointee_direct()
}
Loading