-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
Sensitive
which can be used for hiding Debug
impls
Use it everywhere where we used to manually implement `Debug` on types to hide information. Alternative to #2463. Fixes #2459.
- Loading branch information
Showing
6 changed files
with
82 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::{ | ||
fmt, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
use serde::{ | ||
de::{Deserialize, Deserializer}, | ||
ser::{Serialize, Serializer}, | ||
}; | ||
|
||
#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
pub struct Sensitive<T>(pub T); | ||
|
||
impl<T> fmt::Debug for Sensitive<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.pad("***") | ||
} | ||
} | ||
|
||
impl<T, U: ?Sized> AsRef<U> for Sensitive<T> | ||
where | ||
T: AsRef<U>, | ||
{ | ||
fn as_ref(&self) -> &U { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl<T> Deref for Sensitive<T> { | ||
type Target = T; | ||
fn deref(&self) -> &T { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T> DerefMut for Sensitive<T> { | ||
fn deref_mut(&mut self) -> &mut T { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Sensitive<T> { | ||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Sensitive<T>, D::Error> { | ||
T::deserialize(deserializer).map(Sensitive) | ||
} | ||
} | ||
|
||
impl<T: Serialize> Serialize for Sensitive<T> { | ||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { | ||
self.0.serialize(serializer) | ||
} | ||
} |