Skip to content

fix warnings #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions serial-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ fn probe<P: SerialPort>(port: &mut P) -> io::Result<()> {
let mut buf: Vec<u8> = (0..255).collect();

// configuration
try!(port.reconfigure(&|settings| {
try!(settings.set_baud_rate(serial::Baud9600));
port.reconfigure(&|settings| {
settings.set_baud_rate(serial::Baud9600)?;
settings.set_char_size(serial::Bits8);
settings.set_parity(serial::ParityNone);
settings.set_stop_bits(serial::Stop1);
settings.set_flow_control(serial::FlowNone);
Ok(())
}));
})?;

// I/O
try!(port.set_timeout(Duration::from_millis(100)));
try!(port.write(&buf[..]));
try!(port.read(&mut buf[..]));
port.set_timeout(Duration::from_millis(99))?;
port.write(&buf[..])?;
port.read(&mut buf[..])?;

// control signals
try!(port.set_dtr(true));
try!(port.read_dsr());
port.set_dtr(true)?;
port.read_dsr()?;

Ok(())
}
Expand Down Expand Up @@ -91,20 +91,20 @@ impl<P: SerialPort> Greet for Handle<P> {
fn get_name(&mut self) -> serial::Result<String> {
let mut name = String::new();

try!(self.port.write("What is your name? "));
try!(self.port.read_to_string(&mut name));
self.port.write("What is your name? ")?;
self.port.read_to_string(&mut name)?;

Ok(name)
}

fn say_hello(&mut self, name: &String) -> serial::Result<()> {
try!(writeln!(&mut self.port, "Hello, {}!", name));
writeln!(&mut self.port, "Hello, {}!", name)?;
Ok(())
}
}

fn greet(greeter: &mut Greet) -> serial::Result<()> {
let name = try!(greeter.get_name());
let name = greeter.get_name()?;

greeter.say_hello(name)
}
Expand Down
18 changes: 12 additions & 6 deletions serial-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,10 @@ pub trait SerialPort: io::Read + io::Write {
/// })
/// }
/// ```
fn reconfigure(&mut self, setup: &Fn(&mut SerialPortSettings) -> ::Result<()>) -> ::Result<()>;
fn reconfigure(
&mut self,
setup: &dyn Fn(&mut dyn SerialPortSettings) -> ::Result<()>,
) -> ::Result<()>;

/// Sets the state of the RTS (Request To Send) control signal.
///
Expand Down Expand Up @@ -585,9 +588,9 @@ impl<T> SerialPort for T
}

fn configure(&mut self, settings: &PortSettings) -> ::Result<()> {
let mut device_settings = try!(T::read_settings(self));
let mut device_settings = T::read_settings(self)?;

try!(device_settings.set_baud_rate(settings.baud_rate));
device_settings.set_baud_rate(settings.baud_rate)?;
device_settings.set_char_size(settings.char_size);
device_settings.set_parity(settings.parity);
device_settings.set_stop_bits(settings.stop_bits);
Expand All @@ -596,9 +599,12 @@ impl<T> SerialPort for T
T::write_settings(self, &device_settings)
}

