akka/akka — pull request #1900 by jrudolph (2013-12-20 11:21:48+00:00)
Title: New Bytes abstraction + benefits reaped by simplifying Tcp.WriteCommand hierarchy

Here's a first proposal of the new unified Bytes abstraction as discussed [on the ML](https://groups.google.com/d/topic/akka-dev/43tW_BKXE2Q/discussion).

Some notes:

After first having put the complete `Bytes` code into `ByteString.scala` (to keep sealedness and integrate `ByteString` into the type hierarchy), I went with another approach and split the `Bytes` code into its own file with a sealed hierarchy looking like this:

```
Bytes
|-- SimpleBytes
| |-- ByteStringBytes (= ByteString)
| |-- FileBytes
|-- CompoundBytes
```

I left `ByteStringBytes` unsealed and derived `ByteString` from it but gave it a private constructor (leaving still a small theoretical loop-hole for custom implementations but it still seemed like the cleaner trade-off in comparison with putting anything into one file). This allows to pattern match on the complete hierarchy and still allows `ByteString` to be part of it with only minimal adaptions needed.

One potential conflict is that `Bytes` allow data with Long-range lengths. In spray's `HttpData` the signature of the length method was `def length: Long`. This isn't possible if `ByteString` should be part of the hierarchy since it already has a `def length: Int` method that is needed for inheriting from `IndexedSeq` and other standard library collections. Therefore, a `Bytes.longLength` method was introduced to support Long-range lengths where needed.

The second commit shows how the `Tcp.WriteCommand` hierarchy can be replaced by a simple `case class Tcp.Write(Bytes, ack)`.