Skip to content

Commit 05b9f1c

Browse files
authored
add closable protocol (#160)
motivation: marker protocol for entitites that manage resources and need to be closed to to cleanly free such resources changes: * define Closable protocol * conform WritableByteStrea to Closable * conform PersistenceCache to Closable
1 parent 3f112d1 commit 05b9f1c

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

Sources/TSCBasic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add_library(TSCBasic
1111
ByteString.swift
1212
CStringArray.swift
1313
CacheableSequence.swift
14+
Closable.swift
1415
CodableResult.swift
1516
CollectionAlgorithms.swift
1617
CollectionExtensions.swift

Sources/TSCBasic/Closable.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2020 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
/// Closable entity is one that manages underlying resources and needs to be closed for cleanup
12+
/// The intent of this method is for the sole owner of the refernece/handle of the resource to close it completely, comapred to releasing a shared resource.
13+
public protocol Closable {
14+
func close() throws
15+
}

Sources/TSCBasic/WritableByteStream.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public protocol ByteStreamable {
4343
///
4444
/// would write each item in the list to the stream, separating them with a
4545
/// space.
46-
public protocol WritableByteStream: class, TextOutputStream {
46+
public protocol WritableByteStream: class, TextOutputStream, Closable {
4747
/// The current offset within the output stream.
4848
var position: Int { get }
4949

@@ -55,9 +55,6 @@ public protocol WritableByteStream: class, TextOutputStream {
5555

5656
/// Flush the stream's buffer.
5757
func flush()
58-
59-
/// Signal that the byte stream will not be used further and should be closed.
60-
func close() throws
6158
}
6259

6360
// Default noop implementation of close to avoid source-breaking downstream dependents with the addition of the close

Sources/TSCUtility/PersistenceCache.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public protocol PersistentCacheProtocol {
1919
}
2020

2121
/// SQLite backed persistent cache.
22-
public final class SQLiteBackedPersistentCache: PersistentCacheProtocol {
22+
public final class SQLiteBackedPersistentCache: PersistentCacheProtocol, Closable {
2323
let db: SQLite
2424

2525
init(db: SQLite) throws {
@@ -37,7 +37,11 @@ public final class SQLiteBackedPersistentCache: PersistentCacheProtocol {
3737
}
3838

3939
deinit {
40-
try? db.close()
40+
try? self.close()
41+
}
42+
43+
public func close() throws {
44+
try db.close()
4145
}
4246

4347
public convenience init(cacheFilePath: AbsolutePath) throws {

0 commit comments

Comments
 (0)