我有一个管理多个传感器的结构。我有陀螺仪,加速度计,磁强计,气压计和温度计。所有这些都是特征。
pub struct SensorManager {
barometer: Barometer + Sized,
thermometer: Thermometer + Sized,
gyroscope: Gyroscope + Sized,
accelerometer: Accelerometer + Sized,
magnetometer: Magnetometer + Sized
}我需要使它模块化,以便在配置文件中,您可以指定您正在使用的传感器。
问题是有些传感器重叠。例如:一个人可以有一个包含陀螺仪、加速度计和磁强计的LSM9DS0,而另一个人可以有一个L3GD20陀螺仪和一个LSM303D加速度计,磁强计。
在C++中,我会存储指针或引用,但我不知道如何在Rust中安全地实现这一点。
短版本:需要引用每个传感器作为此结构的成员。其中一些引用具有相同的对象。
发布于 2017-08-06 17:21:32
在C++中,我会存储指针或引用
铁锈不是外星人。你也做同样的事。主要的区别在于,锈蚀会阻止你通过两条不同的路径变异一件东西,或者有一个悬挂在空中的引用。
回答您的问题有的许多潜在的解决方案。例如,您没有描述您是否需要能够对传感器进行变异,或者描述传感器是否会超过管理器、是否涉及线程等等。所有这些都会影响到微优化代码的性能。
最灵活的解决办法是:
Rc或Arc提供的。这允许多个东西拥有传感器。RefCell或Mutex提供的。这将从编译时到运行时一次执行单个变异引用。use std::{cell::RefCell, rc::Rc};
trait Barometer {
fn get(&self) -> i32;
fn set(&self, value: i32);
}
trait Thermometer {
fn get(&self) -> i32;
fn set(&self, value: i32);
}
trait Gyroscope {
fn get(&self) -> i32;
fn set(&self, value: i32);
}
struct Multitudes;
impl Barometer for Multitudes {
fn get(&self) -> i32 {
1
}
fn set(&self, value: i32) {
println!("Multitudes barometer set to {}", value)
}
}
impl Thermometer for Multitudes {
fn get(&self) -> i32 {
2
}
fn set(&self, value: i32) {
println!("Multitudes thermometer set to {}", value)
}
}
struct AutoGyro;
impl Gyroscope for AutoGyro {
fn get(&self) -> i32 {
3
}
fn set(&self, value: i32) {
println!("AutoGyro gyroscope set to {}", value)
}
}
struct SensorManager {
barometer: Rc<RefCell<dyn Barometer>>,
thermometer: Rc<RefCell<dyn Thermometer>>,
gyroscope: Rc<RefCell<dyn Gyroscope>>,
}
impl SensorManager {
fn new(
barometer: Rc<RefCell<dyn Barometer>>,
thermometer: Rc<RefCell<dyn Thermometer>>,
gyroscope: Rc<RefCell<dyn Gyroscope>>,
) -> Self {
Self {
barometer,
thermometer,
gyroscope,
}
}
fn dump_info(&self) {
let barometer = self.barometer.borrow();
let thermometer = self.thermometer.borrow();
let gyroscope = self.gyroscope.borrow();
println!(
"{}, {}, {}",
barometer.get(),
thermometer.get(),
gyroscope.get()
);
}
fn update(&self) {
self.barometer.borrow_mut().set(42);
self.thermometer.borrow_mut().set(42);
self.gyroscope.borrow_mut().set(42);
}
}
fn main() {
let multi = Rc::new(RefCell::new(Multitudes));
let gyro = Rc::new(RefCell::new(AutoGyro));
let manager = SensorManager::new(multi.clone(), multi, gyro);
manager.dump_info();
manager.update();
}气压计:气压计+大小,
你真的不想这么做。Barometer既是特性,也是类型,但该类型没有大小。它总是需要在指针后面引用(&Barometer、Box<Barometer>、RefCell<Barometer>等)。
另请参阅:
https://stackoverflow.com/questions/45534394
复制相似问题