@@ -27,19 +27,19 @@ public protocol ByteStreamable {
2727/// different output destinations, e.g., a file or an in memory buffer. This is
2828/// loosely modeled on LLVM's llvm::raw_ostream class.
2929///
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:
3232///
3333/// let stream = BufferedOutputByteStream()
34- /// stream <<< "Hello, world!"
34+ /// stream.appending( "Hello, world!")
3535///
3636/// would write the UTF8 encoding of "Hello, world!" to the stream.
3737///
3838/// The stream accepts a number of custom formatting operators which are defined
3939/// in the `Format` struct (used for namespacing purposes). For example:
4040///
4141/// let items = ["hello", "world"]
42- /// stream <<< Format.asSeparatedList(items, separator: " ")
42+ /// stream.appending( Format.asSeparatedList(items, separator: " ") )
4343///
4444/// would write each item in the list to the stream, separating them with a
4545/// space.
@@ -137,6 +137,34 @@ extension WritableByteStream {
137137 }
138138 }
139139 }
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+ }
140168}
141169
142170/// The `WritableByteStream` base class.
@@ -366,24 +394,29 @@ precedencegroup StreamingPrecedence {
366394
367395// FIXME: This override shouldn't be necesary but removing it causes a 30% performance regression. This problem is
368396// tracked by the following bug: https://bugs.swift.org/browse/SR-8535
397+
398+ @available ( * , deprecated, message: " use send(_:) function on WritableByteStream instead " )
369399@discardableResult
370400public func <<< ( stream: WritableByteStream , value: ArraySlice < UInt8 > ) -> WritableByteStream {
371401 value. write ( to: stream)
372402 return stream
373403}
374404
405+ @available ( * , deprecated, message: " use send(_:) function on WritableByteStream instead " )
375406@discardableResult
376407public func <<< ( stream: WritableByteStream , value: ByteStreamable ) -> WritableByteStream {
377408 value. write ( to: stream)
378409 return stream
379410}
380411
412+ @available ( * , deprecated, message: " use send(_:) function on WritableByteStream instead " )
381413@discardableResult
382414public func <<< ( stream: WritableByteStream , value: CustomStringConvertible ) -> WritableByteStream {
383415 value. description. write ( to: stream)
384416 return stream
385417}
386418
419+ @available ( * , deprecated, message: " use send(_:) function on WritableByteStream instead " )
387420@discardableResult
388421public func <<< ( stream: WritableByteStream , value: ByteStreamable & CustomStringConvertible ) -> WritableByteStream {
389422 value. write ( to: stream)
@@ -450,7 +483,7 @@ public struct Format {
450483 let value : Bool
451484
452485 func write( to stream: WritableByteStream ) {
453- stream <<< ( value ? " true " : " false " )
486+ stream. send ( value ? " true " : " false " )
454487 }
455488 }
456489
@@ -463,7 +496,7 @@ public struct Format {
463496
464497 func write( to stream: WritableByteStream ) {
465498 // FIXME: Diagnose integers which cannot be represented in JSON.
466- stream <<< value. description
499+ stream. send ( value. description)
467500 }
468501 }
469502
@@ -478,7 +511,7 @@ public struct Format {
478511 // FIXME: What should we do about NaN, etc.?
479512 //
480513 // FIXME: Is Double.debugDescription the best representation?
481- stream <<< value. debugDescription
514+ stream. send ( value. debugDescription)
482515 }
483516 }
484517
@@ -494,9 +527,9 @@ public struct Format {
494527 let value : String
495528
496529 func write( to stream: WritableByteStream ) {
497- stream <<< UInt8 ( ascii: " \" " )
530+ stream. send ( UInt8 ( ascii: " \" " ) )
498531 stream. writeJSONEscaped ( value)
499- stream <<< UInt8 ( ascii: " \" " )
532+ stream. send ( UInt8 ( ascii: " \" " ) )
500533 }
501534 }
502535
@@ -514,12 +547,12 @@ public struct Format {
514547 let items : [ String ]
515548
516549 func write( to stream: WritableByteStream ) {
517- stream <<< UInt8 ( ascii: " [ " )
550+ stream. send ( UInt8 ( ascii: " [ " ) )
518551 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) )
521554 }
522- stream <<< UInt8 ( ascii: " ] " )
555+ stream. send ( UInt8 ( ascii: " ] " ) )
523556 }
524557 }
525558
@@ -531,12 +564,12 @@ public struct Format {
531564 let items : [ String : String ]
532565
533566 func write( to stream: WritableByteStream ) {
534- stream <<< UInt8 ( ascii: " { " )
567+ stream. send ( UInt8 ( ascii: " { " ) )
535568 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) )
538571 }
539- stream <<< UInt8 ( ascii: " } " )
572+ stream. send ( UInt8 ( ascii: " } " ) )
540573 }
541574 }
542575
@@ -551,12 +584,12 @@ public struct Format {
551584 let transform : ( T ) -> String
552585
553586 func write( to stream: WritableByteStream ) {
554- stream <<< UInt8 ( ascii: " [ " )
587+ stream. send ( UInt8 ( ascii: " [ " ) )
555588 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) ) )
558591 }
559- stream <<< UInt8 ( ascii: " ] " )
592+ stream. send ( UInt8 ( ascii: " ] " ) )
560593 }
561594 }
562595
@@ -572,10 +605,10 @@ public struct Format {
572605 for (i, item) in items. enumerated ( ) {
573606 // Add the separator, if necessary.
574607 if i != 0 {
575- stream <<< separator
608+ stream. send ( separator)
576609 }
577610
578- stream <<< item
611+ stream. send ( item)
579612 }
580613 }
581614 }
@@ -596,8 +629,8 @@ public struct Format {
596629
597630 func write( to stream: WritableByteStream ) {
598631 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) )
601634 }
602635 }
603636 }
@@ -617,7 +650,7 @@ public struct Format {
617650
618651 func write( to stream: WritableByteStream ) {
619652 for _ in 0 ..< count {
620- stream <<< string
653+ stream. send ( string)
621654 }
622655 }
623656 }
0 commit comments