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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ serde = ["dep:serde"]
zeroize = ["dep:zeroize"]

[dependencies]
bytemuck = { version = "1.15", default-features = false }
fake = { version = "2.5", optional = true, default-features = false }
rand = { version = "0.8", optional = true, default-features = false }
serde = { version = "1.0", optional = true, default-features = false }
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]

#[cfg(feature = "std")]
mod error;
Expand Down
37 changes: 36 additions & 1 deletion src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::Secret;

use core::ops;
use core::{
ops,
ops::{Deref, DerefMut},
};

macro_rules! ops {
{ ($type:tt, $trait:ident, $method:ident), $($tt:tt)* } => {
Expand Down Expand Up @@ -69,3 +72,35 @@ ops! {
(unary, Neg, neg),
(unary, Not, not),
}

use bytemuck::TransparentWrapper;

/// We introduce this private wrapper around `Secret` to safely implement [TransparentWrapper] without
/// inadvertently providing a way for `Secret`s to be exposed without using `Secret::expose_secret`
#[repr(transparent)]
struct Wrapper<T: ?Sized>(Secret<T>);

// SAFETY: `Secret` and `Wrapper` both contains only a single field and are `#[repr(transparent)]`.
// This meets the documented requirements [bytemuck::TransparentWrapper] as long as we
// do not override any of its methods.
unsafe impl<T: ?Sized> bytemuck::TransparentWrapper<T> for Wrapper<T> {}

impl<T> Deref for Secret<T>
where
T: Deref,
{
type Target = Secret<T::Target>;

fn deref(&self) -> &Self::Target {
&Wrapper::wrap_ref(self.0.deref()).0
}
}

impl<T> DerefMut for Secret<T>
where
T: DerefMut,
{
fn deref_mut(&mut self) -> &mut Secret<T::Target> {
&mut Wrapper::wrap_mut(self.0.deref_mut()).0
}
}