I have the following code
#[derive(Clone, Debug)]
pub struct SwiftContext {
name: String,
}
pub trait RDD {
type Item;
fn compute<T: Iterator>(&self, part: u32) -> T;
fn get_context(&self) -> SwiftContext;
fn map<B, F>(self, f: F) -> MappedRDD<Self, F> where
Self: Sized, F: FnMut(Self::Item) -> B {
MappedRDD {sc: self.get_context(), prev: self, f: f}
}
}
pub struct MappedRDD<P, F> {
pub sc: SwiftContext,
prev: P,
f: F,
}
impl<B, P: RDD, F> RDD for MappedRDD<P, F> where F: FnMut(P::Item) -> B {
type Item = B;
fn compute<T: Iterator>(&self, part: u32) -> T { // error
self.prev.compute(part).map(self.f)
}
fn get_context(&self) -> SwiftContext {
self.sc.clone()
}
}
The problem is in the compute
method, because I need to return a concrete type. what I want to solve is introduce a new type in the trait. something like following
pub trait RDD {
type Item;
type computeType: Iterator; //new
fn compute(&self, part: u32) -> Self::computeType;
fn get_context(&self) -> SwiftContext;
fn map<B, F>(self, f: F) -> MappedRDD<Self, F> where
Self: Sized, F: FnMut(Self::Item) -> B {
MappedRDD {sc: self.get_context(), prev: self, f: f}
}
}
impl<B, P: RDD, F> RDD for MappedRDD<P, F> where F: FnMut(P::Item) -> B {
type Item = B;
type computeType = Map<P::computeType, F>; // error
fn compute(&self, part: u32) -> Self::computeType {
self.prev.compute(part).map(self.f)
}
fn get_context(&self) -> SwiftContext {
self.sc.clone()
}
}
But I do not know how to write the computeType
in the MappedRDD
.
I find the source code in the Map
use the Self
, but my MappedRDD
do not implement the Iterator
trait.
Any advice is thankful!