serial2/into_settings.rs
1use crate::Settings;
2
3/// Trait for objects that can configure a serial port.
4///
5/// The simplest option is to pass a `u32`, which is used to set the baud rate of the port.
6/// That will also disable all OS level input and output modification,
7/// configure a character size of 8 bits with 1 stop bit,
8/// and it disables paritity checks and flow control.
9///
10/// For more control, it is possible to pass a `Fn(Settings) -> std::io::Result<Settings>`.
11/// If you do, you will generally want to start with a call to [`Settings::set_raw()`].
12///
13/// To open a serial port without modifying any settings, pass [`KeepSettings`].
14pub trait IntoSettings {
15 /// Apply the configuration to an existing [`Settings`] struct.
16 fn apply_to_settings(self, settings: &mut Settings) -> std::io::Result<()>;
17}
18
19impl<F> IntoSettings for F
20where
21 F: FnOnce(Settings) -> std::io::Result<Settings>,
22{
23 fn apply_to_settings(self, settings: &mut Settings) -> std::io::Result<()> {
24 *settings = (self)(settings.clone())?;
25 Ok(())
26 }
27}
28
29impl IntoSettings for u32 {
30 fn apply_to_settings(self, settings: &mut Settings) -> std::io::Result<()> {
31 settings.set_raw();
32 settings.set_baud_rate(self)?;
33 Ok(())
34 }
35}
36
37/// A serial port "configuration" that simply keeps all existing settings.
38///
39/// You can pass this to [`SerialPort::open()`][crate::SerialPort::open()] to prevent it from changing any port settings.
40///
41/// Note: many platforms reset the configuration of a serial port when it is no longer in use.
42/// You should normally explicitly configure the settings that you care about.
43pub struct KeepSettings;
44
45impl IntoSettings for KeepSettings {
46 fn apply_to_settings(self, _settings: &mut Settings) -> std::io::Result<()> {
47 Ok(())
48 }
49}