Since FileDescriptor
is not automatically closed on deinit
, I would like to propose adding a property wrapper that automatically closes the wrapped file descriptor.
Here is my first draft:
public protocol Cloesable {
func close() throws
}
extension Optional: Cloesable where Wrapped: Cloesable {
public func close() throws {
try self?.close()
}
}
/// Automatically closes the wrapped file descriptor on deinit.
@propertyWrapper public final class AutoClosing<Descriptor: Cloesable> {
public var wrappedValue: Descriptor
public init(wrappedValue: Descriptor) {
self.wrappedValue = wrappedValue
}
deinit {
try? wrappedValue.close()
}
}
try? wrappedValue.close()
is obviously not ideal here.