审查生成的机密包装器

来自 智能指针
Rust 1.98 高级 8分钟 找出 4处问题

根据契约审查这个生成的智能指针式包装器。

拥有一个 API token,只提供显式的前缀检查,不额外复制或泄漏 token,并且清理时绝不记录 token。

Rust
use std::ops::Deref;

#[derive(Clone)]
struct SecretBox(String);

impl Deref for SecretBox {
    type Target = str;

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

impl Drop for SecretBox {
    fn drop(&mut self) {
        println!("dropping {}", self.0);
    }
}

fn main() {
    let secret = SecretBox(String::from("live_sk_123"));
    let backup = secret.clone();
    println!("accepted: {}", secret.starts_with("live_"));
    std::mem::forget(backup);
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误