You cannot force the compiler to emit an error when some combination of A
and B
doesn't implement save
. But you can have a generic function that requires that the combination of the specific A
and B
it receives implements save
.
To do that, we need to wrap save
in a trait and implement it on some type that includes both A
and B
; the simplest option is a tuple. (Coherence may get in the way if the trait and the types are not all in the same crate, though.)
trait Save {
fn save(self);
}
struct Foo; // sample save source
struct Bar; // sample save destination
// save is defined for the combination of `Foo` and `Bar`
impl Save for (Foo, Bar) {
fn save(self) {
unimplemented!()
}
}
// in order to call this, the type `(A, B)` must implement `Save`
fn call_save<A, B>(a: A, b: B)
where
(A, B): Save
{
(a, b).save();
}
fn main() {
// this call compiles because `impl Save for (Foo, Bar)` is present
call_save(Foo, Bar);
}
You can also do it for references:
trait Save {
fn save(self);
}
struct Foo;
struct Bar;
impl<'a, 'b> Save for (&'a Foo, &'b Bar) {
fn save(self) {
unimplemented!()
}
}
fn call_save<'a, 'b, A, B>(a: &'a A, b: &'b B)
where
(&'a A, &'b B): Save
{
(a, b).save();
}
fn main() {
call_save(&Foo, &Bar);
}