pub struct ZeroingConfirmToken(/* private fields */);Expand description
关节归零确认令牌(Zero-token 类型模式)
安全机制:此类型只能通过三种方式创建:
- 从环境变量读取(
confirm_from_env()) - unsafe 创建(
new_unchecked()) - 测试创建(
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
impl ZeroingConfirmToken
Sourcepub fn confirm_from_env() -> Result<Self, ZeroingTokenError>
pub fn confirm_from_env() -> Result<Self, ZeroingTokenError>
Sourcepub unsafe fn new_unchecked() -> Self
pub unsafe fn new_unchecked() -> Self
不安全创建(供 GUI 应用使用)
⚠️ 安全契约:
调用此方法前,必须确保:
- 用户已在 UI 中明确确认归零操作的 danger
- 显示了清晰的警告信息
- 用户主动点击了“确认“按钮(或其他明确的确认动作)
§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
impl Clone for ZeroingConfirmToken
Source§fn clone(&self) -> ZeroingConfirmToken
fn clone(&self) -> ZeroingConfirmToken
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ZeroingConfirmToken
impl Debug for ZeroingConfirmToken
impl Copy for ZeroingConfirmToken
Auto Trait Implementations§
impl Freeze for ZeroingConfirmToken
impl RefUnwindSafe for ZeroingConfirmToken
impl Send for ZeroingConfirmToken
impl Sync for ZeroingConfirmToken
impl Unpin for ZeroingConfirmToken
impl UnwindSafe for ZeroingConfirmToken
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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