Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ let encryption_key = Secret::new("hello world");
assert_eq!("hello world", *encryption_key.expose_secret())
```

The secret may alternatively be transformed into another secret value
using the [map][Secret::map] method, which acts similarly to
[Option::map][Option::map].

```rust
use redact::Secret;

let encryption_key = Secret::new("hello world").map(str::to_string);
assert_eq!("hello world", *encryption_key.expose_secret())
```

The `Secret` type doubles as a useful documentation tool.
Documenting values maintainers should be careful with.

Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ impl<T> Secret<T> {
pub const fn expose_secret(&self) -> &T {
&self.0
}
/// See [module level documentation][crate]
#[inline]
#[must_use = "the secret will be dropped if not used"]
pub fn map<F, U>(self, mapper: F) -> Secret<U>
where
F: FnOnce(T) -> U,
{
Secret::new(mapper(self.0))
}
}

impl<T> From<T> for Secret<T> {
Expand Down