You can't add a #[cfg] attribute to part of a where-clause because Rust's syntax wasn't designed that way.
I'd also recommend against this pattern in general. Features are meant to compose and be purely additive, but imagine a situation like this...
- Crate A implements
MyTraiton some typeFoowithoutwith-trait1 - Crate B adds A as a dependency
- Crate B uses A, relying on the fact that
Foo: MyTrait - Crate B enables the
with-trait1feature, adding extra requirements forMyTrait... Requirements that crate A doesn't fulfill because it didn't enable thewith-trait1feature.
That said, you can implement it by adding a #[cfg] to the entire trait definition.
#[cfg(feature = "with-trait1")]
pub trait MyTrait: Copy + Trait1 + Trait2 { ... }
#[cfg(not(feature = "with-trait1"))]
pub trait MyTrait: Copy + Trait2 { ... }
// or if the body of `MyTrait` is long and you don't want to
// repeat yourself, pull the requirements out into an
// intermediate trait.
#[cfg(feature = "with-trait1")]
pub trait Deps: Copy + Trait1 + Trait2 { }
#[cfg(not(feature = "with-trait1"))]
pub trait Deps: Copy + Trait2 { }
pub trait MyTrait: Deps { ... }