Reformat rust code using 1.40 rustfmt and fail build if rustfmt fails (#2339)

diff --git a/lib/rs/Makefile.am b/lib/rs/Makefile.am
index 6d74348..dd1c03b 100644
--- a/lib/rs/Makefile.am
+++ b/lib/rs/Makefile.am
@@ -31,9 +31,11 @@
 	@echo '##############################################################'
 
 check-local:
+	$(CARGO) fmt --all -- --check
 	$(CARGO) test
 
 all-local:
+	$(CARGO) fmt --all -- --check
 	$(CARGO) build
 
 clean-local:
diff --git a/lib/rs/src/errors.rs b/lib/rs/src/errors.rs
index 41a055e..03d98cf 100644
--- a/lib/rs/src/errors.rs
+++ b/lib/rs/src/errors.rs
@@ -15,13 +15,15 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use std::convert::TryFrom;
 use std::convert::{From, Into};
 use std::error::Error as StdError;
 use std::fmt::{Debug, Display, Formatter};
 use std::{error, fmt, io, string};
-use std::convert::TryFrom;
 
-use crate::protocol::{TFieldIdentifier, TInputProtocol, TOutputProtocol, TStructIdentifier, TType};
+use crate::protocol::{
+    TFieldIdentifier, TInputProtocol, TOutputProtocol, TStructIdentifier, TType,
+};
 
 // FIXME: should all my error structs impl error::Error as well?
 // FIXME: should all fields in TransportError, ProtocolError and ApplicationError be optional?
@@ -235,10 +237,7 @@
 
         i.read_struct_end()?;
 
-        Ok(ApplicationError {
-            kind,
-            message,
-        })
+        Ok(ApplicationError { kind, message })
     }
 
     /// Convert an `ApplicationError` into its wire representation and write
diff --git a/lib/rs/src/lib.rs b/lib/rs/src/lib.rs
index f71c3e8..279690e 100644
--- a/lib/rs/src/lib.rs
+++ b/lib/rs/src/lib.rs
@@ -80,4 +80,5 @@
 pub type Result<T> = std::result::Result<T, self::Error>;
 
 // Re-export ordered-float, since it is used by the generator
-pub use ordered_float::OrderedFloat as OrderedFloat;
\ No newline at end of file
+// FIXME: check the guidance around type reexports
+pub use ordered_float::OrderedFloat;
diff --git a/lib/rs/src/protocol/binary.rs b/lib/rs/src/protocol/binary.rs
index 22b496c..8509c34 100644
--- a/lib/rs/src/protocol/binary.rs
+++ b/lib/rs/src/protocol/binary.rs
@@ -68,10 +68,7 @@
     /// Set `strict` to `true` if all incoming messages contain the protocol
     /// version number in the protocol header.
     pub fn new(transport: T, strict: bool) -> TBinaryInputProtocol<T> {
-        TBinaryInputProtocol {
-            strict,
-            transport,
-        }
+        TBinaryInputProtocol { strict, transport }
     }
 }
 
@@ -294,10 +291,7 @@
     /// Set `strict` to `true` if all outgoing messages should contain the
     /// protocol version number in the protocol header.
     pub fn new(transport: T, strict: bool) -> TBinaryOutputProtocol<T> {
-        TBinaryOutputProtocol {
-            strict,
-            transport,
-        }
+        TBinaryOutputProtocol { strict, transport }
     }
 }
 
@@ -453,7 +447,10 @@
 }
 
 impl TOutputProtocolFactory for TBinaryOutputProtocolFactory {
-    fn create(&self, transport: Box<dyn TWriteTransport + Send>) -> Box<dyn TOutputProtocol + Send> {
+    fn create(
+        &self,
+        transport: Box<dyn TWriteTransport + Send>,
+    ) -> Box<dyn TOutputProtocol + Send> {
         Box::new(TBinaryOutputProtocol::new(transport, true))
     }
 }
diff --git a/lib/rs/src/protocol/compact.rs b/lib/rs/src/protocol/compact.rs
index f885e40..e08a30b 100644
--- a/lib/rs/src/protocol/compact.rs
+++ b/lib/rs/src/protocol/compact.rs
@@ -427,7 +427,8 @@
         self.write_byte(COMPACT_PROTOCOL_ID)?;
         self.write_byte((u8::from(identifier.message_type) << 5) | COMPACT_VERSION)?;
         // cast i32 as u32 so that varint writing won't use zigzag encoding
-        self.transport.write_varint(identifier.sequence_number as u32)?;
+        self.transport
+            .write_varint(identifier.sequence_number as u32)?;
         self.write_string(&identifier.name)?;
         Ok(())
     }
@@ -1091,6 +1092,7 @@
         let (mut i_prot, _) = test_objects();
 
         // signed two's complement of -1073741823 = 1100_0000_0000_0000_0000_0000_0000_0001
