Apply suggestions from code review

Co-authored-by: Guillaume Lessard <glessard@users.noreply.github.com>

fix test
This commit is contained in:
Alejandro Alonso
2024-01-16 16:07:54 -08:00
parent e6e87881b8
commit f0a74ecb0d
2 changed files with 6 additions and 6 deletions

View File

@@ -34,12 +34,12 @@ public struct Unmanaged<Instance: AnyObject> {
public static func fromOpaque(
@_nonEphemeral _ value: UnsafeRawPointer
) -> Unmanaged {
// NOTE: `value` is allowed to be a dangling pointer, so
// NOTE: `value` is allowed to represent a dangling reference, so
// this function must not ever try to dereference it. For
// example, it must NOT go through the init(_private:) initializer
// because it requires us to materialize a strong reference to 'Instance'.
// This materialization is enough to convince the compiler to add
// retain/releases which we want to avoid for the opaque pointer functions.
// example, this function must NOT use the init(_private:) initializer
// because doing so requires materializing a strong reference to 'Instance'.
// This materialization would be enough to convince the compiler to add
// retain/releases which must be avoided for the opaque pointer functions.
// 'Unmanaged<Instance>' is layout compatible with 'UnsafeRawPointer' and
// casting to that will not attempt to retain the reference held at 'value'.
unsafeBitCast(value, to: Unmanaged<Instance>.self)

View File

@@ -85,7 +85,7 @@ UnmanagedTests.test("Opaque avoid retain/release") {
// Turn it into a dangling unmanaged reference.
// We expect this not to crash, as this operation isn't
// supposed to dereference the pointer in any way.
let unmanaged = Unmanaged.fromOpaque(ref)
let unmanaged = Unmanaged<Foobar>.fromOpaque(ref)
// Similarly, converting the unmanaged reference back to a
// pointer should not ever try to dereference it either.
let ref2 = unmanaged.toOpaque()