How do I properly implement a trait for an array with fixed size?


#1

How do I implement a trait for an array with fixed size say


    [String;1], [String:2], [String: 3] .. [String;n]

I can workaround with the compiler by doing


    impl ToColumnName for [String;1]{
    ...
    }

    impl ToColumnName for [String;2]{
    ...
    }

    impl ToColumnName for [String;3]{
    ...
    }

    macro_rules! impl_to_column_name_for_string{
        ($x:expr)=>(
            impl ToColumnName for [String;$x]{
             ...   
            }
        );
    }

    impl_to_column_name_for_string!(4);
    impl_to_column_name_for_string!(5);
    impl_to_column_name_for_string!(6);
    impl_to_column_name_for_string!(7);
    impl_to_column_name_for_string!(8);
    impl_to_column_name_for_string!(9);
    impl_to_column_name_for_string!(10);
    impl_to_column_name_for_string!(11);
    impl_to_column_name_for_string!(12);

but I think this is code bloat, and it doesn’t even handle arrays with more than 12 items


#2

Until we get type level integers, this is the way to go. There’s an RFC for that, but we will have to wait for that.


#3

Thank you for your response. It’s good to know there is an RFC for this, and good to know also I am not doing anything too silly. :slightly_smiling:


#4

You’re welcome, but you’re doing Rust already ;).