kotlin_poet_rs/spec/
generic_invariance.rs

1use crate::io::RenderKotlin;
2use crate::spec::CodeBlock;
3use crate::tokens;
4
5/// Type of generic [parameter invariance](https://kotlinlang.org/docs/generics.html#variance-and-wildcards-in-java).
6///
7/// Conventionally possibly non-invariant generics are expressed via `Option<GenericInvariance>`
8#[derive(Debug, Clone)]
9pub enum GenericInvariance {
10    /// Corresponds to [tokens::keyword::IN]
11    In,
12    /// Corresponds to [tokens::keyword::OUT]
13    Out,
14}
15
16impl RenderKotlin for GenericInvariance {
17    fn render_into(&self, block: &mut CodeBlock) {
18        match self {
19            GenericInvariance::In => block.push_static_atom(tokens::keyword::IN),
20            GenericInvariance::Out => block.push_static_atom(tokens::keyword::OUT),
21        }
22    }
23}
24
25#[cfg(test)]
26mod tests {
27    use crate::io::RenderKotlin;
28    use super::*;
29
30    #[test]
31    fn test_generic_invariance() {
32        let invariance = GenericInvariance::In;
33        assert_eq!(invariance.render_string(), "in");
34
35        let invariance = GenericInvariance::Out;
36        assert_eq!(invariance.render_string(), "out");
37    }
38}