Permalink
Browse files

update

- 2.8.1 only
- drop JavaIO in favor of implicits in companion objects
- add canBuildFormat for arbitrary Iterable subclasses
  (not currently implicit due to conflict somewhere that causes lub/glb compiler crash)
  • Loading branch information...
harrah committed May 10, 2011
1 parent 301d400 commit 0aa1d8118995e381ab07138ff65582b7e5778927
View
@@ -11,42 +11,39 @@ import Operations._;
trait Generic extends CoreProtocol{
implicit def arrayFormat[T](implicit fmt : Format[T], mf: scala.reflect.Manifest[T]) : Format[Array[T]];
- // Needed to implement viaSeq, which is need for 2.7/2.8 compatibility -MH
+ // Needed to implement viaSeq
implicit def listFormat[T](implicit fmt : Format[T]) : Format[List[T]];
- /** A more general LengthEncoded. For 2.7/8 compatibility (arrays are no longer collections). -MH*/
+ /** A more general LengthEncoded (arrays are no longer collections) */
abstract class CollectionFormat[S, T](implicit binT : Format[T]) extends Format[S]{
def size(s: S): Int
def foreach(s: S)(f: T => Unit): Unit
def build(size : Int, ts : Iterator[T]) : S;
- def reads(in : Input) = { val size = read[Int](in); build(size, (0 until size).map(i => read[T](in)).elements) }
+ def reads(in : Input) = { val size = read[Int](in); build(size, (0 until size).map(i => read[T](in)).iterator) }
def writes(out : Output, ts : S) = { write(out, size(ts)); foreach(ts)(write(out, _)); }
}
/**
* Format instance which encodes the collection by first writing the length
* of the collection as an int, then writing the collection elements in order.
*/
- abstract class LengthEncoded[S <: Collection[T], T](implicit binT : Format[T]) extends CollectionFormat[S, T]{
+ abstract class LengthEncoded[S <: Traversable[T], T](implicit binT : Format[T]) extends CollectionFormat[S, T]{
def size(s: S) = s.size
def foreach(s: S)(f: T => Unit) = s.foreach(f)
}
/**
* Length encodes, but with the result built from an array.
- *
- * implicit Manifest required as of 0.3.1 for Scala 2.8 compatibility. -MH
*/
- def viaArray[S <: Collection[T], T] (f : Array[T] => S) (implicit binary : Format[T], mf: scala.reflect.Manifest[T]) : Format[S] = new Format[S] {
+ def viaArray[S <: Traversable[T], T] (f : Array[T] => S) (implicit binary : Format[T], mf: scala.reflect.Manifest[T]) : Format[S] = new Format[S] {
def writes(out : Output, xs : S) = { write(out, xs.size); xs.foreach(write(out, _)); }
def reads(in : Input) = f(read[Array[T]](in));
}
/**
* Length encodes, but with the result built from a Seq.
- *
- * Exists to solve 2.7/2.8 compatibility. -MH
+ * Useful for when a `ClassManifest` is not available the underlying type `T`.
*/
- def viaSeq[S <: Collection[T], T] (f : Seq[T] => S) (implicit binary : Format[T]) : Format[S] = new Format[S] {
+ def viaSeq[S <: Traversable[T], T] (f : Seq[T] => S) (implicit binary : Format[T]) : Format[S] = new Format[S] {
def writes(out : Output, xs : S) = { write(out, xs.size); xs.foreach(write(out, _)); }
def reads(in : Input) = f(read[List[T]](in));
}
View
@@ -83,7 +83,9 @@ class JavaOutput(out : OutputStream) extends Output{
override def writeAll(source : Array[Byte], offset : Int, length : Int) = out.write(source, offset, length);
}
-object JavaIO{
- implicit def javaInputToInput(x : InputStream) = new JavaInput(x);
- implicit def javaOutputToOutput(x : OutputStream) = new JavaOutput(x);
+object Input {
+ implicit def javaInputToInput(x : InputStream): JavaInput = new JavaInput(x)
+}
+object Output {
+ implicit def javaOutputToOutput(x : OutputStream): JavaOutput = new JavaOutput(x);
}
@@ -2,8 +2,7 @@
package sbinary;
object Operations{
- import java.io._;
- import JavaIO._;
+ import java.io.{BufferedInputStream, BufferedOutputStream, ByteArrayInputStream, ByteArrayOutputStream, File, FileInputStream, FileOutputStream};
def format[T](implicit fm : Format[T]) = fm;
@@ -2,6 +2,7 @@ package sbinary;
import Operations._;
import scala.collection._;
+import generic.CanBuildFrom
trait BasicTypes extends CoreProtocol{
implicit def optionsAreFormat[S](implicit bin : Format[S]) : Format[Option[S]] = new Format[Option[S]]{
@@ -40,15 +41,21 @@ trait BasicTypes extends CoreProtocol{
</#list>
}
-trait CollectionTypes extends BasicTypes with Generic{
- implicit def listFormat[T](implicit bin : Format[T]) : Format[List[T]] =
- new LengthEncoded[List[T], T]{
+trait LowPriorityCollectionTypes extends Generic {
+ def canBuildFormat[CC[X] <: Traversable[X], T](implicit bin : Format[T], cbf: CanBuildFrom[Nothing, T, CC[T]]) : Format[CC[T]] =
+ new LengthEncoded[CC[T], T]{
def build(length : Int, ts : Iterator[T]) = {
- val buffer = new mutable.ListBuffer[T];
- ts.foreach(buffer += (_ : T));
- buffer.toList;
+ val builder = cbf.apply()
+ builder.sizeHint(length)
+ builder ++= ts
+ if(ts.hasNext) error("Builder did not consume all input.") // no lazy builders allowed
+ builder.result()
}
}
+}
+
+trait CollectionTypes extends BasicTypes with LowPriorityCollectionTypes {
+ implicit def listFormat[T](implicit bin : Format[T]) : Format[List[T]] = canBuildFormat[List, T]
implicit def arrayFormat[T](implicit fmt : Format[T], mf : scala.reflect.Manifest[T]) : Format[Array[T]] = fmt match{
case ByteFormat => ByteArrayFormat.asInstanceOf[Format[Array[T]]];
@@ -84,16 +91,14 @@ trait CollectionTypes extends BasicTypes with Generic{
implicit def immutableSetFormat[T](implicit bin : Format[T]) : Format[immutable.Set[T]] =
viaSeq((x : Seq[T]) => immutable.Set(x :_*))
- implicit def immutableSortedSetFormat[S](implicit ord : S => Ordered[S], binS : Format[S]) : Format[immutable.SortedSet[S]] = {
- import BasicTypes.orderable // 2.7/8 compatibility
+ implicit def immutableSortedSetFormat[S](implicit ord : Ordering[S], binS : Format[S]) : Format[immutable.SortedSet[S]] = {
viaSeq( (x : Seq[S]) => immutable.TreeSet[S](x :_*))
}
implicit def immutableMapFormat[S, T](implicit binS : Format[S], binT : Format[T]) : Format[immutable.Map[S, T]] =
viaSeq( (x : Seq[(S, T)]) => immutable.Map(x :_*));
- implicit def immutableSortedMapFormat[S, T](implicit ord : S => Ordered[S], binS : Format[S], binT : Format[T]) : Format[immutable.SortedMap[S, T]] = {
- import BasicTypes.orderable // 2.7/8 compatibility
+ implicit def immutableSortedMapFormat[S, T](implicit ord : Ordering[S], binS : Format[S], binT : Format[T]) : Format[immutable.SortedMap[S, T]] = {
viaSeq( (x : Seq[(S, T)]) => immutable.TreeMap[S, T](x :_*))
}
@@ -162,9 +167,3 @@ trait StandardTypes extends CollectionTypes{
def writes(out : Output, elem : NodeSeq) = write(out, <binary>elem</binary>.toString);
}
}
-object BasicTypes {
- /** 2.7/8 compatibility */
- implicit def orderable[A](implicit s: A => Ordered[A]): Ordering[A] = new Ordering[A] {
- def compare(x: A, y: A) = s(x).compare(y)
- }
-}
@@ -70,7 +70,6 @@ import Equal._;
object CompatTests extends Properties("CompatTests"){
import java.io._;
- import JavaIO._;
def compatFor[T](name : String, readJ : DataInput => T, writeJ : (DataOutput, T) => Unit)(implicit fmt : Format[T], arb : Arbitrary[T]) = {
property(name + "AgreesWithDataInput") = forAll { (x : T) =>
@@ -134,8 +133,7 @@ object FormatTests extends Properties("Formats"){
implicit val arbitraryUnit = Arbitrary[Unit](value(() => ()))
- implicit def arbitrarySortedMap[K, V](implicit ord : K => Ordered[K], arbK : Arbitrary[K], arbV : Arbitrary[V]) : Arbitrary[immutable.SortedMap[K, V]] = {
- import BasicTypes.orderable
+ implicit def arbitrarySortedMap[K, V](implicit ord : Ordering[K], arbK : Arbitrary[K], arbV : Arbitrary[V]) : Arbitrary[immutable.SortedMap[K, V]] = {
Arbitrary(arbitrary[List[(K, V)]].map(x => immutable.TreeMap(x :_*)))
}
@@ -145,12 +143,12 @@ object FormatTests extends Properties("Formats"){
implicit val arbitraryEnumeration : Arbitrary[Enumeration] = Arbitrary(arbitrary[List[String]].map(x => new Enumeration(x : _*){}));
- implicit def orderedOption[T](opt : Option[T])(implicit ord : T => Ordered[T]) : Ordered[Option[T]] = new Ordered[Option[T]]{
+ implicit def orderedOption[T](opt : Option[T])(implicit ord : Ordering[T]) : Ordered[Option[T]] = new Ordered[Option[T]]{
def compare(that : Option[T]) = (opt, that) match {
case (None, None) => 0;
case (None, Some(_)) => -1;
case (Some(_), None) => 1;
- case (Some(x), Some(y)) => x.compare(y);
+ case (Some(x), Some(y)) => ord.compare(x,y);
}
}
@@ -203,7 +201,7 @@ object FormatTests extends Properties("Formats"){
implicit val SomeEnumFormat = enumerationFormat[SomeEnum.Value](SomeEnum)
implicit val SomeEnumEq = allAreEqual[SomeEnum.Value]
- implicit val SomeEnumArb = Arbitrary[SomeEnum.Value](elements(SomeEnum.items :_*))
+ implicit val SomeEnumArb = Arbitrary[SomeEnum.Value](oneOf(SomeEnum.items))
formatSpec[Boolean]("Boolean");
formatSpec[Byte]("Byte");
View
@@ -1,9 +1,6 @@
-#Project properties
-#Sun Jan 24 13:59:22 EST 2010
project.organization=org.scala-tools.sbinary
project.name=SBinary Parent
-sbt.version=0.7.3
-project.version=0.3.1-SNAPSHOT
-def.scala.version=2.7.7
-build.scala.versions=2.7.7 2.8.0.Beta1
+sbt.version=0.7.5
+project.version=0.3.2-SNAPSHOT
+build.scala.versions=2.8.1
project.initialize=false
@@ -16,9 +16,7 @@ class SBinaryProject(info: ProjectInfo) extends ParentProject(info) with NoPubli
}
class CoreProject(info: ProjectInfo) extends DefaultProject(info) with TemplateProject
{
- val sc =
- if(buildScalaVersion.startsWith("2.7")) "org.scala-tools.testing" %% "scalacheck" % "1.6" % "test"
- else "org.scala-tools.testing" % "scalacheck" % "1.7-SNAPSHOT" % "test" from("http://scalacheck.googlecode.com/files/scalacheck_2.8.0.Beta1-1.7-SNAPSHOT.jar")
+ val sc ="org.scala-tools.testing" % "scalacheck_2.8.1" % "1.7" % "test"
override def mainResources = super.mainResources +++ "LICENSE"
}
@@ -27,4 +25,4 @@ trait NoPublish extends BasicManagedProject
{
override def deliverAction = publishAction
override def publishAction = task { None }
-}
+}

0 comments on commit 0aa1d81

Please sign in to comment.