THRIFT-4176: Implement threaded server for Rust
Client: rs
* Create a TIoChannel construct
* Separate TTransport into TReadTransport and TWriteTransport
* Restructure types to avoid shared ownership
* Remove user-visible boxing and ref-counting
* Replace TSimpleServer with a thread-pool based TServer
This closes #1255
diff --git a/lib/rs/src/errors.rs b/lib/rs/src/errors.rs
index a6049d5..e36cb3b 100644
--- a/lib/rs/src/errors.rs
+++ b/lib/rs/src/errors.rs
@@ -21,7 +21,7 @@
use std::{error, fmt, io, string};
use try_from::TryFrom;
-use ::protocol::{TFieldIdentifier, TInputProtocol, TOutputProtocol, TStructIdentifier, TType};
+use 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?
@@ -198,8 +198,8 @@
/// Create an `ApplicationError` from its wire representation.
///
/// Application code **should never** call this method directly.
- pub fn read_application_error_from_in_protocol(i: &mut TInputProtocol)
- -> ::Result<ApplicationError> {
+ pub fn read_application_error_from_in_protocol(i: &mut TInputProtocol,)
+ -> ::Result<ApplicationError> {
let mut message = "general remote error".to_owned();
let mut kind = ApplicationErrorKind::Unknown;
@@ -212,7 +212,9 @@
break;
}
- let id = field_ident.id.expect("sender should always specify id for non-STOP field");
+ let id = field_ident
+ .id
+ .expect("sender should always specify id for non-STOP field");
match id {
1 => {
@@ -222,8 +224,9 @@
}
2 => {
let remote_type_as_int = i.read_i32()?;
- let remote_kind: ApplicationErrorKind = TryFrom::try_from(remote_type_as_int)
- .unwrap_or(ApplicationErrorKind::Unknown);
+ let remote_kind: ApplicationErrorKind =
+ TryFrom::try_from(remote_type_as_int)
+ .unwrap_or(ApplicationErrorKind::Unknown);
i.read_field_end()?;
kind = remote_kind;
}
@@ -235,20 +238,23 @@
i.read_struct_end()?;
- Ok(ApplicationError {
- kind: kind,
- message: message,
- })
+ Ok(
+ ApplicationError {
+ kind: kind,
+ message: message,
+ },
+ )
}
/// Convert an `ApplicationError` into its wire representation and write
/// it to the remote.
///
/// Application code **should never** call this method directly.
- pub fn write_application_error_to_out_protocol(e: &ApplicationError,
- o: &mut TOutputProtocol)
- -> ::Result<()> {
- o.write_struct_begin(&TStructIdentifier { name: "TApplicationException".to_owned() })?;
+ pub fn write_application_error_to_out_protocol(
+ e: &ApplicationError,
+ o: &mut TOutputProtocol,
+ ) -> ::Result<()> {
+ o.write_struct_begin(&TStructIdentifier { name: "TApplicationException".to_owned() },)?;
let message_field = TFieldIdentifier::new("message", TType::String, 1);
let type_field = TFieldIdentifier::new("type", TType::I32, 2);
@@ -303,19 +309,23 @@
impl From<String> for Error {
fn from(s: String) -> Self {
- Error::Application(ApplicationError {
- kind: ApplicationErrorKind::Unknown,
- message: s,
- })
+ Error::Application(
+ ApplicationError {
+ kind: ApplicationErrorKind::Unknown,
+ message: s,
+ },
+ )
}
}
impl<'a> From<&'a str> for Error {
fn from(s: &'a str) -> Self {
- Error::Application(ApplicationError {
- kind: ApplicationErrorKind::Unknown,
- message: String::from(s),
- })
+ Error::Application(
+ ApplicationError {
+ kind: ApplicationErrorKind::Unknown,
+ message: String::from(s),
+ },
+ )
}
}
@@ -418,10 +428,14 @@
5 => Ok(TransportErrorKind::NegativeSize),
6 => Ok(TransportErrorKind::SizeLimit),
_ => {
- Err(Error::Protocol(ProtocolError {
- kind: ProtocolErrorKind::Unknown,
- message: format!("cannot convert {} to TransportErrorKind", from),
- }))
+ Err(
+ Error::Protocol(
+ ProtocolError {
+ kind: ProtocolErrorKind::Unknown,
+ message: format!("cannot convert {} to TransportErrorKind", from),
+ },
+ ),
+ )
}
}
}
@@ -433,34 +447,44 @@
io::ErrorKind::ConnectionReset |
io::ErrorKind::ConnectionRefused |
io::ErrorKind::NotConnected => {
- Error::Transport(TransportError {
- kind: TransportErrorKind::NotOpen,
- message: err.description().to_owned(),
- })
+ Error::Transport(
+ TransportError {
+ kind: TransportErrorKind::NotOpen,
+ message: err.description().to_owned(),
+ },
+ )
}
io::ErrorKind::AlreadyExists => {
- Error::Transport(TransportError {
- kind: TransportErrorKind::AlreadyOpen,
- message: err.description().to_owned(),
- })
+ Error::Transport(
+ TransportError {
+ kind: TransportErrorKind::AlreadyOpen,
+ message: err.description().to_owned(),
+ },
+ )
}
io::ErrorKind::TimedOut => {
- Error::Transport(TransportError {
- kind: TransportErrorKind::TimedOut,
- message: err.description().to_owned(),
- })
+ Error::Transport(
+ TransportError {
+ kind: TransportErrorKind::TimedOut,
+ message: err.description().to_owned(),
+ },
+ )
}
io::ErrorKind::UnexpectedEof => {
- Error::Transport(TransportError {
- kind: TransportErrorKind::EndOfFile,
- message: err.description().to_owned(),
- })
+ Error::Transport(
+ TransportError {
+ kind: TransportErrorKind::EndOfFile,
+ message: err.description().to_owned(),
+ },
+ )
}
_ => {
- Error::Transport(TransportError {
- kind: TransportErrorKind::Unknown,
- message: err.description().to_owned(), // FIXME: use io error's debug string
- })
+ Error::Transport(
+ TransportError {
+ kind: TransportErrorKind::Unknown,
+ message: err.description().to_owned(), // FIXME: use io error's debug string
+ },
+ )
}
}
}
@@ -468,10 +492,12 @@
impl From<string::FromUtf8Error> for Error {
fn from(err: string::FromUtf8Error) -> Self {
- Error::Protocol(ProtocolError {
- kind: ProtocolErrorKind::InvalidData,
- message: err.description().to_owned(), // FIXME: use fmt::Error's debug string
- })
+ Error::Protocol(
+ ProtocolError {
+ kind: ProtocolErrorKind::InvalidData,
+ message: err.description().to_owned(), // FIXME: use fmt::Error's debug string
+ },
+ )
}
}
@@ -558,10 +584,14 @@
5 => Ok(ProtocolErrorKind::NotImplemented),
6 => Ok(ProtocolErrorKind::DepthLimit),
_ => {
- Err(Error::Protocol(ProtocolError {
- kind: ProtocolErrorKind::Unknown,
- message: format!("cannot convert {} to ProtocolErrorKind", from),
- }))
+ Err(
+ Error::Protocol(
+ ProtocolError {
+ kind: ProtocolErrorKind::Unknown,
+ message: format!("cannot convert {} to ProtocolErrorKind", from),
+ },
+ ),
+ )
}
}
}
@@ -668,10 +698,14 @@
9 => Ok(ApplicationErrorKind::InvalidProtocol),
10 => Ok(ApplicationErrorKind::UnsupportedClientType),
_ => {
- Err(Error::Application(ApplicationError {
- kind: ApplicationErrorKind::Unknown,
- message: format!("cannot convert {} to ApplicationErrorKind", from),
- }))
+ Err(
+ Error::Application(
+ ApplicationError {
+ kind: ApplicationErrorKind::Unknown,
+ message: format!("cannot convert {} to ApplicationErrorKind", from),
+ },
+ ),
+ )
}
}
}