+        #[rustfmt::skip]
         let source_bytes: [u8; 11] = [
             0x82, /* protocol ID */
             0x21, /* message type | protocol version */
@@ -1102,7 +1104,7 @@
             0x03, /* message-name length */
             0x66,
             0x6F,
-            0x6F /* "foo" */,
+            0x6F, /* "foo" */
         ];
 
         i_prot.transport.set_readable_bytes(&source_bytes);
diff --git a/lib/rs/src/protocol/mod.rs b/lib/rs/src/protocol/mod.rs
index f9c1f72..e11a104 100644
--- a/lib/rs/src/protocol/mod.rs
+++ b/lib/rs/src/protocol/mod.rs
@@ -577,14 +577,18 @@
 /// ```
 pub trait TOutputProtocolFactory {
     /// Create a `TOutputProtocol` that writes bytes to `transport`.
-    fn create(&self, transport: Box<dyn TWriteTransport + Send>) -> Box<dyn TOutputProtocol + Send>;
+    fn create(&self, transport: Box<dyn TWriteTransport + Send>)
+        -> Box<dyn TOutputProtocol + Send>;
 }
 
 impl<T> TOutputProtocolFactory for Box<T>
 where
     T: TOutputProtocolFactory + ?Sized,
 {
-    fn create(&self, transport: Box<dyn TWriteTransport + Send>) -> Box<dyn TOutputProtocol + Send> {
+    fn create(
+        &self,
+        transport: Box<dyn TWriteTransport + Send>,
+    ) -> Box<dyn TOutputProtocol + Send> {
         (**self).create(transport)
     }
 }
@@ -679,10 +683,7 @@
     /// Create a `TListIdentifier` for a list with `size` elements of type
     /// `element_type`.
     pub fn new(element_type: TType, size: i32) -> TListIdentifier {
-        TListIdentifier {
-            element_type,
-            size,
-        }
+        TListIdentifier { element_type, size }
     }
 }
 
@@ -699,10 +700,7 @@
     /// Create a `TSetIdentifier` for a set with `size` elements of type
     /// `element_type`.
     pub fn new(element_type: TType, size: i32) -> TSetIdentifier {
-        TSetIdentifier {
-            element_type,
-            size,
-        }
+        TSetIdentifier { element_type, size }
     }
 }
 
@@ -878,7 +876,10 @@
 /// `actual`.
 ///
 /// Return `()` if `actual == expected`, `Err` otherwise.
