Skip to content

Instantly share code, notes, and snippets.

@3DRaven
Last active April 19, 2025 13:42
Show Gist options
  • Save 3DRaven/5cf97034082fd02c5a30ca0f70be7a24 to your computer and use it in GitHub Desktop.
Save 3DRaven/5cf97034082fd02c5a30ca0f70be7a24 to your computer and use it in GitHub Desktop.
Type-safe delegation over generics in Rust without the newtype pattern
pub struct GenericId<T>(pub i64, pub PhantomData<T>);

pub struct PhantomMyId;
pub type MyId = GenericId<PhantomMyId>;
pub struct PhantomOtherId;
pub type OtherId = GenericId<PhantomOtherId>;


pub fn foo(my_id: &MyId){

}
  1. It is type-safe; MyId!=OtherId.
  2. The delegate is transparent — no additional effort is required to call the delegate’s methods.
  3. You can use derive or the derive_more crate.
@3DRaven
Copy link
Author

3DRaven commented Apr 19, 2025

pub trait GenericSecretTrait: Debug + AsRef<String> {
    fn expose_secret(&self) -> &str {
        self.as_ref()
    }
}

*error*: type parameter `T` must be used as the type parameter for some local type. 
impl<T: GenericSecretTrait> Debug for T {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "***")
    }
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Validate, From, Into, new, AsRef)]
#[serde(transparent)]
pub struct GenericSecretTraitBased(#[garde(skip)] pub String);

impl GenericSecretTrait for GenericSecretTraitBased {}

The method described above is the shortest approach I've found to provide default trait implementations for many child types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment