From bb377c171b7978789eea43092e557e44c4b1043f Mon Sep 17 00:00:00 2001 From: Dimi Racordon Date: Wed, 20 Sep 2023 11:10:19 +0200 Subject: [PATCH 1/2] Implement a pointee initialization method that doesn't use lambdas --- Library/Hylo/Core/PointerToMutable.hylo | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Library/Hylo/Core/PointerToMutable.hylo b/Library/Hylo/Core/PointerToMutable.hylo index d973d098a..c901f0e6e 100644 --- a/Library/Hylo/Core/PointerToMutable.hylo +++ b/Library/Hylo/Core/PointerToMutable.hylo @@ -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.size()` bytes starting at the address are uninitialized + /// and suitably aligned for `Pointee`. + public fun unsafe_initialize_pointee(_ value: sink Pointee) { + initialize(&(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(_ x: set T, to y: sink T) { + &x = y } From 783a0f1e91e24b3910f8892b722bf98c1c93feac Mon Sep 17 00:00:00 2001 From: Dimi Racordon Date: Wed, 20 Sep 2023 11:19:09 +0200 Subject: [PATCH 2/2] Test pointee initialization without lambdas --- .../TestCases/PointerToMutableTests.hylo | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Tests/LibraryTests/TestCases/PointerToMutableTests.hylo b/Tests/LibraryTests/TestCases/PointerToMutableTests.hylo index 32198a365..844afa613 100644 --- a/Tests/LibraryTests/TestCases/PointerToMutableTests.hylo +++ b/Tests/LibraryTests/TestCases/PointerToMutableTests.hylo @@ -24,16 +24,25 @@ fun test_advance() { precondition(!(f == e)) } -fun test_pointee() { +fun test_initialize_pointee_lambda() { let p = PointerToMutable.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.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() }