Rust 中的继承与代码复用

在学习Rust过程当中忽然想到怎么实现继承,特别是用于代码复用的继承,因而在网上查了查,发现不是那么简单的。java

C++的继承

首先看看c++中是如何作的。node

例如要作一个场景结点的Node类和一个Sprite类继承它。c++

定义一个node基类git

struct Node {
    float x;
    float y;
    void move_to(float x, float y) {
        this->x = x;
        this->y = y;
    }
    virtual void draw() const {
        printf("node: x = %f, y = %f\n", x, y);
    }
};

再定义一个子类Sprite,重载draw方法:github

struct Sprite: public Node {
    virtual void draw() const {
        printf("sprite: x = %f, y = %f\n", x, y);
    }
};

能够把sprite做为一个Node来使用,而且能够重用Node中的move_to函数:函数

Node* sprite = new Sprite();
sprite->move_to(10, 10);
sprite->draw();

Rust中的继承

如今要用Rust作一样的事。定义一个Node基类:学习

struct Node {
    x: f32,
    y: f32,
}

impl Node {
    fn draw(&self) {
        println!("node: x={}, y={}", self.x, self.y)
    }

    fn move_to(&mut self, x: f32, y: f32) {
        self.x = x;
        self.y = y;
    }
}

定义子类的时候咱们遇到了麻烦:Rust里struct是不能继承的!this

struct Sprite: Node;

这么写会报错:code

error: `virtual` structs have been removed from the language

virtual struct是什么东西?原来Rust曾经有一个virtual struct的特性可使struct继承另外一个struct,可是被删掉了:(
RFC在这里。如今Rust的struct是不能继承的了。继承

使用 trait

Rust 里的 trait 是相似于 java 里 interface,能够继承的。咱们把 Node 定义为 trait。

trait Node {
    fn move_to(&mut self, x: f32, y: f32);
    fn draw(&self);
}

但咱们发现没有办法在 Node 中实现 move_to 方法,由于 trait 中不能有成员数据:x, y。

那只好在每一个子类中写各自的方法实现,例如咱们须要一个空Node类和一个Sprite类:

struct EmptyNode {
    x: f32,
    y: f32,
}

impl Node for EmptyNode {
    fn draw(&self) {
        println!("node: x={}, y={}", self.x, self.y)
    }

    fn move_to(&mut self, x: f32, y: f32) {
        self.x = x;
        self.y = y;
    }
}

struct Sprite {
    x: f32,
    y: f32,
}

impl Node for Sprite {
    fn draw(&self) {
        println!("sprite: x={}, y={}", self.x, self.y)
    }

    fn move_to(&mut self, x: f32, y: f32) {
        self.x = x;
        self.y = y;
    }
}

是否是以为有大量代码重复了?Sprite只须要重写 draw方法,但要把全部方法都实现一遍。若是要实现不少种 Node,每种都要实现一遍,那就要写吐血了。

组合

组合是一个代码重用的好方法。要重用代码时,组合并且比继承更能体现“has-a”的关系。咱们把 Node 从新定义为以前的 struct 基类,而后把 Node 放在 Sprite 中:

struct Node {
    x: f32,
    y: f32,
}

impl Node {
    fn draw(&self) {
        println!("node: x={}, y={}", self.x, self.y)
    }

    fn move_to(&mut self, x: f32, y: f32) {
        self.x = x;
        self.y = y;
    }
}

struct Sprite {
    node: Node
}

impl Sprite {
    fn draw(&self) {
        println!("sprite: x={}, y={}", self.node.x, self.node.y)
    }

    fn move_to(&mut self, x: f32, y: f32) {
        self.node.move_to(x, y);
    }
}

清爽了很多,美中不足的是还不能省略 move_to 方法,还要手动写一遍,简单调用 Node 中的同名方法。

组合和继承还有一些不一样的,好比不能把 Sprite 转型为 Node。

Deref & DerefMut trait

std::ops::Deref 用于重载取值运算符: *。这个重载能够返回其余类型,正好能够解决组合中不能转换类型的问题。

在这个例子中,因为 move_to 的 self 可变的,因此要实现 Deref 和 DerefMut

struct Sprite {
    node: Node
}

impl Sprite {
    fn draw(&self) {
        println!("sprite: x={}, y={}", self.node.x, self.node.y)
    }
}

impl Deref for Sprite {
    type Target = Node;

    fn deref<'a>(&'a self) -> &'a Node {
        &self.node
    }
}

impl DerefMut for Sprite {
    fn deref_mut<'a>(&'a mut self) -> &'a mut Node {
        &mut self.node
    }
}

以后就能够把 &Sprite 转换为 &Node

let mut sprite = Sprite{ node: Node { x: 10.0, y: 20.0 } };
let mut sprite_node: &mut Node = &mut sprite;
sprite_node.move_to(100.0, 100.0);

要注意的是对sprite_node的方法调用重载是不起做用的。若是 sprite_node.draw(),调用的仍是Node.draw(),而不是Sprite.draw()。

若是要调用子类的方法,必须有子类类型的变量来调用。

let mut sprite = Sprite{ node: Node { x: 10.0, y: 20.0 } };

// 这个大括号限制 mut borrow 范围
{
    let mut sprite_node: &mut Node = &mut sprite;
    sprite_node.move_to(100.0, 100.0);
    sprite.node.draw(); // 输出 node: x=100, y=100
} 

sprite.draw(); // 输出 sprite: x=100, y=100
相关文章
相关标签/搜索