Skip to main content

ZeroingConfirmToken

Struct ZeroingConfirmToken 

Source
pub struct ZeroingConfirmToken(/* private fields */);
Expand description

关节归零确认令牌(Zero-token 类型模式)

安全机制:此类型只能通过三种方式创建:

  1. 从环境变量读取(confirm_from_env()
  2. unsafe 创建(new_unchecked()
  3. 测试创建(confirm_for_test(),仅测试可用)

这确保了用户明确确认了归零操作的 danger。

§设计说明

这是一个 Zero-cost 类型安全 模式:

  • 类型大小:0 字节(ZST,零大小类型)
  • 运行时开销:0
  • 编译期检查:✅

§示例

// 从环境变量读取(推荐)
let token = ZeroingConfirmToken::confirm_from_env()?;

// 或使用 unsafe(仅用于 GUI)
let token = unsafe { ZeroingConfirmToken::new_unchecked() };

Implementations§

Source§

impl ZeroingConfirmToken

Source

pub fn confirm_from_env() -> Result<Self, ZeroingTokenError>

从环境变量确认(推荐用于 CLI 应用)

环境变量PIPPER_ZEROING_CONFIRM 期望值I_CONFIRM_ZEROING_IS_DANGEROUS

§错误
  • 环境变量未设置
  • 环境变量值不匹配
§示例
// 用户需要在命令行明确确认:
// export PIPER_ZEROING_CONFIRM=I_CONFIRM_ZEROING_IS_DANGEROUS
let token = ZeroingConfirmToken::confirm_from_env()?;
Source

pub unsafe fn new_unchecked() -> Self

不安全创建(供 GUI 应用使用)

⚠️ 安全契约

调用此方法前,必须确保:

  1. 用户已在 UI 中明确确认归零操作的 danger
  2. 显示了清晰的警告信息
  3. 用户主动点击了“确认“按钮(或其他明确的确认动作)
§Safety

调用者必须保证用户已经明确确认了归零操作的 danger。 此函数绕过了环境变量检查,因此调用者有责任确保用户同意。

§示例
// 显示确认对话框
if show_confirmation_dialog() {
    // ⚠️ 用户已确认,使用 unsafe 跳过检查
    let token = unsafe { ZeroingConfirmToken::new_unchecked() };
} else {
    return Err(Box::new(io::Error::new(
        io::ErrorKind::Other,
        "User cancelled"
    )) as Box<dyn std::error::Error>);
}

Trait Implementations§

Source§

impl Clone for ZeroingConfirmToken

Source§

fn clone(&self) -> ZeroingConfirmToken

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ZeroingConfirmToken

Source§

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

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

impl Copy for ZeroingConfirmToken

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more