These are chat archives for rust-lang/rust

30th
Apr 2019
Akos Vandra
@axos88
Apr 30 08:26
unless you use some global static magic - yuck :D
Akos Vandra
@axos88
Apr 30 08:34

So... I have a "trait" called Command (as in a command to an embedded chip with a response) with fields code and data, and a function send_data that sends the command and returns a bunch of bytes as a response. And I have a bunch of implementations for it. All implementations only differ by their implementation of code and data, basically only at initialization and how they allow the user to "view" the response. Eg. QuerySerial will respond with a Serial object to send_data, TurnOn will respond with a StatusCode, ReadMeasurment will respond with an f64.

Now all these implementations' sizes are fixed. Is there a way to avoid having to box them? Maybe they don't have to be trait implementations, but something else? It would be awesome to put the whole thing into an enum.

enum Command { 
  ReadSerial,
  TurnOn,
  ReadMeasurement,
  SetSerial(String),
  SetAlarm { min: f64, max: f64 }
}

which would somehow construct the byte stream that needs to be send (code and data), and then parse the response correctly. Any ideas on how to do this nicely?

anykey111
@anykey111
Apr 30 13:39
@axos88 It's not clear what you're trying to solve.
Writing nice wrapper for low level protocol or achive bit level compatibility of rust enums.
If you have control on both sides thats' a lot of options.
anykey111
@anykey111
Apr 30 13:51
As i see response doesn't contains original command id. Might be a problem for asynchronous events.
No checksum, that probably not an UART transport.
mandreyel
@mandreyel
Apr 30 17:03
This message was deleted
mandreyel
@mandreyel
Apr 30 17:08
oops, sorry, wrong channel!
Akos Vandra
@axos88
Apr 30 18:39
@anykey111 It is an UART transport, the checksum is calculated on the fly when sending the command. It's supposed to be a wrapper for the low level protocol.
And the command code can be deducted from the type
anykey111
@anykey111
Apr 30 21:22
Then maybe user should't even know about command details.
Just expose fn's.
impl Device {
    fn read_measurement(&self) -> Result<f64> {
        self.send_data(READ_CODE, NO_DATA)?;
        let resp = self.read_data(8)?;
        Ok(resp.to_f64())
    } 
}
Akos Vandra
@axos88
Apr 30 22:46
@anykey111 Hmm yeah, maybe your're right. It would have been nice though to be able to use some typed interface even internally to the module to make sure it doesn't send a ReadMeasurement command, and try to parse the result as a Serial by programming error.
WindSoilder
@WindSoilder
Apr 30 23:30
Hello :) Does anyone know how to get system proxies setting in rust? I can't find a useful way via the search engine :(
Denis Lisov
@tanriol
Apr 30 23:31
Which OSes do you need to support?
WindSoilder
@WindSoilder
Apr 30 23:33
Mainly MaxOS, Linux(Better to support distributions as much as possible), and Windows