Skip to content

Parameterization of const generics? #300

@janbridley

Description

@janbridley

Is there a clear-cut way to parameterize const generics? I see #207 mentions generic types, but I'm not sure if those solutions translate here - I haven't figured out a method by which rstest can pass const values. I have an example where I parameterize the dimension of a struct and would like to use the same test for multiple const generics

pub struct Sphere<const N: usize> {
    /// Radius of the sphere
    pub r: f64,
    pub center: [f64; N],
}

// ...

Currently, I parameterize tests with a declarative macro but I'm curious as to whether rstest provides a cleaner option:

    macro_rules! parameterize_dimension {
        ($test_body:ident, [$($dim:expr),*]) => {
            $(
                paste! {
                    #[test]
                    fn [< $test_body "_" $dim>]() {
                        const DIM: usize = $dim;
                        $test_body::<DIM>();
                    }
                }
            )*
        };
    }
    fn test_radius<const N: usize>() {
        let s = Sphere::<N>::from(1.0);
        assert_eq!(s.r, 1.0);
    }
    parameterize_vector_length!(test_radius, [0, 1, 2, 3, 4, 5]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions