当特征需要的状态多于结构中包含的状态时,如何为结构实现特征?

How do I implement a trait for a struct when the trait requires more state than is contained in the struct?

当特征需要的状态多于结构中包含的状态时,如何为结构实现特征?例如,我将如何为下面显示的 Human 结构实现 Employee 特征?

struct Human {
    name: &str,
}

trait Employee {
    fn id(&self) -> i32;
    fn name(&self) -> &str;
}

impl Employee for Human {
    fn id(&self) -> i32 {
        // From where do I get the ID?
    }
    fn name(&self) -> &str {
        self.name
    }
}

我没有看到任何方法可以将其他状态塞入 impl 或特征中。

创建一个新的 HumanToEmployeeAdapter 结构来保存丢失的信息然后为新结构实现 Employee 特征是唯一的选择吗?

P.S。我的背景是 C#。以下是我将如何使用该语言处理它:

class Human
{
    public string Name { get; }

    public Human(string name) { Name = name; }
}

interface IEmployee
{
    int Id { get; }
    string Name { get; }
}

class HumanToEmployeeAdapter : IEmployee
{
    readonly Human _human;

    public int Id { get; }
    public string Name => _human.Name;

    public HumanToEmployeeAdapter(
        Human human,
        int id)
    {
        _human = human;
        Id = id;
    }
}

您会注意到这是 "create a new HumanToEmployeeAdapter struct" 路径。那么,这就是Rustaceans解决这个问题的方式吗?

您几乎可以完全准确地翻译您的 C# 代码,如下所示:

struct Human<'a> {
    name: &'a str,
}

trait Employee {
    fn id(&self) -> i32;
    fn name(&self) -> &str;
}

struct HumanToEmployeeAdapter<'a> {
    human: &'a Human<'a>,
    id: i32,
}

impl<'a> HumanToEmployeeAdapter<'a> {
    fn new(id: i32, human: &'a Human<'a>) -> Self {
        HumanToEmployeeAdapter { id, human }
    }
}

impl<'a> Employee for HumanToEmployeeAdapter<'a> {
    fn id(&self) -> i32 {
        self.id
    }

    fn name(&self) -> &str {
        self.human.name
    }
}

如果您的 Human 类型可以设为 Copy(其行为类似于 C# value type),那么您可以通过让 HumanToEmployeeAdapter 拥有 [=12] 来简化问题=],这意味着您不必担心引用的生命周期:

#[derive(Copy, Clone)]
struct Human<'a> {
    name: &'a str,
}

trait Employee {
    fn id(&self) -> i32;
    fn name(&self) -> &str;
}

struct HumanToEmployeeAdapter<'a> {
    human: Human<'a>,
    id: i32,
}

impl<'a> HumanToEmployeeAdapter<'a> {
    fn new(id: i32, human: Human<'a>) -> Self {
        HumanToEmployeeAdapter { id, human }
    }
}

impl<'a> Employee for HumanToEmployeeAdapter<'a> {
    fn id(&self) -> i32 {
        self.id
    }

    fn name(&self) -> &str {
        self.human.name
    }
}

请注意,您仍然需要跟踪 name 的生命周期,因为 &str 是一个引用。如果你把它变成一个拥有的 String,那么你就不需要 Human 的生命周期参数,但是 Human 就不可能是 Copy。这是因为 Strings 不能安全地复制到内存中,因为它们的 Drop impl(类似于 C# finalizer),如果 Rust 允许你这样做,这将导致双重释放 -这就是为什么它没有。