Skip to content

Commit a2b53bb

Browse files
committed
modify consuming methods to call discard self
1 parent 3d63685 commit a2b53bb

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

Sources/System/MachPort.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
167167
/// After this function completes, the Mach.Port is destroyed and no longer
168168
/// usable.
169169
@inlinable
170-
public __consuming func relinquish(
170+
public consuming func relinquish(
171171
) -> (name: mach_port_name_t, context: mach_port_context_t) {
172-
return (name: _name, context: _context)
172+
let destructured = (name: _name, context: _context)
173+
discard self
174+
return destructured
173175
}
174176

175177
/// Remove guard and transfer ownership of the underlying port right to
@@ -187,9 +189,11 @@ extension Mach.Port where RightType == Mach.ReceiveRight {
187189
/// Mach.ReceiveRights. Use relinquish() to avoid the syscall and extract
188190
/// the context value along with the port name.
189191
@inlinable
190-
public __consuming func unguardAndRelinquish() -> mach_port_name_t {
191-
_machPrecondition(mach_port_unguard(mach_task_self_, _name, _context))
192-
return _name
192+
public consuming func unguardAndRelinquish() -> mach_port_name_t {
193+
let (name, context) = (_name, _context)
194+
discard self
195+
_machPrecondition(mach_port_unguard(mach_task_self_, name, context))
196+
return name
193197
}
194198

195199
/// Borrow access to the port name in a block that can perform
@@ -288,8 +292,10 @@ extension Mach.Port where RightType == Mach.SendRight {
288292
/// After this function completes, the Mach.Port is destroyed and no longer
289293
/// usable.
290294
@inlinable
291-
public __consuming func relinquish() -> mach_port_name_t {
292-
return _name
295+
public consuming func relinquish() -> mach_port_name_t {
296+
let name = _name
297+
discard self
298+
return name
293299
}
294300

295301
/// Create another send right from a given send right.
@@ -326,8 +332,10 @@ extension Mach.Port where RightType == Mach.SendOnceRight {
326332
/// After this function completes, the Mach.Port is destroyed and no longer
327333
/// usable.
328334
@inlinable
329-
public __consuming func relinquish() -> mach_port_name_t {
330-
return _name
335+
public consuming func relinquish() -> mach_port_name_t {
336+
let name = _name
337+
discard self
338+
return name
331339
}
332340
}
333341

Tests/SystemTests/MachPortTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ final class MachPortTests: XCTestCase {
9292

9393
let stillOne = refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_SEND)
9494
XCTAssertEqual(stillOne, 1)
95+
96+
recv.withBorrowedName {
97+
let alsoOne = refCountForMachPortName(name: $0, kind: MACH_PORT_RIGHT_RECEIVE)
98+
XCTAssertEqual(alsoOne, 1)
99+
}
95100
}
96101

97102
func testMakeSendCountSettable() throws {

0 commit comments

Comments
 (0)