From cdf730ca3cdc5ce7dfb284986d39398ac23187b2 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Wed, 28 Nov 2018 10:47:12 -0700 Subject: Prefer `map(drop)` to `map(|_| ())` I previously advocated for the latter syntax on stylistic grounds. But it generates less efficient code, because it creates a new lambda function for each usage. The optimizer does not combine them. This change saves about 6KB of code. --- src/sys/termios.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/sys/termios.rs') diff --git a/src/sys/termios.rs b/src/sys/termios.rs index 11cacd7c..8621cf0c 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -929,7 +929,7 @@ cfg_if!{ let inner_termios = unsafe { termios.get_libc_termios_mut() }; let res = unsafe { libc::cfsetispeed(inner_termios, baud.into() as libc::speed_t) }; termios.update_wrapper(); - Errno::result(res).map(|_| ()) + Errno::result(res).map(drop) } /// Set output baud rate (see @@ -940,7 +940,7 @@ cfg_if!{ let inner_termios = unsafe { termios.get_libc_termios_mut() }; let res = unsafe { libc::cfsetospeed(inner_termios, baud.into() as libc::speed_t) }; termios.update_wrapper(); - Errno::result(res).map(|_| ()) + Errno::result(res).map(drop) } /// Set both the input and output baud rates (see @@ -952,7 +952,7 @@ cfg_if!{ let inner_termios = unsafe { termios.get_libc_termios_mut() }; let res = unsafe { libc::cfsetspeed(inner_termios, baud.into() as libc::speed_t) }; termios.update_wrapper(); - Errno::result(res).map(|_| ()) + Errno::result(res).map(drop) } } else { /// Get input baud rate (see @@ -981,7 +981,7 @@ cfg_if!{ let inner_termios = unsafe { termios.get_libc_termios_mut() }; let res = unsafe { libc::cfsetispeed(inner_termios, baud as libc::speed_t) }; termios.update_wrapper(); - Errno::result(res).map(|_| ()) + Errno::result(res).map(drop) } /// Set output baud rate (see @@ -992,7 +992,7 @@ cfg_if!{ let inner_termios = unsafe { termios.get_libc_termios_mut() }; let res = unsafe { libc::cfsetospeed(inner_termios, baud as libc::speed_t) }; termios.update_wrapper(); - Errno::result(res).map(|_| ()) + Errno::result(res).map(drop) } /// Set both the input and output baud rates (see @@ -1004,7 +1004,7 @@ cfg_if!{ let inner_termios = unsafe { termios.get_libc_termios_mut() }; let res = unsafe { libc::cfsetspeed(inner_termios, baud as libc::speed_t) }; termios.update_wrapper(); - Errno::result(res).map(|_| ()) + Errno::result(res).map(drop) } } } @@ -1060,13 +1060,13 @@ pub fn tcgetattr(fd: RawFd) -> Result { /// *any* of the parameters were successfully set, not only if all were set successfully. pub fn tcsetattr(fd: RawFd, actions: SetArg, termios: &Termios) -> Result<()> { let inner_termios = termios.get_libc_termios(); - Errno::result(unsafe { libc::tcsetattr(fd, actions as c_int, &*inner_termios) }).map(|_| ()) + Errno::result(unsafe { libc::tcsetattr(fd, actions as c_int, &*inner_termios) }).map(drop) } /// Block until all output data is written (see /// [tcdrain(3p)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/tcdrain.html)). pub fn tcdrain(fd: RawFd) -> Result<()> { - Errno::result(unsafe { libc::tcdrain(fd) }).map(|_| ()) + Errno::result(unsafe { libc::tcdrain(fd) }).map(drop) } /// Suspend or resume the transmission or reception of data (see @@ -1075,7 +1075,7 @@ pub fn tcdrain(fd: RawFd) -> Result<()> { /// `tcflow()` suspends of resumes the transmission or reception of data for the given port /// depending on the value of `action`. pub fn tcflow(fd: RawFd, action: FlowArg) -> Result<()> { - Errno::result(unsafe { libc::tcflow(fd, action as c_int) }).map(|_| ()) + Errno::result(unsafe { libc::tcflow(fd, action as c_int) }).map(drop) } /// Discard data in the output or input queue (see @@ -1084,7 +1084,7 @@ pub fn tcflow(fd: RawFd, action: FlowArg) -> Result<()> { /// `tcflush()` will discard data for a terminal port in the input queue, output queue, or both /// depending on the value of `action`. pub fn tcflush(fd: RawFd, action: FlushArg) -> Result<()> { - Errno::result(unsafe { libc::tcflush(fd, action as c_int) }).map(|_| ()) + Errno::result(unsafe { libc::tcflush(fd, action as c_int) }).map(drop) } /// Send a break for a specific duration (see @@ -1093,7 +1093,7 @@ pub fn tcflush(fd: RawFd, action: FlushArg) -> Result<()> { /// When using asynchronous data transmission `tcsendbreak()` will transmit a continuous stream /// of zero-valued bits for an implementation-defined duration. pub fn tcsendbreak(fd: RawFd, duration: c_int) -> Result<()> { - Errno::result(unsafe { libc::tcsendbreak(fd, duration) }).map(|_| ()) + Errno::result(unsafe { libc::tcsendbreak(fd, duration) }).map(drop) } /// Get the session controlled by the given terminal (see -- cgit v1.2.3