-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
secrecy: Allow opting in to PartialEq/Eq implementations for secrets for testing #632
Comments
How about It's slightly more cumbersome in that you have to import a trait and use |
@steven-joruk, which workaround did you end up using? Mind sharing an example? 🙇🏻♂️ |
Unfortunately, due to the orphan trait rule, I couldn't find a nice workaround for this, so I just made my own wrapper type and used that instead. use secrecy::{
ExposeSecret, Secret, Zeroize,
};
/// Secret<T> wrapper that impls PartialEq and Eq (so you can use it in BTreeSets and such)
pub struct SecretWrapper<T>(pub Secret<T>)
where
T: Zeroize;
impl<T> SecretWrapper<T>
where
T: Zeroize,
{
pub fn new(value: T) -> Self {
Self(Secret::new(value))
}
}
impl<T> Eq for SecretWrapper<T> where T: Zeroize + Eq {}
impl<T> PartialEq for SecretWrapper<T>
where
T: Zeroize + PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.expose_secret() == other.expose_secret()
}
} You'll need to impl any other traits you may need (Clone/Debug/etc) |
I would find it useful when unit testing equivalence of values which contain secrets.
I can work around it by manually implementing the traits for my structs which contain secrets, but maybe others would find this useful as well.
Perhaps it should be an opt-in feature flag so that it could support more test project configurations?
The text was updated successfully, but these errors were encountered: