Coder Social home page Coder Social logo

Idomatic APIs about serialport-rs HOT 9 OPEN

serialport avatar serialport commented on June 6, 2024
Idomatic APIs

from serialport-rs.

Comments (9)

zstewar1 avatar zstewar1 commented on June 6, 2024

If you're interested in this, I can port my pull request from gitlab and resubmit it here.

from serialport-rs.

jessebraham avatar jessebraham commented on June 6, 2024

Thank you for your offer!

I admittedly have only given the issue a quick read, but I think you make some good arguments! I figure now is as good a time as any to make API changes as well. I will need to read through the original comment thread again at some point.

I don't have a timeline for when I will get to it, but if you're willing to open a PR with your changes here I would encourage you to do so!

from serialport-rs.

jerrywrice avatar jerrywrice commented on June 6, 2024

Very good suggestions.

If I'm understanding this correctly, I think that the final bullet item under your Disadvantages section is actually a 'feature' suggestion and should be moved out of the 'Disadvantages' section. Or maybe I'm misreading it?

from serialport-rs.

zstewar1 avatar zstewar1 commented on June 6, 2024

from serialport-rs.

sirhcel avatar sirhcel commented on June 6, 2024

The disadvantage is that if the Serialport type is a concrete (struct) type rather than a trait, it is harder to mock out in unit tests. I just then spend much more time describing ways to make it easier to work around that disadvantage.

Does the serial port itself need to be a trait for mocking? I'm wondering to which extend mocking will be used and makes sense in unit tests. I've seen https://github.com/dbrgn/embedded-hal-mock where mocking happens with respect to traits of embedded-hal. This allows for example to mock I2C communication at the level of the I2C traits like i2c::Read, i2c::Write, and i2c::WriteRead but not the initialization of a certain I2C controller.

Wouldn't mocking io::Read and io::Write for a serial port be a Pareto-style solution for mocking general serial communication? Just as a first thought, mocking things like changing baud rate, parity, ... "in-flight" looks like where things get rough. In contrast, just mocking the read and write traits seems pretty straightforward to me. And if there is a really a good use case, we could still introduce for example a configuration trait for it.

from serialport-rs.

zstewar1 avatar zstewar1 commented on June 6, 2024

from serialport-rs.

jerrywrice avatar jerrywrice commented on June 6, 2024

Per 'sirhcel's comment above, with regards to this crate's unit test having the ability to operate without physical serial ports (using mocked hardware), I believe a key question to ask is how beneficial this approach can realistically be. Writing code that in-fact copies data streams around in memory (rather than actually transferring over a physical port) is quite difficult to accurately and precisely implement in a way that comes close to modeling real hardware with respect to behavior and timing. It's the intricacies of various platform I/O and serial hardware port behavior that offers the most benefit while unit testing. I also recognize that in an ideal world one would like to unit test their code without operator involvement and in a fully automated fashion, but in the world of physical hardware this is rarely feasible.

from serialport-rs.

sirhcel avatar sirhcel commented on June 6, 2024

That is esssentially what my proposal is -- make SerialPort a struct and let people mock read/write and optionally add a trait for the serialport controls, if needed. Note that because of how traits work in rust, anyone who really needs it could just write their own 'serialport controls' trait to test against and implement it for our SerialPort type, so I don't even think it's necessary to provide such a trait, but I wanted to describe the option in case other people thought it was important.

Alright, then we are on the same page. This is the way I would like to go forward with it. And just not confusing things: Are you referring to PR #34 from #28 (comment)?

from serialport-rs.

sirhcel avatar sirhcel commented on June 6, 2024

Per 'sirhcel's comment above, with regards to this crate's unit test having the ability to operate without physical serial ports (using mocked hardware), I believe a key question to ask is how beneficial this approach can realistically be. Writing code that in-fact copies data streams around in memory (rather than actually transferring over a physical port) is quite difficult to accurately and precisely implement in a way that comes close to modeling real hardware with respect to behavior and timing. It's the intricacies of various platform I/O and serial hardware port behavior that offers the most benefit while unit testing. I also recognize that in an ideal world one would like to unit test their code without operator involvement and in a fully automated fashion, but in the world of physical hardware this is rarely feasible.

Sorry I did not read the original message right in the first place. Now I understand that @zstewar1 talked about the possibilities to mock the proposed static serial port. Yes, a static serial port struct would be harder to mock completely. But to me this does not seem relevant as - as you already said - mocking timing behaviour and other nitty gritty details won't get you both finished and far.

So from my Pareto-perspective, implementing io::Read and io::Write is fine for almost anyone. And if there is a significant demand, I bet we will learn about it when working towards 5.0.

In theory, we could statically analyze code of our known and actively maintained dependents. But I have only vaguely heared about this and crater so far. Any show and tell is welcome.

from serialport-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.