Fomalhaut
Fomalhaut

Reputation: 9745

How to pass an instance of a structure that has implemented a certain trait?

I am trying to write a correct signature for a method that takes an object by reference as input. It's supposed the object to be an instance of a structure that implements a certain trait.

impl MyStruct {
    pub fn create_proof<E: Engine>(&self, C: &Circuit<E>, pk: &Parameters<E>) -> Proof<E> {
        unimplemented!()
    }
}

Circuit is defined as a trait like this trait Circuit<E: Engine> and it has an implemented method inside.

When I compile the project I get the error:

the trait `mylib::Circuit` cannot be made into an object
note: method `circuit_method` has generic type parameters

Why this error occurred and how to fix it? I am not allowed to modify everything bound to mylib where the trait Circuit is. All I am allowed to do to write the correct signature. The whole code of the project is too huge and tricky, I do not think it is a good idea to share it.

Upvotes: 0

Views: 122

Answers (1)

msrd0
msrd0

Reputation: 8371

Try to make the struct that implements Circuit a generic type as well:

pub fn create_proof<C, E>(&self, c: &C, pk: &Parameters<E>) -> Proof<E>
where
    C: Circuit<E>,
    E: Engine,
{
    unimplemented!()
}

Upvotes: 2

Related Questions