Hey,
I want to add an auto object to my Modbus crate (IO protocol for industrial applications).
It should be possible to set a Coil (Bit) and get a ScopedCoil
object back. If that object get's out of scope, the Coil should be automatically toggled back by implementing Drop
for ScopedCoil
.
The Problem is, that all manipulation methods are in a Transport
connection object. So it must be borrowed/referenced in the ScopedCoil
object, to call the toggle method in drop()
.
pub enum CoilDropFunction {
On,
Off,
Toggle,
}
pub struct ScopedCoil {
pub address: u16,
pub drop_value: Coil,
pub transport: RefCell<Transport>,
}
impl Drop for ScopedCoil {
fn drop(&mut self) {
self.transport.borrow_mut().write_single_coil(self.address, self.drop_value).unwrap()
}
}
impl ScopedCoil {
fn new(transport: &mut Transport,
address: u16,
value: Coil,
fun: CoilDropFunction)
-> ModbusResult<ScopedCoil> {
try!(transport.write_single_coil(address, value));
let drop_value = match fun {
CoilDropFunction::On => Coil::On,
CoilDropFunction::Off => Coil::Off,
CoilDropFunction::Toggle if value == Coil::On => Coil::Off,
CoilDropFunction::Toggle if value == Coil::Off => Coil::On,
_ => panic!("Impossible drop function"),
};
Ok(ScopedCoil {
address: address,
drop_value: drop_value,
transport: RefCell::new(*transport), // <-- How can I store a ref to my transport
})
}
How can I store a reference (RefCell
?) to my transport object?
Thanks, Falco