Skip to content

Commit 08977b5

Browse files
secrecy: make integer primitive SecretSlices cloneable (#1236)
Impls `Clone` for `SecretSlice` when the `S` generic type is `CloneableSecret + Zeroize`. As originally requested in #1070, also marks the integer primitive types as `CloneableSecret`, which makes it possible to clone a `SecretSlice<u8>`. Closes #1233
1 parent ae44831 commit 08977b5

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

secrecy/src/lib.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,28 @@ where
184184
}
185185
}
186186

187+
impl<S> Clone for SecretSlice<S>
188+
where
189+
S: CloneableSecret + Zeroize,
190+
[S]: Zeroize,
191+
{
192+
fn clone(&self) -> Self {
193+
SecretBox {
194+
inner_secret: Vec::from(&*self.inner_secret).into_boxed_slice(),
195+
}
196+
}
197+
}
198+
199+
impl<S> Default for SecretSlice<S>
200+
where
201+
S: Zeroize,
202+
[S]: Zeroize,
203+
{
204+
fn default() -> Self {
205+
Vec::new().into()
206+
}
207+
}
208+
187209
/// Secret string type.
188210
///
189211
/// This is a type alias for [`SecretBox<str>`] which supports some helpful trait impls.
@@ -206,14 +228,30 @@ impl Clone for SecretString {
206228
}
207229

208230
impl Default for SecretString {
209-
fn default() -> SecretString {
231+
fn default() -> Self {
210232
String::default().into()
211233
}
212234
}
213235

214236
/// Marker trait for secrets which are allowed to be cloned
215237
pub trait CloneableSecret: Clone + Zeroize {}
216238

239+
// Mark integer primitives as cloneable secrets
240+
241+
impl CloneableSecret for i8 {}
242+
impl CloneableSecret for i16 {}
243+
impl CloneableSecret for i32 {}
244+
impl CloneableSecret for i64 {}
245+
impl CloneableSecret for i128 {}
246+
impl CloneableSecret for isize {}
247+
248+
impl CloneableSecret for u8 {}
249+
impl CloneableSecret for u16 {}
250+
impl CloneableSecret for u32 {}
251+
impl CloneableSecret for u64 {}
252+
impl CloneableSecret for u128 {}
253+
impl CloneableSecret for usize {}
254+
217255
/// Expose a reference to an inner secret
218256
pub trait ExposeSecret<S: ?Sized> {
219257
/// Expose secret: this is the only method providing access to a secret.

0 commit comments

Comments
 (0)