Generate code which only compiles if type is invariant

⚓ Rust    📅 2025-07-11    👤 surdeus    👁️ 2      

surdeus

I decided to make a library providing a macro to enforce a compile time requirement that a type's generic parameters conform to a certain variance. My plan for doing this was to have the macro generate code which only compiles if the type follows that variance, but I cannot figure out how to do that for invariance. Is making a function which only compiles if a type is invariant with respect to a specific generic parameter even possible? If not, would the best path forward be to insert fields to ensure invariance but continue with the compile-time checking strategy for enforcing covariance and contravariance?

2 posts - 2 participants

Read full topic

🏷️ rust_feed