Rust:将借来的结构传递给借来的枚举?

时间:2019-09-08 10:20:31

标签: enums rust borrow-checker

我正在尝试将借来的结构传递给借来的枚举。

#[derive(Copy, Clone)]
pub struct CustomerData {
    // Many fields about customers
}

#[derive(Copy, Clone)]
pub struct EmployeeData {
    // Many fields about employees
}

pub enum Person {
    Customer(CustomerData),
    Employee(EmployeeData)
}

fn do_something_with_customer(customer: &CustomerData) {
    let person = &Person::Customer(customer);

    // This would work, but this can be a large struct.
    // let person = &Person::Customer(customer.clone());

    general_method(person);
}

fn do_something_with_employee(employee: &EmployeeData) {
    let person = &Person::Employee(employee);

    // This would work, but this can be a large struct.
    // let person = &Person::Employee(employee.clone());

    general_method(person);
}

fn general_method(person: &Person) {

}

fn main() {
    let person = Person::Customer(CustomerData { });

    match &person {
        Person::Customer(data) => {
            do_something_with_customer(data);
        }
        Person::Employee(data) => {
            do_something_with_employee(data);
        }
    }
}

编译会给我结果:

error[E0308]: mismatched types
  --> src/main.rs:19:36
   |
19 |     let person = &Person::Customer(customer);
   |                                    ^^^^^^^^
   |                                    |
   |                                    expected struct `CustomerData`, found reference
   |                                    help: consider dereferencing the borrow: `*customer`
   |
   = note: expected type `CustomerData`
              found type `&CustomerData`

error[E0308]: mismatched types
  --> src/main.rs:28:36
   |
28 |     let person = &Person::Employee(employee);
   |                                    ^^^^^^^^
   |                                    |
   |                                    expected struct `EmployeeData`, found reference
   |                                    help: consider dereferencing the borrow: `*employee`
   |
   = note: expected type `EmployeeData`
              found type `&EmployeeData`

我知道Rust编译器不允许我这样做,但是考虑到我也将借用我的结构体的枚举,我觉得应该可以。

此方案是否有模式/解决方法?也许使用Rc类型?在这种情况下,我不希望用它乱扔我的代码。

use std::rc::Rc;

#[derive(Copy, Clone)]
pub struct CustomerData {
    // Many fields about customers
}

#[derive(Copy, Clone)]
pub struct EmployeeData {
    // Many fields about employees
}

pub enum Person {
    Customer(Rc<CustomerData>),
    Employee(Rc<EmployeeData>)
}

fn do_something_with_customer(customer: Rc<CustomerData>) {
    let person = &Person::Customer(customer);

    // This would work, but this can be a large struct.
    // let person = &Person::Customer(customer.clone());

    general_method(person);
}

fn do_something_with_employee(employee: Rc<EmployeeData>) {
    let person = &Person::Employee(employee);

    // This would work, but this can be a large struct.
    // let person = &Person::Employee(employee.clone());

    general_method(person);
}

fn general_method(person: &Person) {

}

fn main() {
    let person = Person::Customer(Rc::new(CustomerData { }));

    match &person {
        Person::Customer(data) => {
            do_something_with_customer(data.clone());
        }
        Person::Employee(data) => {
            do_something_with_employee(data.clone());
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您已经错误地识别了问题,并且编译器对其错误注释进行了现场检查。

您这样定义枚举:

pub enum Person {
    Customer(CustomerData),
    Employee(EmployeeData)
}

但是随后您决定您的枚举成员应为Person::Customer(&CustomerData)

fn do_something_with_customer(customer: &CustomerData) {
    let person = &Person::Customer(customer);

引用不是可传递的。因为&CustomerData是引用,并不意味着整个枚举都将是对真实数据(即&Person::Customer(CustomerData))的引用。

有两种解决方法:显而易见的是查看CustomerData是否实现Copy。如果是这样,您可以取消引用(并因此隐式复制):

fn do_something_with_customer(customer: &CustomerData) {
    let person = Person::Customer(*customer);

(这是编译器的建议,因此我很确定您的类型实现了Copy

另一个选项是在类型上#[derive(Clone)],然后调用customer.clone()。同样,以额外分配为代价。

如果您确实希望在枚举中引用,则需要将枚举定义更改为:

pub enum Person<'a> {
    Customer(&'a CustomerData),
    Employee(&'a EmployeeData)
}

处理对象属性现在是引用以及所有相关问题的事实。