Skip to main content

stefans_utils/
as_secret.rs

1use crate::secret::Secret;
2
3pub trait AsSecret<T> {
4    #[allow(clippy::wrong_self_convention)]
5    fn as_secret(self) -> Secret<T>;
6}
7
8impl<T> AsSecret<T> for Secret<T> {
9    fn as_secret(self) -> Secret<T> {
10        self
11    }
12}
13
14impl<T> AsSecret<T> for T {
15    fn as_secret(self) -> Secret<T> {
16        Secret::new(self)
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    use crate::as_secret::AsSecret;
23    use crate::secret::Secret;
24
25    #[test]
26    fn hmm() {
27        fn assert_eq_as_secret(value: impl AsSecret<&'static str>, expected: &str) {
28            assert_eq!(
29                value.as_secret().expose_ref(),
30                Secret::new(expected).expose_ref()
31            );
32        }
33
34        assert_eq_as_secret("hello", "hello");
35        assert_eq_as_secret(Secret::new("hello"), "hello");
36    }
37}