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

时间:2019-02-25 16:14:49

标签: oop struct rust traits composition

当特征要求的状态多于结构所包含的状态时,如何为该结构实现特征?例如,如何为下面显示的Employee结构实现Human特性?

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;
    }
}

您会注意到,这是“创建新的HumanToEmployeeAdapter结构”路径。那么,这是Rustaceans解决此问题的方式吗?

1 个答案:

答案 0 :(得分:4)

您几乎可以完全翻译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拥有Human来简化事务,这意味着您不必担心引用的有效期:

#[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。这是因为String的{​​{1}}暗示(类似于C# finalizer)不能安全地复制到内存中,如果Rust允许您这样做,则会导致双重释放-这就是为什么不这样。

相关问题