-pub fn verify_expected_message_type(expected: TMessageType, actual: TMessageType) -> crate::Result<()> {
+pub fn verify_expected_message_type(
+    expected: TMessageType,
+    actual: TMessageType,
+) -> crate::Result<()> {
     if expected == actual {
         Ok(())
     } else {
diff --git a/lib/rs/src/protocol/multiplexed.rs b/lib/rs/src/protocol/multiplexed.rs
index 83498fb..697b7e6 100644
--- a/lib/rs/src/protocol/multiplexed.rs
+++ b/lib/rs/src/protocol/multiplexed.rs
@@ -191,7 +191,9 @@
 #[cfg(test)]
 mod tests {
 
-    use crate::protocol::{TBinaryOutputProtocol, TMessageIdentifier, TMessageType, TOutputProtocol};
+    use crate::protocol::{
+        TBinaryOutputProtocol, TMessageIdentifier, TMessageType, TOutputProtocol,
+    };
     use crate::transport::{TBufferChannel, TIoChannel, WriteHalf};
 
     use super::*;
diff --git a/lib/rs/src/server/mod.rs b/lib/rs/src/server/mod.rs
index 050feee..64c6da2 100644
--- a/lib/rs/src/server/mod.rs
+++ b/lib/rs/src/server/mod.rs
@@ -91,7 +91,8 @@
     /// the response to `o`.
     ///
     /// Returns `()` if the handler was executed; `Err` otherwise.
-    fn process(&self, i: &mut dyn TInputProtocol, o: &mut dyn TOutputProtocol) -> crate::Result<()>;
+    fn process(&self, i: &mut dyn TInputProtocol, o: &mut dyn TOutputProtocol)
+        -> crate::Result<()>;
 }
 
 /// Convenience function used in generated `TProcessor` implementations to
diff --git a/lib/rs/src/server/multiplexed.rs b/lib/rs/src/server/multiplexed.rs
index 4f41f24..8331d91 100644
--- a/lib/rs/src/server/multiplexed.rs
+++ b/lib/rs/src/server/multiplexed.rs
@@ -136,7 +136,11 @@
 }
 
 impl TProcessor for TMultiplexedProcessor {
-    fn process(&self, i_prot: &mut dyn TInputProtocol, o_prot: &mut dyn TOutputProtocol) -> crate::Result<()> {
+    fn process(
+        &self,
+        i_prot: &mut dyn TInputProtocol,
+        o_prot: &mut dyn TOutputProtocol,
+    ) -> crate::Result<()> {
         let msg_ident = i_prot.read_message_begin()?;
 
         debug!("process incoming msg id:{:?}", &msg_ident);
@@ -183,7 +187,9 @@
     use std::sync::atomic::{AtomicBool, Ordering};
     use std::sync::Arc;
 
-    use crate::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TMessageIdentifier, TMessageType};
+    use crate::protocol::{
+        TBinaryInputProtocol, TBinaryOutputProtocol, TMessageIdentifier, TMessageType,
+    };
     use crate::transport::{ReadHalf, TBufferChannel, TIoChannel, WriteHalf};
     use crate::{ApplicationError, ApplicationErrorKind};
 
@@ -261,7 +267,11 @@
     }
 
     impl TProcessor for Service {
-        fn process(&self, _: &mut dyn TInputProtocol, _: &mut dyn TOutputProtocol) -> crate::Result<()> {
+        fn process(
+            &self,
+            _: &mut dyn TInputProtocol,
+            _: &mut dyn TOutputProtocol,
+        ) -> crate::Result<()> {
             let res = self
                 .invoked
                 .compare_and_swap(false, true, Ordering::Relaxed);
diff --git a/lib/rs/src/server/threaded.rs b/lib/rs/src/server/threaded.rs
index 64bf8bb..897235c 100644
--- a/lib/rs/src/server/threaded.rs
+++ b/lib/rs/src/server/threaded.rs
@@ -21,7 +21,9 @@
 use std::sync::Arc;
 use threadpool::ThreadPool;
 
-use crate::protocol::{TInputProtocol, TInputProtocolFactory, TOutputProtocol, TOutputProtocolFactory};
+use crate::protocol::{
+    TInputProtocol, TInputProtocolFactory, TOutputProtocol, TOutputProtocolFactory,
+};
 use crate::transport::{TIoChannel, TReadTransportFactory, TTcpChannel, TWriteTransportFactory};
 use crate::{ApplicationError, ApplicationErrorKind};
 
@@ -196,7 +198,10 @@
     fn new_protocols_for_connection(
         &mut self,
         stream: TcpStream,
-    ) -> crate::Result<(Box<dyn TInputProtocol + Send>, Box<dyn TOutputProtocol + Send>)> {
+    ) -> crate::Result<(
+        Box<dyn TInputProtocol + Send>,
+        Box<dyn TOutputProtocol + Send>,
+    )> {
         // create the shared tcp stream
         let channel = TTcpChannel::with_stream(stream);
 
@@ -227,10 +232,11 @@
     let mut o_prot = o_prot;
     loop {
         match processor.process(&mut *i_prot, &mut *o_prot) {
-            Ok(()) => {},
+            Ok(()) => {}
             Err(err) => {
                 match err {
-                    crate::Error::Transport(ref transport_err) if transport_err.kind == TransportErrorKind::EndOfFile => {},
+                    crate::Error::Transport(ref transport_err)
+                        if transport_err.kind == TransportErrorKind::EndOfFile => {}
                     other => warn!("processor completed with error: {:?}", other),
                 }
                 break;
diff --git a/lib/rs/src/transport/buffered.rs b/lib/rs/src/transport/buffered.rs
index ebdcdc2..a54f823 100644
--- a/lib/rs/src/transport/buffered.rs
+++ b/lib/rs/src/transport/buffered.rs
@@ -365,7 +365,7 @@
         assert_eq!(&buf, &[0, 1, 2, 3, 4, 5, 6, 7]);
 
         // let's clear out the buffer and try read again
-        for b in &mut buf{
+        for b in &mut buf {
             *b = 0;
         }
         let read_result = t.read(&mut buf);
diff --git a/lib/rs/src/transport/mod.rs b/lib/rs/src/transport/mod.rs
index d02a87c..2b5733f 100644
--- a/lib/rs/src/transport/mod.rs
+++ b/lib/rs/src/transport/mod.rs
@@ -111,7 +111,12 @@
     /// Returned halves may share the underlying OS channel or buffer resources.
     /// Implementations **should ensure** that these two halves can be safely
     /// used independently by concurrent threads.
-    fn split(self) -> crate::Result<(crate::transport::ReadHalf<Self>, crate::transport::WriteHalf<Self>)>
+    fn split(
+        self,
+    ) -> crate::Result<(
+        crate::transport::ReadHalf<Self>,
+        crate::transport::WriteHalf<Self>,
+    )>
     where
         Self: Sized;
 }
diff --git a/lib/rs/test/Makefile.am b/lib/rs/test/Makefile.am
index 5dc4f56..19056a6 100644
--- a/lib/rs/test/Makefile.am
+++ b/lib/rs/test/Makefile.am
@@ -28,6 +28,7 @@
 	$(THRIFT) -out src --gen rs $(top_builddir)/test/Identifiers.thrift #THRIFT-4953
 
 check: stubs
+	$(CARGO) fmt --all -- --check
 	$(CARGO) build
 	$(CARGO) test
 	[ -d bin ] || mkdir bin