Conditionally adding trait bounds based on feature flags

⚓ Rust    📅 2025-06-28    👤 surdeus    👁️ 8      

surdeus

Warning

This post was published 43 days ago. The information described in this article may have changed.

I currently have code that looks somewhat like this:

pub trait Bla {
  #[clf(feature = "serde")]
  type Ta: A + B + C + D + E + F + G + Serialize

  #[clf(not(feature = "serde"))]
  type Ta: A + B + C + D + E + F + G

  #[clf(feature = "serde")]
  type Ta_two: H + I + J + K + L + M + Serialize

  #[clf(not(feature = "serde"))]
  type Ta_two: H + I + J + K + L + M

  fn func(x: Ta) -> usize;

...

}

Is there a better way of doing this?

3 posts - 2 participants

Read full topic

🏷️ rust_feed