Skip to main content

Secret

Struct Secret 

Source
pub struct Secret<T>
where T: Zeroize,
{ /* private fields */ }
Expand description

English:
Owns a value whose Zeroize::zeroize method is called before it is dropped.

Debug and Display write the literal [REDACTED] rather than formatting T. Access is explicit through Self::as_inner, Self::expose_secret, or Self::into_inner; this type implements neither Deref nor serde serialization or deserialization, even with the serde feature. Cloning copies the inner value into another independently owned Secret. Equality delegates to the inner type and does not promise constant-time comparison.

Zeroization follows T’s implementation and requires this wrapper’s destructor to run; it does not erase copies made elsewhere. Self::into_inner bypasses that destructor. If T::zeroize panics, the inner value’s destructor is not called by this wrapper. Send and Sync depend on T; no synchronization is added.

Formatting hides the value; the closure below explicitly reads its length.

日本語:
所有する値を破棄する前に、その値の Zeroize::zeroize を呼び出すラッパーです。

Debug と Display は T を書式出力せず、リテラル [REDACTED] を出力します。値へのアクセスには Self::as_inner、Self::expose_secret、Self::into_inner を明示的に使います。 Deref は実装せず、serde feature が有効でも serde のシリアライズ・デシリアライズは 実装しません。クローンは内包値を複製して別の Secret に所有させます。等値比較は内包型に 委譲し、定数時間での比較を保証しません。

ゼロ化の範囲は T の実装に従い、このラッパーのデストラクターが実行される必要があります。 別の場所に作られたコピーは消去しません。Self::into_inner はデストラクターを迂回します。 T::zeroize がパニックした場合、このラッパーは内包値のデストラクターを呼び出しません。 Send と Sync は T に依存し、同期機構は追加しません。

書式出力は値を隠します。次のクロージャーでは長さを明示的に読み取ります。

§Example / 使用例

use ddd_toolkit_core::Secret;

let secret = Secret::new(String::from("token"));
assert_eq!(format!("{secret:?}"), "[REDACTED]");
assert_eq!(format!("{secret}"), "[REDACTED]");
assert_eq!(secret.expose_secret(|value| value.len()), 5);

Implementations§

Source§

impl<T> Secret<T>
where T: Zeroize,

Source

pub fn new(value: T) -> Secret<T>

English:
Takes ownership of value without copying or zeroizing it at construction.

日本語:
構築時にコピーやゼロ化を行わず、value の所有権を受け取ります。

Source

pub fn as_inner(&self) -> &T

English:
Borrows the unredacted value for no longer than the borrow of self.

Callers can read or copy the value; those copies are not zeroized by this wrapper.

日本語:
self の借用期間を超えない参照として、秘匿化していない値を借用します。

呼び出し側は値を読み取ったりコピーしたりできますが、そのコピーはこのラッパーではゼロ化しません。

Source

pub fn into_inner(self) -> T

English:
Consumes the wrapper and returns its value without zeroizing it.

The wrapper’s destructor is not run. The caller becomes responsible for the returned value’s lifetime and any required zeroization.

日本語:
ラッパーを消費し、ゼロ化せずに内包値を返します。

ラッパーのデストラクターは実行しません。返された値の生存期間と、必要なゼロ化の管理は 呼び出し側の責任になります。

§Example / 使用例
use ddd_toolkit_core::Secret;
use zeroize::Zeroize;

let mut value = Secret::new(vec![1_u8, 2, 3]).into_inner();
assert_eq!(value, [1, 2, 3]);
value.zeroize();
Source

pub fn expose_secret<R>(&self, f: impl FnOnce(&T) -> R) -> R

English:
Calls f once with a shared reference to the value and returns its result.

This does not consume or zeroize the value. The closure can copy it or return a borrowed reference; the closure is not an isolation boundary. Any panic from f propagates.

日本語:
値への共有参照を渡して f を1回呼び出し、その結果を返します。

値の消費やゼロ化は行いません。クロージャーは値をコピーしたり借用した参照を返したりでき、 情報の隔離境界にはなりません。f がパニックした場合はそのまま伝播します。

Trait Implementations§

Source§

impl<T> Clone for Secret<T>
where T: Zeroize + Clone,

Source§

fn clone(&self) -> Secret<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Secret<T>
where T: Zeroize,

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Display for Secret<T>
where T: Zeroize,

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for Secret<T>
where T: Zeroize,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T> Eq for Secret<T>
where T: Zeroize + Eq,

Source§

impl<T> PartialEq for Secret<T>
where T: Zeroize + PartialEq,

Source§

fn eq(&self, other: &Secret<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Secret<T>
where ManuallyDrop<T>: Freeze,

§

impl<T> RefUnwindSafe for Secret<T>

§

impl<T> Send for Secret<T>
where ManuallyDrop<T>: Send,

§

impl<T> Sync for Secret<T>
where ManuallyDrop<T>: Sync,

§

impl<T> Unpin for Secret<T>
where ManuallyDrop<T>: Unpin,

§

impl<T> UnsafeUnpin for Secret<T>

§

impl<T> UnwindSafe for Secret<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.