Skip to content

Commit b7ac241

Browse files
ebainvillemoiseev
authored andcommitted
made enums RawRepresentable, added availability for all top-level declarations, cosmetic changes
(cherry picked from commit 1b6f112)
1 parent f792724 commit b7ac241

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

stdlib/public/Darwin/Compression/Compression.swift

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import Foundation
1515
@_exported import Compression
1616

1717
/// Compression algorithms, wraps the C API constants.
18-
public enum Algorithm: CaseIterable {
18+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
19+
public enum Algorithm: CaseIterable, RawRepresentable {
1920

2021
/// LZFSE
2122
case lzfse
@@ -29,6 +30,16 @@ public enum Algorithm: CaseIterable {
2930
/// LZMA in a XZ container
3031
case lzma
3132

33+
public init?(rawValue: compression_algorithm) {
34+
switch rawValue {
35+
case COMPRESSION_LZFSE: self = .lzfse
36+
case COMPRESSION_ZLIB: self = .zlib
37+
case COMPRESSION_LZ4: self = .lz4
38+
case COMPRESSION_LZMA: self = .lzma
39+
default: return nil
40+
}
41+
}
42+
3243
public var rawValue: compression_algorithm {
3344
switch self {
3445
case .lzfse: return COMPRESSION_LZFSE
@@ -39,26 +50,24 @@ public enum Algorithm: CaseIterable {
3950
}
4051
}
4152

42-
/// Compression errors
43-
public enum FilterError: Error {
44-
/// Filter failed to initialize,
45-
/// invalid internal state,
46-
/// invalid parameters
47-
case invalidState
48-
49-
/// Invalid data in a call to compression_stream_process,
50-
/// non-empty write after an output filter has been finalized
51-
case invalidData
52-
}
53-
5453
/// Compression filter direction of operation, compress/decompress
55-
public enum FilterOperation {
54+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
55+
public enum FilterOperation: RawRepresentable {
56+
5657
/// Compress raw data to a compressed payload
5758
case compress
5859

5960
/// Decompress a compressed payload to raw data
6061
case decompress
6162

63+
public init?(rawValue: compression_stream_operation) {
64+
switch rawValue {
65+
case COMPRESSION_STREAM_ENCODE: self = .compress
66+
case COMPRESSION_STREAM_DECODE: self = .decompress
67+
default: return nil
68+
}
69+
}
70+
6271
public var rawValue: compression_stream_operation {
6372
switch self {
6473
case .compress: return COMPRESSION_STREAM_ENCODE
@@ -67,6 +76,20 @@ public enum FilterOperation {
6776
}
6877
}
6978

79+
/// Compression errors
80+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
81+
public enum FilterError: Error {
82+
83+
/// Filter failed to initialize,
84+
/// or invalid internal state,
85+
/// or invalid parameters
86+
case invalidState
87+
88+
/// Invalid data in a call to compression_stream_process,
89+
/// or non-empty write after an output filter has been finalized
90+
case invalidData
91+
}
92+
7093
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
7194
extension compression_stream {
7295

@@ -94,7 +117,7 @@ public class OutputFilter {
94117
private var _stream: compression_stream
95118
private var _buf: UnsafeMutablePointer<UInt8>
96119
private let _bufCapacity: Int
97-
private let _writeFunc: (Data?) throws -> ()
120+
private let _writeFunc: (Data?) throws -> Void
98121
private var _finalized: Bool = false
99122

100123
/// Initialize an output filter
@@ -110,7 +133,7 @@ public class OutputFilter {
110133
_ operation: FilterOperation,
111134
using algorithm: Algorithm,
112135
bufferCapacity: Int = 65536,
113-
writingTo writeFunc: @escaping (Data?) throws -> ()
136+
writingTo writeFunc: @escaping (Data?) throws -> Void
114137
) throws {
115138
_stream = try compression_stream(operation: operation, algorithm: algorithm)
116139
_buf = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferCapacity)
@@ -141,7 +164,7 @@ public class OutputFilter {
141164
try region.withUnsafeBytes { (raw_src_ptr: UnsafeRawBufferPointer) in
142165
_stream.src_size = region.count
143166
_stream.src_ptr = raw_src_ptr.baseAddress!.assumingMemoryBound(to: UInt8.self)
144-
while (_stream.src_size > 0) { _ = try process(finalizing: false) }
167+
while (_stream.src_size > 0) { _ = try _process(finalizing: false) }
145168
}
146169
}
147170
}
@@ -160,7 +183,7 @@ public class OutputFilter {
160183
// Finalize stream
161184
_stream.src_size = 0
162185
var status = COMPRESSION_STATUS_OK
163-
while (status != COMPRESSION_STATUS_END) { status = try process(finalizing: true) }
186+
while (status != COMPRESSION_STATUS_END) { status = try _process(finalizing: true) }
164187

165188
// Update state
166189
_finalized = true
@@ -181,7 +204,7 @@ public class OutputFilter {
181204

182205
// Call compression_stream_process with current src, and dst set to _buf, then write output to the closure
183206
// Return status
184-
private func process(finalizing finalize: Bool) throws -> compression_status {
207+
private func _process(finalizing finalize: Bool) throws -> compression_status {
185208
// Process current input, and write to buf
186209
_stream.dst_ptr = _buf
187210
_stream.dst_size = _bufCapacity
@@ -212,6 +235,7 @@ public class InputFilter<D: DataProtocol> {
212235
private var _remaining: Int // total bytes remaining to process in _data
213236
private var _regionIndex: D.Regions.Index // region being read in _data
214237
private var _regionRemaining: Int // remaining bytes to read in region being read in _data
238+
215239
public init(_ data: D) throws {
216240
_data = data
217241
_remaining = _data.count

0 commit comments

Comments
 (0)