Generic struct or specialised struct implementing trait?

⚓ Rust    📅 2025-06-30    👤 surdeus    👁️ 5      

surdeus

Warning

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

I'm working on my first Rust project, which will include a library.
In this library, I'm currently defining traits (eg AsfaloadSecretKey), and the goal is to have multiple structs (eg MinisignSecretKey) implement these traits (eg impl AsfaloadSecretKey for MinisignSecretKey).

I'm wondering if I should replace

struct MinisignSecretKey {
    key: minisign::SecretKey,
}

by

// Define generic struct at same level as trait
struct AsfaloadSecretKey<K> {
    key: K,
}
// Implement the trait for the specialised struct
impl AsfaloadSecretKey for AsfaloadSecretKey<minisign::SecretKey> {
//...
}

What are the up/downsides of my each approach? Does my current approach have limitations?
Could anyone share their experience?

Thanks in advance!

1 post - 1 participant

Read full topic

🏷️ rust_feed