Avatar
Mut 自体に手を加えても良いならこうやって property wrapper にもできるんですけどね. struct S: ~Copyable, ~Escapable { @Mut var prop: Int @_lifetime(&x) init(x: inout Int) { self._prop = .init(wrappedValue: &x) } mutating func countUp() { self.prop += 1 } } @propertyWrapper @safe public struct Mut<T: ~Copyable>: ~Copyable, ~Escapable { let _pointer: UnsafeMutablePointer<T> public init(wrappedValue instance: inout T) { unsafe _pointer = .init(bitPattern: 0)! // unsafe _pointer = UnsafeMutablePointer<T>(Builtin.unprotectedAddressOf(&instance)) } public var wrappedValue: T { unsafeAddress { unsafe UnsafePointer<T>(_pointer) } @_lifetime(self: copy self) unsafeMutableAddress { unsafe _pointer } } }
naruhodo 1