I sumbled upon a problem while trying out Rust, that might point to a bigger non understanding of its concepts on my part.
My goal is to programm a variant of Conway's Game of Life. I want the values when cells are created or stay alive not to be hard coded, but within a struct. My first attempt was to create a struct
use std::ops::Range;
struct Rules {
be_born: Range<usize>,
stay_alive: Range<usize>,
}
impl Rules {
pub fn new(be_born: Range<usize>, stay_alive: Range<usize>) -> Rules {
Rules { be_born, stay_alive }
}
}
let rules = Rules::new(2..4, 3..6);
This object is later used within the algorithm that iterates over all the cells. It works fine, until I also want to allow other kind of Ranges during creation like for example RangeTo (2..=3).
I know I could rewrite the struct Rules
to be generic.
use std::ops::RangeBounds;
struct Rules<BR: RangeBounds<usize>, AR: RangeBounds<usize>> {
be_born: BR,
stay_alive: AR,
}
This in turn would force me to make all the algorithms I use to be generic as well. This seems to be quite a lot of overhead just to include two simple ranges.
On the other hand none of my attempts to include variables of type RangeBounds
directly into my struct succeeded. I tried &dyn RangeBounds<usize>
or Box<&dyn RangeBounds<usize>>
, just to always get the error E0038 that I cannot make this trait into an object.
Is there any other way to get this done, or is there some other feasable way I do not see?
Thank you in advance for all your hints.