fn reconfigure(&mut self, setup: &Fn(&mut SerialPortSettings) -> ::Result<()>) -> ::Result<()> {
let mut device_settings = try!(T::read_settings(self));
try!(setup(&mut device_settings));
fn reconfigure(
&mut self,
setup: &dyn Fn(&mut dyn SerialPortSettings) -> ::Result<()>,
) -> ::Result<()> {
let mut device_settings = T::read_settings(self)?;
setup(&mut device_settings)?;
T::write_settings(self, &device_settings)
}

Expand Down
3 changes: 1 addition & 2 deletions serial-unix/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core;

use std::error::Error;
use std::ffi::CStr;
use std::io;
use std::str;
Expand Down Expand Up @@ -30,7 +29,7 @@ pub fn from_io_error(io_error: io::Error) -> core::Error {
match io_error.raw_os_error() {
Some(errno) => from_raw_os_error(errno),
None => {
let description = io_error.description().to_string();
let description = io_error.to_string();

core::Error::new(core::ErrorKind::Io(io_error.kind()), description)
}
Expand Down
8 changes: 4 additions & 4 deletions serial-unix/src/tty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl TTYPort {
}

// apply initial settings
let settings = try!(port.read_settings());
try!(port.write_settings(&settings));
let settings = port.read_settings()?;
port.write_settings(&settings)?;

Ok(port)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ impl AsRawFd for TTYPort {

impl io::Read for TTYPort {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
try!(super::poll::wait_read_fd(self.fd, self.timeout));
super::poll::wait_read_fd(self.fd, self.timeout)?;

let len = unsafe {
libc::read(self.fd, buf.as_ptr() as *mut c_void, buf.len() as size_t)
Expand All @@ -143,7 +143,7 @@ impl io::Read for TTYPort {

impl io::Write for TTYPort {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
try!(super::poll::wait_write_fd(self.fd, self.timeout));
super::poll::wait_write_fd(self.fd, self.timeout)?;

let len = unsafe {
libc::write(self.fd, buf.as_ptr() as *mut c_void, buf.len() as size_t)
Expand Down
2 changes: 1 addition & 1 deletion serial-windows/src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl COMPort {
timeout: timeout,
};

try!(port.set_timeout(timeout));
port.set_timeout(timeout)?;
Ok(port)
}
else {
Expand Down
12 changes: 6 additions & 6 deletions serial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ fn main() {
}

fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
try!(port.reconfigure(&|settings| {
try!(settings.set_baud_rate(serial::Baud9600));
port.reconfigure(&|settings| {
settings.set_baud_rate(serial::Baud9600)?;
settings.set_char_size(serial::Bits8);
settings.set_parity(serial::ParityNone);
settings.set_stop_bits(serial::Stop1);
settings.set_flow_control(serial::FlowNone);
Ok(())
}));
})?;

try!(port.set_timeout(Duration::from_millis(1000)));
port.set_timeout(Duration::from_millis(1000))?;

let mut buf: Vec<u8> = (0..255).collect();

try!(port.write(&buf[..]));
try!(port.read(&mut buf[..]));
port.write(&buf[..])?;
port.read(&mut buf[..])?;

Ok(())
}
Expand Down
20 changes: 10 additions & 10 deletions serial/examples/probe_pins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ fn main() {
}

fn probe_pins<T: SerialPort>(port: &mut T) -> serial::Result<()> {
try!(port.configure(&SETTINGS));
try!(port.set_timeout(Duration::from_millis(100)));
port.configure(&SETTINGS)?;
port.set_timeout(Duration::from_millis(100))?;

try!(port.set_rts(false));
try!(port.set_dtr(false));
port.set_rts(false)?;
port.set_dtr(false)?;

let mut rts = false;
let mut dtr = false;
Expand All @@ -38,20 +38,20 @@ fn probe_pins<T: SerialPort>(port: &mut T) -> serial::Result<()> {

if toggle {
rts = !rts;
try!(port.set_rts(rts));
port.set_rts(rts)?;
}
else {
dtr = !dtr;
try!(port.set_dtr(dtr));
port.set_dtr(dtr)?;
}

println!("RTS={:5?} DTR={:5?} CTS={:5?} DSR={:5?} RI={:5?} CD={:?}",
rts,
dtr,
try!(port.read_cts()),
try!(port.read_dsr()),
try!(port.read_ri()),
try!(port.read_cd()));
port.read_cts()?,
port.read_dsr()?,
port.read_ri()?,
port.read_cd()?);

toggle = !toggle;
}
Expand Down
9 changes: 4 additions & 5 deletions serial/examples/read_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ extern crate serial;
use std::env;
use std::time::Duration;

use std::io::prelude::*;
use serial::prelude::*;

const SETTINGS: serial::PortSettings = serial::PortSettings {
Expand All @@ -24,16 +23,16 @@ fn main() {
}

fn interact<T: SerialPort>(port: &mut T) -> serial::Result<()> {
try!(port.configure(&SETTINGS));
try!(port.set_timeout(Duration::from_secs(1)));
port.configure(&SETTINGS)?;
port.set_timeout(Duration::from_secs(1))?;

let mut buf: Vec<u8> = (0..255).collect();

println!("writing bytes");
try!(port.write(&buf[..]));
port.write(&buf[..])?;

println!("reading bytes");
try!(port.read(&mut buf[..]));
port.read(&mut buf[..])?;

Ok(())
}