@@ -27,19 +27,19 @@ public protocol ByteStreamable {
27
27
/// different output destinations, e.g., a file or an in memory buffer. This is
28
28
/// loosely modeled on LLVM's llvm::raw_ostream class.
29
29
///
30
- /// The stream is generally used in conjunction with the custom streaming
31
- /// operator '<<<'. For example:
30
+ /// The stream is generally used in conjunction with the `appending` function.
31
+ /// For example:
32
32
///
33
33
/// let stream = BufferedOutputByteStream()
34
- /// stream <<< "Hello, world!"
34
+ /// stream.appending( "Hello, world!")
35
35
///
36
36
/// would write the UTF8 encoding of "Hello, world!" to the stream.
37
37
///
38
38
/// The stream accepts a number of custom formatting operators which are defined
39
39
/// in the `Format` struct (used for namespacing purposes). For example:
40
40
///
41
41
/// let items = ["hello", "world"]
42
- /// stream <<< Format.asSeparatedList(items, separator: " ")
42
+ /// stream.appending( Format.asSeparatedList(items, separator: " ") )
43
43
///
44
44
/// would write each item in the list to the stream, separating them with a
45
45
/// space.
@@ -137,6 +137,34 @@ extension WritableByteStream {
137
137
}
138
138
}
139
139
}
140
+
141
+ // MARK: helpers that return `self`
142
+
143
+ // FIXME: This override shouldn't be necesary but removing it causes a 30% performance regression. This problem is
144
+ // tracked by the following bug: https://bugs.swift.org/browse/SR-8535
145
+ @discardableResult
146
+ public func send( _ value: ArraySlice < UInt8 > ) -> WritableByteStream {
147
+ value. write ( to: self )
148
+ return self
149
+ }
150
+
151
+ @discardableResult
152
+ public func send( _ value: ByteStreamable ) -> WritableByteStream {
153
+ value. write ( to: self )
154
+ return self
155
+ }
156
+
157
+ @discardableResult
158
+ public func send( _ value: CustomStringConvertible ) -> WritableByteStream {
159
+ value. description. write ( to: self )
160
+ return self
161
+ }
162
+
163
+ @discardableResult
164
+ public func send( _ value: ByteStreamable & CustomStringConvertible ) -> WritableByteStream {
165
+ value. write ( to: self )
166
+ return self
167
+ }
140
168
}
141
169
142
170
/// The `WritableByteStream` base class.
@@ -366,24 +394,29 @@ precedencegroup StreamingPrecedence {
366
394
367
395
// FIXME: This override shouldn't be necesary but removing it causes a 30% performance regression. This problem is
368
396
// tracked by the following bug: https://bugs.swift.org/browse/SR-8535
397
+
398
+ @available ( * , deprecated, message: " use send(_:) function on WritableByteStream instead " )
369
399
@discardableResult
370
400
public func <<< ( stream: WritableByteStream , value: ArraySlice < UInt8 > ) -> WritableByteStream {
371
401
value. write ( to: stream)
372
402
return stream
373
403
}
374
404
405
+ @available ( * , deprecated, message: " use send(_:) function on WritableByteStream instead " )
375
406
@discardableResult
376
407
public func <<< ( stream: WritableByteStream , value: ByteStreamable ) -> WritableByteStream {
377
408
value. write ( to: stream)
378
409
return stream
379
410
}
380
411
412
+ @available ( * , deprecated, message: " use send(_:) function on WritableByteStream instead " )
381
413
@discardableResult
382
414
public func <<< ( stream: WritableByteStream , value: CustomStringConvertible ) -> WritableByteStream {
383
415
value. description. write ( to: stream)
384
416
return stream
385
417
}
386
418
419
+ @available ( * , deprecated, message: " use send(_:) function on WritableByteStream instead " )
387
420
@discardableResult
388
421
public func <<< ( stream: WritableByteStream , value: ByteStreamable & CustomStringConvertible ) -> WritableByteStream {
389
422
value. write ( to: stream)
@@ -450,7 +483,7 @@ public struct Format {
450
483
let value : Bool
451
484
452
485
func write( to stream: WritableByteStream ) {
453
- stream <<< ( value ? " true " : " false " )
486
+ stream. send ( value ? " true " : " false " )
454
487
}
455
488
}
456
489
@@ -463,7 +496,7 @@ public struct Format {
463
496
464
497
func write( to stream: WritableByteStream ) {
465
498
// FIXME: Diagnose integers which cannot be represented in JSON.
466
- stream <<< value. description
499
+ stream. send ( value. description)
467
500
}
468
501
}
469
502
@@ -478,7 +511,7 @@ public struct Format {
478
511
// FIXME: What should we do about NaN, etc.?
479
512
//
480
513
// FIXME: Is Double.debugDescription the best representation?
481
- stream <<< value. debugDescription
514
+ stream. send ( value. debugDescription)
482
515
}
483
516
}
484
517
@@ -494,9 +527,9 @@ public struct Format {
494
527
let value : String
495
528
496
529
func write( to stream: WritableByteStream ) {
497
- stream <<< UInt8 ( ascii: " \" " )
530
+ stream. send ( UInt8 ( ascii: " \" " ) )
498
531
stream. writeJSONEscaped ( value)
499
- stream <<< UInt8 ( ascii: " \" " )
532
+ stream. send ( UInt8 ( ascii: " \" " ) )
500
533
}
501
534
}
502
535
@@ -514,12 +547,12 @@ public struct Format {
514
547
let items : [ String ]
515
548
516
549
func write( to stream: WritableByteStream ) {
517
- stream <<< UInt8 ( ascii: " [ " )
550
+ stream. send ( UInt8 ( ascii: " [ " ) )
518
551
for (i, item) in items. enumerated ( ) {
519
- if i != 0 { stream <<< " , " }
520
- stream <<< Format . asJSON ( item)
552
+ if i != 0 { stream. send ( " , " ) }
553
+ stream. send ( Format . asJSON ( item) )
521
554
}
522
- stream <<< UInt8 ( ascii: " ] " )
555
+ stream. send ( UInt8 ( ascii: " ] " ) )
523
556
}
524
557
}
525
558
@@ -531,12 +564,12 @@ public struct Format {
531
564
let items : [ String : String ]
532
565
533
566
func write( to stream: WritableByteStream ) {
534
- stream <<< UInt8 ( ascii: " { " )
567
+ stream. send ( UInt8 ( ascii: " { " ) )
535
568
for (offset: i, element: ( key: key, value: value) ) in items. enumerated ( ) {
536
- if i != 0 { stream <<< " , " }
537
- stream <<< Format . asJSON ( key) <<< " : " <<< Format . asJSON ( value)
569
+ if i != 0 { stream. send ( " , " ) }
570
+ stream. send ( Format . asJSON ( key) ) . send ( " : " ) . send ( Format . asJSON ( value) )
538
571
}
539
- stream <<< UInt8 ( ascii: " } " )
572
+ stream. send ( UInt8 ( ascii: " } " ) )
540
573
}
541
574
}
542
575
@@ -551,12 +584,12 @@ public struct Format {
551
584
let transform : ( T ) -> String
552
585
553
586
func write( to stream: WritableByteStream ) {
554
- stream <<< UInt8 ( ascii: " [ " )
587
+ stream. send ( UInt8 ( ascii: " [ " ) )
555
588
for (i, item) in items. enumerated ( ) {
556
- if i != 0 { stream <<< " , " }
557
- stream <<< Format . asJSON ( transform ( item) )
589
+ if i != 0 { stream. send ( " , " ) }
590
+ stream. send ( Format . asJSON ( transform ( item) ) )
558
591
}
559
- stream <<< UInt8 ( ascii: " ] " )
592
+ stream. send ( UInt8 ( ascii: " ] " ) )
560
593
}
561
594
}
562
595
@@ -572,10 +605,10 @@ public struct Format {
572
605
for (i, item) in items. enumerated ( ) {
573
606
// Add the separator, if necessary.
574
607
if i != 0 {
575
- stream <<< separator
608
+ stream. send ( separator)
576
609
}
577
610
578
- stream <<< item
611
+ stream. send ( item)
579
612
}
580
613
}
581
614
}
@@ -596,8 +629,8 @@ public struct Format {
596
629
597
630
func write( to stream: WritableByteStream ) {
598
631
for (i, item) in items. enumerated ( ) {
599
- if i != 0 { stream <<< separator }
600
- stream <<< transform ( item)
632
+ if i != 0 { stream. send ( separator) }
633
+ stream. send ( transform ( item) )
601
634
}
602
635
}
603
636
}
@@ -617,7 +650,7 @@ public struct Format {
617
650
618
651
func write( to stream: WritableByteStream ) {
619
652
for _ in 0 ..< count {
620
- stream <<< string
653
+ stream. send ( string)
621
654
}
622
655
}
623
656
}
0 commit comments