implicit def either[L, R]()

in bijection-core/src/main/scala/com/twitter/bijection/Bufferable.scala [208:231]


  implicit def either[L, R](implicit
      bufl: Bufferable[L],
      bufr: Bufferable[R]
  ): Bufferable[Either[L, R]] =
    build[Either[L, R]] { (bb, eith) =>
      eith match {
        case Left(l) => {
          val nextBb = reallocatingPut(bb) { _.put(0: Byte) }
          reallocatingPut(nextBb) { bufl.put(_, l) }
        }
        case Right(r) => {
          val nextBb = reallocatingPut(bb) { _.put(1: Byte) }
          reallocatingPut(nextBb) { bufr.put(_, r) }
        }
      }
    } { bb =>
      val dup = bb.duplicate
      val byte0 = 0: Byte
      if (dup.get == byte0) {
        bufl.get(dup).map { tup => (tup._1, Left(tup._2)) }
      } else {
        bufr.get(dup).map { tup => (tup._1, Right(tup._2)) }
      }
    }