1 个不稳定版本
使用旧的 Rust 2015
0.1.0 | 2016 年 4 月 9 日 |
---|
#5 in #paradigms
用于 quandl-v3
5KB
Rust 的 traits 的 'has a' 关系
这个crate提供了一种替代方案,以填补 Rust 编程语言中缺失的功能。即,特质能够持有状态。
文档
http://proksima.github.io/has-doc/has/index.html
简单示例
#[macro_use]
extern crate has;
use has::*;
struct Apple;
trait ApplesContainer: HasMut<Vec<Apple>> {
fn take_apple(&mut self) -> Option<Apple> {
self.get_mut().pop()
}
fn put_apple(&mut self, apple: Apple) {
self.get_mut().push(apple);
}
}
#[derive(Default)]
struct Basket {
pub apples: Vec<Apple>,
}
impl ApplesContainer for Basket {}
impl_has!(Basket, Vec<Apple>, apples);
fn main() {
let mut basket = Basket::default();
basket.put_apple(Apple);
basket.put_apple(Apple);
basket.put_apple(Apple);
basket.take_apple();
assert_eq!(basket.apples.len(), 2);
}