@@ -46,7 +46,7 @@ trait DataProduct[-A] {
46
46
47
47
/** Low priority built-in implementations of [[DataProduct ]]
48
48
*
49
- * @note This trait exists so that `dataDataProduct` can be lower priority than `seqDataProduct ` to resolve ambiguity
49
+ * @note This trait exists so that `dataDataProduct` can be lower priority than `iterableDataProduct ` to resolve ambiguity
50
50
*/
51
51
sealed trait LowPriorityDataProduct {
52
52
@@ -63,6 +63,25 @@ sealed trait LowPriorityDataProduct {
63
63
*/
64
64
object DataProduct extends LowPriorityDataProduct {
65
65
66
+ /** Factory method for constructing [[DataProduct ]]s */
67
+ def apply [A ](f : (A , String ) => Iterator [(Data , String )]): DataProduct [A ] = new DataProduct [A ] {
68
+ def dataIterator (a : A , path : String ): Iterator [(Data , String )] = f(a, path)
69
+ }
70
+
71
+ /** Factory for constructing [[DataProduct ]] for types that do not contain any [[Data ]] */
72
+ def empty [A ]: DataProduct [A ] = apply[A ] { case _ => Iterator .empty }
73
+
74
+ // DataProducts for simple primitive types
75
+ implicit val charDataProduct : DataProduct [Char ] = empty
76
+ implicit val stringDataProduct : DataProduct [String ] = empty
77
+ implicit val intDataProduct : DataProduct [Int ] = empty
78
+ implicit val byteDataProduct : DataProduct [Byte ] = empty
79
+ implicit val shortDataProduct : DataProduct [Short ] = empty
80
+ implicit val longDataProduct : DataProduct [Long ] = empty
81
+ implicit val bigIntDataProduct : DataProduct [BigInt ] = empty
82
+ implicit val floatDataProduct : DataProduct [Float ] = empty
83
+ implicit val doubleDataProduct : DataProduct [Double ] = empty
84
+
66
85
/** [[DataProduct ]] implementation for [[BaseModule ]] */
67
86
implicit val userModuleDataProduct : DataProduct [BaseModule ] = new DataProduct [BaseModule ] {
68
87
def dataIterator (a : BaseModule , path : String ): Iterator [(Data , String )] = {
@@ -82,7 +101,8 @@ object DataProduct extends LowPriorityDataProduct {
82
101
}
83
102
84
103
/** [[DataProduct ]] implementation for any `Seq[A]` where `A` has an implementation of `DataProduct`. */
85
- implicit def seqDataProduct [A : DataProduct ]: DataProduct [Seq [A ]] = new DataProduct [Seq [A ]] {
104
+ @ deprecated(" Use iterableDataProduct instead" , " Chisel 7.0" )
105
+ def seqDataProduct [A : DataProduct ]: DataProduct [Seq [A ]] = new DataProduct [Seq [A ]] {
86
106
def dataIterator (a : Seq [A ], path : String ): Iterator [(Data , String )] = {
87
107
val dpa = implicitly[DataProduct [A ]]
88
108
a.iterator.zipWithIndex.flatMap {
@@ -92,6 +112,17 @@ object DataProduct extends LowPriorityDataProduct {
92
112
}
93
113
}
94
114
115
+ /** [[DataProduct ]] implementation for any `Iterable[A]` where `A` has an implementation of `DataProduct`. */
116
+ implicit def iterableDataProduct [A : DataProduct , F [A ] <: IterableOnce [A ]]: DataProduct [F [A ]] = new DataProduct [F [A ]] {
117
+ def dataIterator (a : F [A ], path : String ): Iterator [(Data , String )] = {
118
+ val dpa = implicitly[DataProduct [A ]]
119
+ a.iterator.zipWithIndex.flatMap {
120
+ case (elt, idx) =>
121
+ dpa.dataIterator(elt, s " $path[ $idx] " )
122
+ }
123
+ }
124
+ }
125
+
95
126
/** [[DataProduct ]] implementation for any [[scala.Tuple2 ]] where each field has an implementation of `DataProduct`. */
96
127
implicit def tuple2DataProduct [A : DataProduct , B : DataProduct ]: DataProduct [(A , B )] = new DataProduct [(A , B )] {
97
128
def dataIterator (tup : (A , B ), path : String ): Iterator [(Data , String )] = {
0 commit comments