Skip to content

Commit 0e32e38

Browse files
committed
NFC: Rename OutputByteStream to WritableByteStream for a more semantic meaning of the type.
1 parent f567f9c commit 0e32e38

11 files changed

+71
-68
lines changed

Sources/TSCBasic/ByteString.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Foundation
2121
/// strings, we wish to retain the flexibility to micro-optimize the memory
2222
/// allocation of the storage (for example, by inlining the storage for small
2323
/// strings or and by eliminating wasted space in growable arrays). For
24-
/// construction of byte arrays, clients should use the `OutputByteStream` class
24+
/// construction of byte arrays, clients should use the `WritableByteStream` class
2525
/// and then convert to a `ByteString` when complete.
2626
public struct ByteString: ExpressibleByArrayLiteral, Hashable {
2727
/// The buffer contents.
@@ -142,7 +142,7 @@ extension ByteString: CustomStringConvertible {
142142
/// ByteStreamable conformance for a ByteString.
143143
extension ByteString: ByteStreamable {
144144
@inlinable
145-
public func write(to stream: OutputByteStream) {
145+
public func write(to stream: WritableByteStream) {
146146
stream.write(_bytes)
147147
}
148148
}

Sources/TSCBasic/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ add_library(TSCBasic
3333
ObjectIdentifierProtocol.swift
3434
OrderedDictionary.swift
3535
OrderedSet.swift
36-
OutputByteStream.swift
36+
WritableByteStream.swift
3737
Path.swift
3838
PathShims.swift
3939
Process.swift

Sources/TSCBasic/FileSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public extension FileSystem {
230230
}
231231

232232
/// Write to a file from a stream producer.
233-
func writeFileContents(_ path: AbsolutePath, body: (OutputByteStream) -> Void) throws {
233+
func writeFileContents(_ path: AbsolutePath, body: (WritableByteStream) -> Void) throws {
234234
let contents = BufferedOutputByteStream()
235235
body(contents)
236236
try createDirectory(path.parentDirectory, recursive: true)

Sources/TSCBasic/JSON.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ extension JSON {
115115

116116
/// Support writing to a byte stream.
117117
extension JSON: ByteStreamable {
118-
public func write(to stream: OutputByteStream) {
118+
public func write(to stream: WritableByteStream) {
119119
write(to: stream, indent: nil)
120120
}
121121

122-
public func write(to stream: OutputByteStream, indent: Int?) {
122+
public func write(to stream: WritableByteStream, indent: Int?) {
123123
func indentStreamable(offset: Int? = nil) -> ByteStreamable {
124124
return Format.asRepeating(string: " ", count: indent.flatMap({ $0 + (offset ?? 0) }) ?? 0)
125125
}

Sources/TSCBasic/TerminalController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public final class TerminalController {
5858
}
5959

6060
/// Pointer to output stream to operate on.
61-
private var stream: OutputByteStream
61+
private var stream: WritableByteStream
6262

6363
/// Width of the terminal.
6464
public let width: Int
@@ -73,7 +73,7 @@ public final class TerminalController {
7373
private let boldString = "\u{001B}[1m"
7474

7575
/// Constructs the instance if the stream is a tty.
76-
public init?(stream: OutputByteStream) {
76+
public init?(stream: WritableByteStream) {
7777
let realStream = (stream as? ThreadSafeOutputByteStream)?.stream ?? stream
7878

7979
// Make sure it is a file stream and it is tty.
@@ -188,7 +188,7 @@ public final class TerminalController {
188188
return stream.bytes.description
189189
}
190190

191-
private func writeWrapped(_ string: String, inColor color: Color, bold: Bool = false, stream: OutputByteStream) {
191+
private func writeWrapped(_ string: String, inColor color: Color, bold: Bool = false, stream: WritableByteStream) {
192192
// Don't wrap if string is empty or color is no color.
193193
guard !string.isEmpty && color != .noColor else {
194194
stream <<< string

Sources/TSCBasic/OutputByteStream.swift renamed to Sources/TSCBasic/WritableyteStream.swift

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private func hexdigit(_ value: UInt8) -> UInt8 {
1818

1919
/// Describes a type which can be written to a byte stream.
2020
public protocol ByteStreamable {
21-
func write(to stream: OutputByteStream)
21+
func write(to stream: WritableByteStream)
2222
}
2323

2424
/// An output byte stream.
@@ -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 OutputByteStream: class, TextOutputStream {
46+
public protocol WritableByteStream: class, TextOutputStream {
4747
/// The current offset within the output stream.
4848
var position: Int { get }
4949

@@ -57,7 +57,10 @@ public protocol OutputByteStream: class, TextOutputStream {
5757
func flush()
5858
}
5959

60-
extension OutputByteStream {
60+
// Public alias to the old name to not introduce API compatibility.
61+
public typealias OutputByteStream = WritableByteStream
62+
63+
extension WritableByteStream {
6164
/// Write a sequence of bytes to the buffer.
6265
public func write<S: Sequence>(sequence: S) where S.Iterator.Element == UInt8 {
6366
// Iterate the sequence and append byte by byte since sequence's append
@@ -124,12 +127,12 @@ extension OutputByteStream {
124127
}
125128
}
126129

127-
/// The `OutputByteStream` base class.
130+
/// The `WritableByteStream` base class.
128131
///
129-
/// This class provides a base and efficient implementation of the `OutputByteStream`
132+
/// This class provides a base and efficient implementation of the `WritableByteStream`
130133
/// protocol. It can not be used as is-as subclasses as several functions need to be
131134
/// implemented in subclasses.
132-
public class _OutputByteStreamBase: OutputByteStream {
135+
public class _WritableByteStreamBase: WritableByteStream {
133136
/// If buffering is enabled
134137
@usableFromInline let _buffered : Bool
135138

@@ -149,7 +152,7 @@ public class _OutputByteStreamBase: OutputByteStream {
149152

150153
// When not buffered we still reserve 1 byte, as it is used by the
151154
// by the single byte write() variant.
152-
self._buffer.reserveCapacity(buffered ? _OutputByteStreamBase.bufferSize : 1)
155+
self._buffer.reserveCapacity(buffered ? _WritableByteStreamBase.bufferSize : 1)
153156
}
154157

155158
// MARK: Data Access API
@@ -268,13 +271,13 @@ public class _OutputByteStreamBase: OutputByteStream {
268271

269272
/// The thread-safe wrapper around output byte streams.
270273
///
271-
/// This class wraps any `OutputByteStream` conforming type to provide a type-safe
272-
/// access to its operations. If the provided stream inherits from `_OutputByteStreamBase`,
274+
/// This class wraps any `WritableByteStream` conforming type to provide a type-safe
275+
/// access to its operations. If the provided stream inherits from `_WritableByteStreamBase`,
273276
/// it will also ensure it is type-safe will all other `ThreadSafeOutputByteStream` instances
274277
/// around the same stream.
275-
public final class ThreadSafeOutputByteStream: OutputByteStream {
278+
public final class ThreadSafeOutputByteStream: WritableByteStream {
276279
private static let defaultQueue = DispatchQueue(label: "org.swift.swiftpm.basic.thread-safe-output-byte-stream")
277-
public let stream: OutputByteStream
280+
public let stream: WritableByteStream
278281
private let queue: DispatchQueue
279282

280283
public var position: Int {
@@ -283,9 +286,9 @@ public final class ThreadSafeOutputByteStream: OutputByteStream {
283286
}
284287
}
285288

286-
public init(_ stream: OutputByteStream) {
289+
public init(_ stream: WritableByteStream) {
287290
self.stream = stream
288-
self.queue = (stream as? _OutputByteStreamBase)?.queue ?? ThreadSafeOutputByteStream.defaultQueue
291+
self.queue = (stream as? _WritableByteStreamBase)?.queue ?? ThreadSafeOutputByteStream.defaultQueue
289292
}
290293

291294
public func write(_ byte: UInt8) {
@@ -331,73 +334,73 @@ precedencegroup StreamingPrecedence {
331334
// FIXME: This override shouldn't be necesary but removing it causes a 30% performance regression. This problem is
332335
// tracked by the following bug: https://bugs.swift.org/browse/SR-8535
333336
@discardableResult
334-
public func <<< (stream: OutputByteStream, value: ArraySlice<UInt8>) -> OutputByteStream {
337+
public func <<< (stream: WritableByteStream, value: ArraySlice<UInt8>) -> WritableByteStream {
335338
value.write(to: stream)
336339
return stream
337340
}
338341

339342
@discardableResult
340-
public func <<< (stream: OutputByteStream, value: ByteStreamable) -> OutputByteStream {
343+
public func <<< (stream: WritableByteStream, value: ByteStreamable) -> WritableByteStream {
341344
value.write(to: stream)
342345
return stream
343346
}
344347

345348
@discardableResult
346-
public func <<< (stream: OutputByteStream, value: CustomStringConvertible) -> OutputByteStream {
349+
public func <<< (stream: WritableByteStream, value: CustomStringConvertible) -> WritableByteStream {
347350
value.description.write(to: stream)
348351
return stream
349352
}
350353

351354
@discardableResult
352-
public func <<< (stream: OutputByteStream, value: ByteStreamable & CustomStringConvertible) -> OutputByteStream {
355+
public func <<< (stream: WritableByteStream, value: ByteStreamable & CustomStringConvertible) -> WritableByteStream {
353356
value.write(to: stream)
354357
return stream
355358
}
356359

357360
extension UInt8: ByteStreamable {
358-
public func write(to stream: OutputByteStream) {
361+
public func write(to stream: WritableByteStream) {
359362
stream.write(self)
360363
}
361364
}
362365

363366
extension Character: ByteStreamable {
364-
public func write(to stream: OutputByteStream) {
367+
public func write(to stream: WritableByteStream) {
365368
stream.write(String(self))
366369
}
367370
}
368371

369372
extension String: ByteStreamable {
370-
public func write(to stream: OutputByteStream) {
373+
public func write(to stream: WritableByteStream) {
371374
stream.write(self.utf8)
372375
}
373376
}
374377

375378
extension Substring: ByteStreamable {
376-
public func write(to stream: OutputByteStream) {
379+
public func write(to stream: WritableByteStream) {
377380
stream.write(self.utf8)
378381
}
379382
}
380383

381384
extension StaticString: ByteStreamable {
382-
public func write(to stream: OutputByteStream) {
385+
public func write(to stream: WritableByteStream) {
383386
withUTF8Buffer { stream.write($0) }
384387
}
385388
}
386389

387390
extension Array: ByteStreamable where Element == UInt8 {
388-
public func write(to stream: OutputByteStream) {
391+
public func write(to stream: WritableByteStream) {
389392
stream.write(self)
390393
}
391394
}
392395

393396
extension ArraySlice: ByteStreamable where Element == UInt8 {
394-
public func write(to stream: OutputByteStream) {
397+
public func write(to stream: WritableByteStream) {
395398
stream.write(self)
396399
}
397400
}
398401

399402
extension ContiguousArray: ByteStreamable where Element == UInt8 {
400-
public func write(to stream: OutputByteStream) {
403+
public func write(to stream: WritableByteStream) {
401404
stream.write(self)
402405
}
403406
}
@@ -413,7 +416,7 @@ public struct Format {
413416
private struct JSONEscapedBoolStreamable: ByteStreamable {
414417
let value: Bool
415418

416-
func write(to stream: OutputByteStream) {
419+
func write(to stream: WritableByteStream) {
417420
stream <<< (value ? "true" : "false")
418421
}
419422
}
@@ -425,7 +428,7 @@ public struct Format {
425428
private struct JSONEscapedIntStreamable: ByteStreamable {
426429
let value: Int
427430

428-
func write(to stream: OutputByteStream) {
431+
func write(to stream: WritableByteStream) {
429432
// FIXME: Diagnose integers which cannot be represented in JSON.
430433
stream <<< value.description
431434
}
@@ -438,7 +441,7 @@ public struct Format {
438441
private struct JSONEscapedDoubleStreamable: ByteStreamable {
439442
let value: Double
440443

441-
func write(to stream: OutputByteStream) {
444+
func write(to stream: WritableByteStream) {
442445
// FIXME: What should we do about NaN, etc.?
443446
//
444447
// FIXME: Is Double.debugDescription the best representation?
@@ -457,7 +460,7 @@ public struct Format {
457460
private struct JSONEscapedStringStreamable: ByteStreamable {
458461
let value: String
459462

460-
func write(to stream: OutputByteStream) {
463+
func write(to stream: WritableByteStream) {
461464
stream <<< UInt8(ascii: "\"")
462465
stream.writeJSONEscaped(value)
463466
stream <<< UInt8(ascii: "\"")
@@ -477,7 +480,7 @@ public struct Format {
477480
private struct JSONEscapedStringListStreamable: ByteStreamable {
478481
let items: [String]
479482

480-
func write(to stream: OutputByteStream) {
483+
func write(to stream: WritableByteStream) {
481484
stream <<< UInt8(ascii: "[")
482485
for (i, item) in items.enumerated() {
483486
if i != 0 { stream <<< "," }
@@ -494,7 +497,7 @@ public struct Format {
494497
private struct JSONEscapedDictionaryStreamable: ByteStreamable {
495498
let items: [String: String]
496499

497-
func write(to stream: OutputByteStream) {
500+
func write(to stream: WritableByteStream) {
498501
stream <<< UInt8(ascii: "{")
499502
for (offset: i, element: (key: key, value: value)) in items.enumerated() {
500503
if i != 0 { stream <<< "," }
@@ -514,7 +517,7 @@ public struct Format {
514517
let items: [T]
515518
let transform: (T) -> String
516519

517-
func write(to stream: OutputByteStream) {
520+
func write(to stream: WritableByteStream) {
518521
stream <<< UInt8(ascii: "[")
519522
for (i, item) in items.enumerated() {
520523
if i != 0 { stream <<< "," }
@@ -532,7 +535,7 @@ public struct Format {
532535
let items: [T]
533536
let separator: String
534537

535-
func write(to stream: OutputByteStream) {
538+
func write(to stream: WritableByteStream) {
536539
for (i, item) in items.enumerated() {
537540
// Add the separator, if necessary.
538541
if i != 0 {
@@ -558,7 +561,7 @@ public struct Format {
558561
let transform: (T) -> ByteStreamable
559562
let separator: String
560563

561-
func write(to stream: OutputByteStream) {
564+
func write(to stream: WritableByteStream) {
562565
for (i, item) in items.enumerated() {
563566
if i != 0 { stream <<< separator }
564567
stream <<< transform(item)
@@ -579,22 +582,22 @@ public struct Format {
579582
self.count = count
580583
}
581584

582-
func write(to stream: OutputByteStream) {
585+
func write(to stream: WritableByteStream) {
583586
for _ in 0..<count {
584587
stream <<< string
585588
}
586589
}
587590
}
588591
}
589592

590-
/// In memory implementation of OutputByteStream.
591-
public final class BufferedOutputByteStream: _OutputByteStreamBase {
593+
/// In memory implementation of WritableByteStream.
594+
public final class BufferedOutputByteStream: _WritableByteStreamBase {
592595

593596
/// Contents of the stream.
594597
private var contents = [UInt8]()
595598

596599
public init() {
597-
// We disable the buffering of the underlying _OutputByteStreamBase as
600+
// We disable the buffering of the underlying _WritableByteStreamBase as
598601
// we are explicitly buffering the whole stream in memory
599602
super.init(buffered: false)
600603
}
@@ -625,7 +628,7 @@ public final class BufferedOutputByteStream: _OutputByteStreamBase {
625628
}
626629

627630
/// Represents a stream which is backed to a file. Not for instantiating.
628-
public class FileOutputByteStream: _OutputByteStreamBase {
631+
public class FileOutputByteStream: _WritableByteStreamBase {
629632

630633
/// Closes the file flushing any buffered data.
631634
public final func close() throws {

Sources/TSCUtility/ArgumentParser.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ public final class ArgumentParser {
888888
}
889889

890890
/// Prints usage text for this parser on the provided stream.
891-
public func printUsage(on stream: OutputByteStream) {
891+
public func printUsage(on stream: WritableByteStream) {
892892
/// Space settings.
893893
let maxWidthDefault = 24
894894
let padding = 2
@@ -905,7 +905,7 @@ public final class ArgumentParser {
905905
}
906906

907907
/// Prints an argument on a stream if it has usage.
908-
func print(formatted argument: String, usage: String, on stream: OutputByteStream) {
908+
func print(formatted argument: String, usage: String, on stream: WritableByteStream) {
909909
// Start with a new line and add some padding.
910910
stream <<< "\n" <<< Format.asRepeating(string: " ", count: padding)
911911
let count = argument.count

0 commit comments

Comments
 (0)