Skip to content

Type of a serialport #118

Closed Answered by mlsvrts
DavidLibault asked this question in Q&A
Discussion options

You must be logged in to vote

Since serialport-rs uses a RAII philosophy, if you have access to a port it is also open.

I think this results in the issue you describe, so I might suggest something like:

port: Option<Box<dyn SerialPort>>

When you initialize your structure, the port will be None; when you acquire/open a port it will be Some(Box<dyn SerialPort>).

This does create some boilerplate when reading/writing to the port, since you need to be checking if the port is Some -- but I think this is conceptually the same as needing to check that a port is 'open'.

The other option, of course, is simply to open/create the port when you create your structure.

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by sirhcel
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #116 on August 06, 2023 11:55.