Hello everyone, I'm fooling around, trying to increase my knowledge of the language.
Today I'm trying to solve this problem.
I'd like to implement the Farmer trait below. I hereby list some facts that may or may not help:
- The
Farmertrait must not be generic. - The number of structs that implement the
Stagetrait is known, butStagemust at all cost be a trait. - The
Cropsassociated type of theFarmertrait will most likely be a struct containing oneVectorfor each life cycle of the plant. This struct contains only one type that implementsFarmPlant. - The
farmmethod of theFarmertrait will most likely be used while looping throughCrops.
struct Seed;
struct Young;
struct Mature;
struct Flower;
struct Fruit;
pub trait Stage {}
impl Stage for Seed {}
impl Stage for Young {}
impl Stage for Mature {}
impl Stage for Flower {}
impl Stage for Fruit {}
pub trait FarmPlant<D: Stage> {}
// This to me feels like a natural way of doing thigs, but
// I'd very much like to remain on stable.
pub trait Farmer { // A farmer can attend to only a type of crop.
type Crops; // A struct containing a vector for each of the crop's life cycles.
type Crop<D: Stage>: FarmPlant<D>;
fn farm<D: Stage>(&self, crop: &Self::Crop<D>);
fn crops(&self) -> &Self::Crops;
}