Skip to content

Commit 822530c

Browse files
secrecy: add SecretSlice<S> (#1214)
Adds a type alias for `SecretBox<[S]>` as a more convenient way to work with boxed secret slices.
1 parent 23d134d commit 822530c

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

secrecy/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
extern crate alloc;
3838

39-
use alloc::{boxed::Box, string::String};
39+
use alloc::{boxed::Box, string::String, vec::Vec};
4040
use core::{
4141
any,
4242
fmt::{self, Debug},
@@ -167,6 +167,23 @@ impl<S: Zeroize + ?Sized> ExposeSecretMut<S> for SecretBox<S> {
167167
}
168168
}
169169

170+
/// Secret slice type.
171+
///
172+
/// This is a type alias for [`SecretBox<[S]>`] which supports some helpful trait impls.
173+
///
174+
/// Notably it has a [`From<Vec<S>>`] impl which is the preferred method for construction.
175+
pub type SecretSlice<S> = SecretBox<[S]>;
176+
177+
impl<S> From<Vec<S>> for SecretSlice<S>
178+
where
179+
S: Zeroize,
180+
[S]: Zeroize,
181+
{
182+
fn from(vec: Vec<S>) -> Self {
183+
Self::from(vec.into_boxed_slice())
184+
}
185+
}
186+
170187
/// Secret string type.
171188
///
172189
/// This is a type alias for [`SecretBox<str>`] which supports some helpful trait impls.

0 commit comments

Comments
 (0)