Macro needed that takes matrixValue!(mat,R,C) to mat.aRC


#1

Hi,

Macro needed that takes matrixValue!(mat,R,C) and writes mat.aRC

Where R is Row number and C is Column number.

like mat.a44 etc


#2

Unfortunately, I don’t think this is possible using the standard rust macro system. It will be possible with function-type Macros 2.0 (which might have a prototype in nightly relatively soon actually).


#3

Thanks so is there anyway to have a union of my matrix variables accessiable via Array in matrix4x4 struct?
I want both forms of matrix element access.


#4

Sorry, I don’t quite understand the question. Perhaps you can provide an example of your desired result?


#5

I have below struct that Im also interested in accessing its elements like an Array.

pub struct real4x4 {
    pub a11: real,
    pub a12: real,
    pub a13: real,
    pub a14: real,
    pub a21: real,
    pub a22: real,
    pub a23: real,
    pub a24: real,
    pub a31: real,
    pub a32: real,
    pub a33: real,
    pub a34: real,
    pub a41: real,
    pub a42: real,
    pub a43: real,
    pub a44: real,
}

#6

If you are looking for a method to access the fields, then the only technique I know of is using a procedural macro. You can take a look at https://cbreeden.github.io/Macros11/ to get an idea of how to build one. The code you want to generate is probably something like this:

impl Real4x4 {
    fn get_value(&self, m: usize, n: usize) -> Real {
        match (m, n) {
            (1, 1) => self.a11,
            (1, 2) => self.a12,
            (1, 3) => self.a13,
            ..
            (4, 3) => self.a43,
            (4, 4) => self.a44,
            _ => panic!() // Or do whatever
        }
    }
}

If your structs are named similarly, like Real4x4 then building this pattern matching should be a matter of decoding the name, or you could decode the field names.


#7

Thanks man. If it resolves at compile time its all i need.