Adding another generic parameter without modifying all exist API

I have a strcut which take a generic argument:

struct Foo<const I: i32> {}

impl<const I: i32> Foo<I> {
    pub fn foo() {}
}

And now I need to add another generic parameter:

struct Foo<const I: i32, const J: i32> {}

then all exist API was invalid:

error[E0107]: this struct takes 2 generic arguments but 1 generic argument was supplied
 --> src/main.rs:3:20
  |
3 | impl<const I: i32> Foo<I> {
  |                    ^^^ - supplied 1 generic argument
  |                    |
  |                    expected 2 generic arguments
  |
note: struct defined here, with 2 generic parameters: `I`, `J`
 --> src/main.rs:1:8
  |
1 | struct Foo<const I: i32, const J: i32> {}
  |        ^^^ ------------  ------------
help: add missing generic argument
  |
3 | impl<const I: i32> Foo<I, J> {

This is reasonable, but it is painful to refactor the code for two reasons:

  1. There are a lot of functions like foo in my code, I need to change them all.
  2. All the existing functions like foo has nothing to do with the new parameter J.

So is there any way I can refactor the code, that whenever I add a new generic parameter, existing functions do not need to be modified?

P.S. More details about reason 1: the design pattern of my project is not put all functions in one impl block, that why I have to change a lot of places.
P.P.S. I tried to give the J a default value, in this way I do avoid of modifying functions, but it's not what I want, it means all the old functions are only applied for types with default value.

Solution 1:

trait OnlyForJ<const J: i32> {
    // All stuff about J
    fn f(){dbg!(J);}
}

impl<const I: i32, const J: i32> OnlyForJ<J> for Foo<I>{}

Solution 2:

impl<const I: i32> Foo<I> {
    fn g<const J: i32>() {dbg!(J);}
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.