def toContainer[A, B, C <: IterableOnce[A], D <: IterableOnce[B]]()

in bijection-core/src/main/scala-2.13+/com/twitter/bijection/CollectionInjections.scala [76:102]


  def toContainer[A, B, C <: IterableOnce[A], D <: IterableOnce[B]](
      goodInv: (D, C) => Boolean
  )(implicit
      inj: Injection[A, B],
      cd: Factory[B, D],
      dc: Factory[A, C]
  ): Injection[C, D] =
    new AbstractInjection[C, D] {
      def apply(c: C): D = {
        val builder = cd.newBuilder
        c.iterator.foreach { builder += inj(_) }
        builder.result()
      }
      override def invert(d: D): Try[C] = {
        val builder = dc.newBuilder
        d.iterator.foreach { b =>
          val thisB = inj.invert(b)
          if (thisB.isSuccess) {
            builder += thisB.get
          } else {
            return InversionFailure.failedAttempt(d)
          }
        }
        val res = builder.result()
        if (goodInv(d, res)) Success(res) else InversionFailure.failedAttempt(d)
      }
    }