From 822530c9ab1dfc3c326d0d4b06201e8c9f3dd803 Mon Sep 17 00:00:00 2001 From: "Tony Arcieri (iqlusion)" Date: Tue, 17 Sep 2024 16:40:39 -0600 Subject: [PATCH] secrecy: add `SecretSlice` (#1214) Adds a type alias for `SecretBox<[S]>` as a more convenient way to work with boxed secret slices. --- secrecy/src/lib.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/secrecy/src/lib.rs b/secrecy/src/lib.rs index fef8f404..48570aeb 100644 --- a/secrecy/src/lib.rs +++ b/secrecy/src/lib.rs @@ -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}, @@ -167,6 +167,23 @@ impl ExposeSecretMut for SecretBox { } } +/// Secret slice type. +/// +/// This is a type alias for [`SecretBox<[S]>`] which supports some helpful trait impls. +/// +/// Notably it has a [`From>`] impl which is the preferred method for construction. +pub type SecretSlice = SecretBox<[S]>; + +impl From> for SecretSlice +where + S: Zeroize, + [S]: Zeroize, +{ + fn from(vec: Vec) -> Self { + Self::from(vec.into_boxed_slice()) + } +} + /// Secret string type. /// /// This is a type alias for [`SecretBox`] which supports some helpful trait impls.