Skip to content

Commit

Permalink
secrecy: add SecretSlice<S> (#1214)
Browse files Browse the repository at this point in the history
Adds a type alias for `SecretBox<[S]>` as a more convenient way to work
with boxed secret slices.
  • Loading branch information
tony-iqlusion authored Sep 17, 2024
1 parent 23d134d commit 822530c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion secrecy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

extern crate alloc;

use alloc::{boxed::Box, string::String};
use alloc::{boxed::Box, string::String, vec::Vec};
use core::{
any,
fmt::{self, Debug},
Expand Down Expand Up @@ -167,6 +167,23 @@ impl<S: Zeroize + ?Sized> ExposeSecretMut<S> for SecretBox<S> {
}
}

/// Secret slice type.
///
/// This is a type alias for [`SecretBox<[S]>`] which supports some helpful trait impls.
///
/// Notably it has a [`From<Vec<S>>`] impl which is the preferred method for construction.
pub type SecretSlice<S> = SecretBox<[S]>;

impl<S> From<Vec<S>> for SecretSlice<S>
where
S: Zeroize,
[S]: Zeroize,
{
fn from(vec: Vec<S>) -> Self {
Self::from(vec.into_boxed_slice())
}
}

/// Secret string type.
///
/// This is a type alias for [`SecretBox<str>`] which supports some helpful trait impls.
Expand Down

0 comments on commit 822530c

Please sign in to comment.