diff --git a/src/cargo/util/auth.rs b/src/cargo/util/auth.rs index 20efc68089c..2e7606810d2 100644 --- a/src/cargo/util/auth.rs +++ b/src/cargo/util/auth.rs @@ -26,6 +26,16 @@ use super::config::CredentialCacheValue; /// /// This type does not implement `Display`, and has a `Debug` impl that hides /// the contained value. +/// +/// ``` +/// # use cargo::util::auth::Secret; +/// let token = Secret::from("super secret string"); +/// assert_eq!(format!("{:?}", token), "Secret { inner: \"REDACTED\" }"); +/// ``` +/// +/// Currently, we write a borrowed `Secret` as `Secret<&T>`. +/// The [`as_deref`](Secret::as_deref) and [`owned`](Secret::owned) methods can +/// be used to convert back and forth between `Secret` and `Secret<&str>`. #[derive(Clone, PartialEq, Eq)] pub struct Secret { inner: T, @@ -41,9 +51,11 @@ impl Secret { } /// Converts a `Secret` to a `Secret<&T::Target>`. - /// - /// For example, this can be used to convert from `&Secret` to - /// `Secret<&str>`. + /// ``` + /// # use cargo::util::auth::Secret; + /// let owned: Secret = Secret::from(String::from("token")); + /// let borrowed: Secret<&str> = owned.as_deref(); + /// ``` pub fn as_deref(&self) -> Secret<&::Target> where T: Deref, @@ -51,10 +63,12 @@ impl Secret { Secret::from(self.inner.deref()) } + /// Converts a `Secret` to a `Secret<&T>`. pub fn as_ref(&self) -> Secret<&T> { Secret::from(&self.inner) } + /// Converts a `Secret` to a `Secret` by applying `f` to the contained value. pub fn map(self, f: F) -> Secret where F: FnOnce(T) -> U, @@ -66,21 +80,25 @@ impl Secret { impl Secret<&T> { /// Converts a `Secret` containing a borrowed type to a `Secret` containing the /// corresponding owned type. - /// - /// For example, this can be used to convert from `Secret<&str>` to - /// `Secret`. + /// ``` + /// # use cargo::util::auth::Secret; + /// let borrowed: Secret<&str> = Secret::from("token"); + /// let owned: Secret = borrowed.owned(); + /// ``` pub fn owned(&self) -> Secret<::Owned> { Secret::from(self.inner.to_owned()) } } impl Secret> { + /// Converts a `Secret>` to a `Result, E>`. pub fn transpose(self) -> Result, E> { self.inner.map(|v| Secret::from(v)) } } impl> Secret { + /// Checks if the contained value is empty. pub fn is_empty(&self) -> bool { self.inner.as_ref().is_empty() }