I apologize for the title which is not very clear, but I don't know how to describe my problem.
I try to make a program that do some dimensional analysis.
What I do is that a make a tree for the dimensions (a unit like “kg”, a power like “m²”, or a calculation like “U × I”).
Then I want to have my dimension with only the ones from the SI.
For now, it was quite simple to implement this.
But now I want to stock the dimension and its SI representation in a same struct.
So first, we have an enum Dimension<'a> which is a tree that represent a calculation.
pub enum Dimension<'di> {
Unite(String),
Power(&'di RawDimension<'di>, i32),
Composed(&'di Dimension<'di>, Op, &'di Dimension<'di>),
}
enum Op {
Plus,
Minus
}
Add, a method to get the SI representation
impl<'di> Dimension<'di> {
fn into_SI(&'di self) -> Vec<Dimension> {
// some code
vec![]
}
}
Finally, we have a struct Final<'a> which handle a Dimension<'a> add its result.
struct Final<'di> {
dimension: Dimension<'di>,
SI: Vec<Dimention<'di>>
}
BUT, when the time comes to initialize Final, everything goes wrong.
impl<'di> From<Dimension<'di>> for Final<'di> {
fn from(dim: Dimension<'di>) -> Final<'di> {
Final {
dimension: dim, // dim is owned by the dimention field
SI: dim.into_SI(), // but it's borrow here at the same time !
}
}
}
I try to use Pin, but I don't manage to make it work.
Do you have any solution ?