diff options
author | Allen George <allen.george@gmail.com> | 2017-01-30 07:15:00 -0500 |
---|---|---|
committer | James E. King, III <jking@apache.org> | 2017-04-27 08:46:02 -0400 |
commit | 0e22c362b967bd3765ee3da349faa789904a0707 (patch) | |
tree | cf7271e15659c1181abb6ed8c57b599d79d026f3 /test/rs | |
parent | 9db23b7be330f47037b4e3e5e374eda5e38b0dfd (diff) | |
download | thrift-0e22c362b967bd3765ee3da349faa789904a0707.tar.gz |
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
Diffstat (limited to 'test/rs')
-rw-r--r-- | test/rs/src/bin/test_client.rs | 216 | ||||
-rw-r--r-- | test/rs/src/bin/test_server.rs | 225 |
2 files changed, 255 insertions, 186 deletions
diff --git a/test/rs/src/bin/test_client.rs b/test/rs/src/bin/test_client.rs index a2ea83204..aad78a058 100644 --- a/test/rs/src/bin/test_client.rs +++ b/test/rs/src/bin/test_client.rs @@ -22,14 +22,14 @@ extern crate thrift; extern crate thrift_test; // huh. I have to do this to use my lib use ordered_float::OrderedFloat; -use std::cell::RefCell; use std::collections::{BTreeMap, BTreeSet}; use std::fmt::Debug; -use std::rc::Rc; use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TCompactInputProtocol, TCompactOutputProtocol, TInputProtocol, TOutputProtocol}; -use thrift::transport::{TBufferedTransport, TFramedTransport, TTcpTransport, TTransport}; +use thrift::transport::{ReadHalf, TBufferedReadTransport, TBufferedWriteTransport, + TFramedReadTransport, TFramedWriteTransport, TIoChannel, TReadTransport, + TTcpChannel, TWriteTransport, WriteHalf}; use thrift_test::*; fn main() { @@ -58,7 +58,8 @@ fn run() -> thrift::Result<()> { (@arg transport: --transport +takes_value "Thrift transport implementation to use (\"buffered\", \"framed\")") (@arg protocol: --protocol +takes_value "Thrift protocol implementation to use (\"binary\", \"compact\")") (@arg testloops: -n --testloops +takes_value "Number of times to run tests") - ).get_matches(); + ) + .get_matches(); let host = matches.value_of("host").unwrap_or("127.0.0.1"); let port = value_t!(matches, "port", u16).unwrap_or(9090); @@ -66,32 +67,39 @@ fn run() -> thrift::Result<()> { let transport = matches.value_of("transport").unwrap_or("buffered"); let protocol = matches.value_of("protocol").unwrap_or("binary"); - let t = open_tcp_transport(host, port)?; + let (i_chan, o_chan) = tcp_channel(host, port)?; - let t: Box<TTransport> = match transport { - "buffered" => Box::new(TBufferedTransport::new(t)), - "framed" => Box::new(TFramedTransport::new(t)), + let (i_tran, o_tran) = match transport { + "buffered" => { + (Box::new(TBufferedReadTransport::new(i_chan)) as Box<TReadTransport>, + Box::new(TBufferedWriteTransport::new(o_chan)) as Box<TWriteTransport>) + } + "framed" => { + (Box::new(TFramedReadTransport::new(i_chan)) as Box<TReadTransport>, + Box::new(TFramedWriteTransport::new(o_chan)) as Box<TWriteTransport>) + } unmatched => return Err(format!("unsupported transport {}", unmatched).into()), }; - let t = Rc::new(RefCell::new(t)); let (i_prot, o_prot): (Box<TInputProtocol>, Box<TOutputProtocol>) = match protocol { "binary" => { - (Box::new(TBinaryInputProtocol::new(t.clone(), true)), - Box::new(TBinaryOutputProtocol::new(t.clone(), true))) + (Box::new(TBinaryInputProtocol::new(i_tran, true)), + Box::new(TBinaryOutputProtocol::new(o_tran, true))) } "compact" => { - (Box::new(TCompactInputProtocol::new(t.clone())), - Box::new(TCompactOutputProtocol::new(t.clone()))) + (Box::new(TCompactInputProtocol::new(i_tran)), + Box::new(TCompactOutputProtocol::new(o_tran))) } unmatched => return Err(format!("unsupported protocol {}", unmatched).into()), }; - println!("connecting to {}:{} with {}+{} stack", - host, - port, - protocol, - transport); + println!( + "connecting to {}:{} with {}+{} stack", + host, + port, + protocol, + transport + ); let mut client = ThriftTestSyncClient::new(i_prot, o_prot); @@ -102,16 +110,19 @@ fn run() -> thrift::Result<()> { Ok(()) } -// FIXME: expose "open" through the client interface so I don't have to early open the transport -fn open_tcp_transport(host: &str, port: u16) -> thrift::Result<Rc<RefCell<Box<TTransport>>>> { - let mut t = TTcpTransport::new(); - match t.open(&format!("{}:{}", host, port)) { - Ok(()) => Ok(Rc::new(RefCell::new(Box::new(t) as Box<TTransport>))), - Err(e) => Err(e), - } +// FIXME: expose "open" through the client interface so I don't have to early +// open +fn tcp_channel( + host: &str, + port: u16, +) -> thrift::Result<(ReadHalf<TTcpChannel>, WriteHalf<TTcpChannel>)> { + let mut c = TTcpChannel::new(); + c.open(&format!("{}:{}", host, port))?; + c.split() } -fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Error> { +fn make_thrift_calls(client: &mut ThriftTestSyncClient<Box<TInputProtocol>, Box<TOutputProtocol>>,) + -> Result<(), thrift::Error> { println!("testVoid"); client.test_void()?; @@ -131,12 +142,15 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er verify_expected_result(client.test_i32(1159348374), 1159348374)?; println!("testi64"); - // try!(verify_expected_result(client.test_i64(-8651829879438294565), -8651829879438294565)); + // try!(verify_expected_result(client.test_i64(-8651829879438294565), + // -8651829879438294565)); verify_expected_result(client.test_i64(i64::min_value()), i64::min_value())?; println!("testDouble"); - verify_expected_result(client.test_double(OrderedFloat::from(42.42)), - OrderedFloat::from(42.42))?; + verify_expected_result( + client.test_double(OrderedFloat::from(42.42)), + OrderedFloat::from(42.42), + )?; println!("testTypedef"); { @@ -175,10 +189,14 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er } // Xtruct again, with optional values - // FIXME: apparently the erlang thrift server does not like opt-in-req-out parameters that are undefined. Joy. + // FIXME: apparently the erlang thrift server does not like opt-in-req-out + // parameters that are undefined. Joy. // { - // let x_snd = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: None, i32_thing: None, i64_thing: Some(12938492818) }; - // let x_cmp = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: Some(0), i32_thing: Some(0), i64_thing: Some(12938492818) }; // the C++ server is responding correctly + // let x_snd = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: None, + // i32_thing: None, i64_thing: Some(12938492818) }; + // let x_cmp = Xtruct { string_thing: Some("foo".to_owned()), byte_thing: + // Some(0), i32_thing: Some(0), i64_thing: Some(12938492818) }; // the C++ + // server is responding correctly // try!(verify_expected_result(client.test_struct(x_snd), x_cmp)); // } // @@ -188,22 +206,26 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er { let x_snd = Xtruct2 { byte_thing: Some(32), - struct_thing: Some(Xtruct { - string_thing: Some("foo".to_owned()), - byte_thing: Some(1), - i32_thing: Some(324382098), - i64_thing: Some(12938492818), - }), + struct_thing: Some( + Xtruct { + string_thing: Some("foo".to_owned()), + byte_thing: Some(1), + i32_thing: Some(324382098), + i64_thing: Some(12938492818), + }, + ), i32_thing: Some(293481098), }; let x_cmp = Xtruct2 { byte_thing: Some(32), - struct_thing: Some(Xtruct { - string_thing: Some("foo".to_owned()), - byte_thing: Some(1), - i32_thing: Some(324382098), - i64_thing: Some(12938492818), - }), + struct_thing: Some( + Xtruct { + string_thing: Some("foo".to_owned()), + byte_thing: Some(1), + i32_thing: Some(324382098), + i64_thing: Some(12938492818), + }, + ), i32_thing: Some(293481098), }; verify_expected_result(client.test_nest(x_snd), x_cmp)?; @@ -270,7 +292,8 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er } // nested map - // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, } + // expect : {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 + // => 2, 3 => 3, 4 => 4, }, } println!("testMapMap"); { let mut m_cmp_nested_0: BTreeMap<i32, i32> = BTreeMap::new(); @@ -302,13 +325,10 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er i64_thing: Some(-19234123981), }; - verify_expected_result(client.test_multi(1, - -123948, - -19234123981, - m_snd, - Numberz::EIGHT, - 81), - s_cmp)?; + verify_expected_result( + client.test_multi(1, -123948, -19234123981, m_snd, Numberz::EIGHT, 81), + s_cmp, + )?; } // Insanity @@ -324,24 +344,30 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er arg_map_usermap.insert(Numberz::EIGHT, 19); let mut arg_vec_xtructs: Vec<Xtruct> = Vec::new(); - arg_vec_xtructs.push(Xtruct { - string_thing: Some("foo".to_owned()), - byte_thing: Some(8), - i32_thing: Some(29), - i64_thing: Some(92384), - }); - arg_vec_xtructs.push(Xtruct { - string_thing: Some("bar".to_owned()), - byte_thing: Some(28), - i32_thing: Some(2), - i64_thing: Some(-1281), - }); - arg_vec_xtructs.push(Xtruct { - string_thing: Some("baz".to_owned()), - byte_thing: Some(0), - i32_thing: Some(3948539), - i64_thing: Some(-12938492), - }); + arg_vec_xtructs.push( + Xtruct { + string_thing: Some("foo".to_owned()), + byte_thing: Some(8), + i32_thing: Some(29), + i64_thing: Some(92384), + }, + ); + arg_vec_xtructs.push( + Xtruct { + string_thing: Some("bar".to_owned()), + byte_thing: Some(28), + i32_thing: Some(2), + i64_thing: Some(-1281), + }, + ); + arg_vec_xtructs.push( + Xtruct { + string_thing: Some("baz".to_owned()), + byte_thing: Some(0), + i32_thing: Some(3948539), + i64_thing: Some(-12938492), + }, + ); let mut s_cmp_nested_1: BTreeMap<Numberz, Insanity> = BTreeMap::new(); let insanity = Insanity { @@ -372,7 +398,7 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er Err(thrift::Error::User(ref e)) => { match e.downcast_ref::<Xception>() { Some(x) => Ok(x), - None => Err(thrift::Error::User("did not get expected Xception struct".into())), + None => Err(thrift::Error::User("did not get expected Xception struct".into()),), } } _ => Err(thrift::Error::User("did not get exception".into())), @@ -414,7 +440,7 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er Err(thrift::Error::User(ref e)) => { match e.downcast_ref::<Xception>() { Some(x) => Ok(x), - None => Err(thrift::Error::User("did not get expected Xception struct".into())), + None => Err(thrift::Error::User("did not get expected Xception struct".into()),), } } _ => Err(thrift::Error::User("did not get exception".into())), @@ -435,7 +461,7 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er Err(thrift::Error::User(ref e)) => { match e.downcast_ref::<Xception2>() { Some(x) => Ok(x), - None => Err(thrift::Error::User("did not get expected Xception struct".into())), + None => Err(thrift::Error::User("did not get expected Xception struct".into()),), } } _ => Err(thrift::Error::User("did not get exception".into())), @@ -443,12 +469,17 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er let x_cmp = Xception2 { error_code: Some(2002), - struct_thing: Some(Xtruct { - string_thing: Some("This is an Xception2".to_owned()), - byte_thing: Some(0), /* since this is an OPT_IN_REQ_OUT field the sender sets a default */ - i32_thing: Some(0), /* since this is an OPT_IN_REQ_OUT field the sender sets a default */ - i64_thing: Some(0), /* since this is an OPT_IN_REQ_OUT field the sender sets a default */ - }), + struct_thing: Some( + Xtruct { + string_thing: Some("This is an Xception2".to_owned()), + // since this is an OPT_IN_REQ_OUT field the sender sets a default + byte_thing: Some(0), + // since this is an OPT_IN_REQ_OUT field the sender sets a default + i32_thing: Some(0), + // since this is an OPT_IN_REQ_OUT field the sender sets a default + i64_thing: Some(0), + }, + ), }; verify_expected_result(Ok(x), &x_cmp)?; @@ -458,17 +489,18 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er { let r = client.test_multi_exception("haha".to_owned(), "RETURNED".to_owned()); let x = match r { - Err(e) => { - Err(thrift::Error::User(format!("received an unexpected exception {:?}", e).into())) - } + Err(e) => Err(thrift::Error::User(format!("received an unexpected exception {:?}", e).into(),),), _ => r, }?; let x_cmp = Xtruct { string_thing: Some("RETURNED".to_owned()), - byte_thing: Some(0), // since this is an OPT_IN_REQ_OUT field the sender sets a default - i32_thing: Some(0), // since this is an OPT_IN_REQ_OUT field the sender sets a default - i64_thing: Some(0), // since this is an OPT_IN_REQ_OUT field the sender sets a default + // since this is an OPT_IN_REQ_OUT field the sender sets a default + byte_thing: Some(0), + // since this is an OPT_IN_REQ_OUT field the sender sets a default + i32_thing: Some(0), + // since this is an OPT_IN_REQ_OUT field the sender sets a default + i64_thing: Some(0), }; verify_expected_result(Ok(x), x_cmp)?; @@ -479,20 +511,22 @@ fn make_thrift_calls(client: &mut ThriftTestSyncClient) -> Result<(), thrift::Er client.test_oneway(1)?; } - // final test to verify that the connection is still writable after the one-way call + // final test to verify that the connection is still writable after the one-way + // call client.test_void() } -fn verify_expected_result<T: Debug + PartialEq + Sized>(actual: Result<T, thrift::Error>, - expected: T) - -> Result<(), thrift::Error> { +#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] +fn verify_expected_result<T: Debug + PartialEq + Sized>( + actual: Result<T, thrift::Error>, + expected: T, +) -> Result<(), thrift::Error> { match actual { Ok(v) => { if v == expected { Ok(()) } else { - Err(thrift::Error::User(format!("expected {:?} but got {:?}", &expected, &v) - .into())) + Err(thrift::Error::User(format!("expected {:?} but got {:?}", &expected, &v).into()),) } } Err(e) => Err(e), diff --git a/test/rs/src/bin/test_server.rs b/test/rs/src/bin/test_server.rs index 613cd5559..9c738ab01 100644 --- a/test/rs/src/bin/test_server.rs +++ b/test/rs/src/bin/test_server.rs @@ -29,8 +29,10 @@ use std::time::Duration; use thrift::protocol::{TBinaryInputProtocolFactory, TBinaryOutputProtocolFactory, TCompactInputProtocolFactory, TCompactOutputProtocolFactory, TInputProtocolFactory, TOutputProtocolFactory}; -use thrift::server::TSimpleServer; -use thrift::transport::{TBufferedTransportFactory, TFramedTransportFactory, TTransportFactory}; +use thrift::server::TServer; +use thrift::transport::{TBufferedReadTransportFactory, TBufferedWriteTransportFactory, + TFramedReadTransportFactory, TFramedWriteTransportFactory, + TReadTransportFactory, TWriteTransportFactory}; use thrift_test::*; fn main() { @@ -49,7 +51,6 @@ fn run() -> thrift::Result<()> { // --domain-socket // --named-pipe // --ssl - // --workers let matches = clap_app!(rust_test_client => (version: "1.0") (author: "Apache Thrift Developers <dev@thrift.apache.org>") @@ -57,29 +58,35 @@ fn run() -> thrift::Result<()> { (@arg port: --port +takes_value "port on which the test server listens") (@arg transport: --transport +takes_value "transport implementation to use (\"buffered\", \"framed\")") (@arg protocol: --protocol +takes_value "protocol implementation to use (\"binary\", \"compact\")") - (@arg server_type: --server_type +takes_value "type of server instantiated (\"simple\", \"thread-pool\", \"threaded\", \"non-blocking\")") - ).get_matches(); + (@arg server_type: --server_type +takes_value "type of server instantiated (\"simple\", \"thread-pool\")") + (@arg workers: -n --workers +takes_value "number of thread-pool workers (\"4\")") + ) + .get_matches(); let port = value_t!(matches, "port", u16).unwrap_or(9090); let transport = matches.value_of("transport").unwrap_or("buffered"); let protocol = matches.value_of("protocol").unwrap_or("binary"); - let server_type = matches.value_of("server_type").unwrap_or("simple"); + let server_type = matches.value_of("server_type").unwrap_or("thread-pool"); + let workers = value_t!(matches, "workers", usize).unwrap_or(4); let listen_address = format!("127.0.0.1:{}", port); println!("binding to {}", listen_address); - let (i_transport_factory, o_transport_factory): (Box<TTransportFactory>, - Box<TTransportFactory>) = match &*transport { - "buffered" => { - (Box::new(TBufferedTransportFactory::new()), Box::new(TBufferedTransportFactory::new())) - } - "framed" => { - (Box::new(TFramedTransportFactory::new()), Box::new(TFramedTransportFactory::new())) - } - unknown => { - return Err(format!("unsupported transport type {}", unknown).into()); - } - }; + let (i_transport_factory, o_transport_factory): (Box<TReadTransportFactory>, + Box<TWriteTransportFactory>) = + match &*transport { + "buffered" => { + (Box::new(TBufferedReadTransportFactory::new()), + Box::new(TBufferedWriteTransportFactory::new())) + } + "framed" => { + (Box::new(TFramedReadTransportFactory::new()), + Box::new(TFramedWriteTransportFactory::new())) + } + unknown => { + return Err(format!("unsupported transport type {}", unknown).into()); + } + }; let (i_protocol_factory, o_protocol_factory): (Box<TInputProtocolFactory>, Box<TOutputProtocolFactory>) = @@ -101,11 +108,24 @@ fn run() -> thrift::Result<()> { let mut server = match &*server_type { "simple" => { - TSimpleServer::new(i_transport_factory, - i_protocol_factory, - o_transport_factory, - o_protocol_factory, - processor) + TServer::new( + i_transport_factory, + i_protocol_factory, + o_transport_factory, + o_protocol_factory, + processor, + 1, + ) + } + "thread-pool" => { + TServer::new( + i_transport_factory, + i_protocol_factory, + o_transport_factory, + o_protocol_factory, + processor, + workers, + ) } unknown => { return Err(format!("unsupported server type {}", unknown).into()); @@ -117,95 +137,93 @@ fn run() -> thrift::Result<()> { struct ThriftTestSyncHandlerImpl; impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl { - fn handle_test_void(&mut self) -> thrift::Result<()> { + fn handle_test_void(&self) -> thrift::Result<()> { println!("testVoid()"); Ok(()) } - fn handle_test_string(&mut self, thing: String) -> thrift::Result<String> { + fn handle_test_string(&self, thing: String) -> thrift::Result<String> { println!("testString({})", &thing); Ok(thing) } - fn handle_test_bool(&mut self, thing: bool) -> thrift::Result<bool> { + fn handle_test_bool(&self, thing: bool) -> thrift::Result<bool> { println!("testBool({})", thing); Ok(thing) } - fn handle_test_byte(&mut self, thing: i8) -> thrift::Result<i8> { + fn handle_test_byte(&self, thing: i8) -> thrift::Result<i8> { println!("testByte({})", thing); Ok(thing) } - fn handle_test_i32(&mut self, thing: i32) -> thrift::Result<i32> { + fn handle_test_i32(&self, thing: i32) -> thrift::Result<i32> { println!("testi32({})", thing); Ok(thing) } - fn handle_test_i64(&mut self, thing: i64) -> thrift::Result<i64> { + fn handle_test_i64(&self, thing: i64) -> thrift::Result<i64> { println!("testi64({})", thing); Ok(thing) } - fn handle_test_double(&mut self, - thing: OrderedFloat<f64>) - -> thrift::Result<OrderedFloat<f64>> { + fn handle_test_double(&self, thing: OrderedFloat<f64>) -> thrift::Result<OrderedFloat<f64>> { println!("testDouble({})", thing); Ok(thing) } - fn handle_test_binary(&mut self, thing: Vec<u8>) -> thrift::Result<Vec<u8>> { + fn handle_test_binary(&self, thing: Vec<u8>) -> thrift::Result<Vec<u8>> { println!("testBinary({:?})", thing); Ok(thing) } - fn handle_test_struct(&mut self, thing: Xtruct) -> thrift::Result<Xtruct> { + fn handle_test_struct(&self, thing: Xtruct) -> thrift::Result<Xtruct> { println!("testStruct({:?})", thing); Ok(thing) } - fn handle_test_nest(&mut self, thing: Xtruct2) -> thrift::Result<Xtruct2> { + fn handle_test_nest(&self, thing: Xtruct2) -> thrift::Result<Xtruct2> { println!("testNest({:?})", thing); Ok(thing) } - fn handle_test_map(&mut self, thing: BTreeMap<i32, i32>) -> thrift::Result<BTreeMap<i32, i32>> { + fn handle_test_map(&self, thing: BTreeMap<i32, i32>) -> thrift::Result<BTreeMap<i32, i32>> { println!("testMap({:?})", thing); Ok(thing) } - fn handle_test_string_map(&mut self, - thing: BTreeMap<String, String>) - -> thrift::Result<BTreeMap<String, String>> { + fn handle_test_string_map( + &self, + thing: BTreeMap<String, String>, + ) -> thrift::Result<BTreeMap<String, String>> { println!("testStringMap({:?})", thing); Ok(thing) } - fn handle_test_set(&mut self, thing: BTreeSet<i32>) -> thrift::Result<BTreeSet<i32>> { + fn handle_test_set(&self, thing: BTreeSet<i32>) -> thrift::Result<BTreeSet<i32>> { println!("testSet({:?})", thing); Ok(thing) } - fn handle_test_list(&mut self, thing: Vec<i32>) -> thrift::Result<Vec<i32>> { + fn handle_test_list(&self, thing: Vec<i32>) -> thrift::Result<Vec<i32>> { println!("testList({:?})", thing); Ok(thing) } - fn handle_test_enum(&mut self, thing: Numberz) -> thrift::Result<Numberz> { + fn handle_test_enum(&self, thing: Numberz) -> thrift::Result<Numberz> { println!("testEnum({:?})", thing); Ok(thing) } - fn handle_test_typedef(&mut self, thing: UserId) -> thrift::Result<UserId> { + fn handle_test_typedef(&self, thing: UserId) -> thrift::Result<UserId> { println!("testTypedef({})", thing); Ok(thing) } /// @return map<i32,map<i32,i32>> - returns a dictionary with these values: - /// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, } - fn handle_test_map_map(&mut self, - hello: i32) - -> thrift::Result<BTreeMap<i32, BTreeMap<i32, i32>>> { + /// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => + /// 2, 3 => 3, 4 => 4, }, } + fn handle_test_map_map(&self, hello: i32) -> thrift::Result<BTreeMap<i32, BTreeMap<i32, i32>>> { println!("testMapMap({})", hello); let mut inner_map_0: BTreeMap<i32, i32> = BTreeMap::new(); @@ -232,9 +250,10 @@ impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl { /// 2 => { 6 => <empty Insanity struct>, }, /// } /// return map<UserId, map<Numberz,Insanity>> - a map with the above values - fn handle_test_insanity(&mut self, - argument: Insanity) - -> thrift::Result<BTreeMap<UserId, BTreeMap<Numberz, Insanity>>> { + fn handle_test_insanity( + &self, + argument: Insanity, + ) -> thrift::Result<BTreeMap<UserId, BTreeMap<Numberz, Insanity>>> { println!("testInsanity({:?})", argument); let mut map_0: BTreeMap<Numberz, Insanity> = BTreeMap::new(); map_0.insert(Numberz::TWO, argument.clone()); @@ -254,15 +273,18 @@ impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl { Ok(ret) } - /// returns an Xtruct with string_thing = "Hello2", byte_thing = arg0, i32_thing = arg1 and i64_thing = arg2 - fn handle_test_multi(&mut self, - arg0: i8, - arg1: i32, - arg2: i64, - _: BTreeMap<i16, String>, - _: Numberz, - _: UserId) - -> thrift::Result<Xtruct> { + /// returns an Xtruct with: + /// string_thing = "Hello2", byte_thing = arg0, i32_thing = arg1 and + /// i64_thing = arg2 + fn handle_test_multi( + &self, + arg0: i8, + arg1: i32, + arg2: i64, + _: BTreeMap<i16, String>, + _: Numberz, + _: UserId, + ) -> thrift::Result<Xtruct> { let x_ret = Xtruct { string_thing: Some("Hello2".to_owned()), byte_thing: Some(arg0), @@ -273,64 +295,77 @@ impl ThriftTestSyncHandler for ThriftTestSyncHandlerImpl { Ok(x_ret) } - /// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg + /// if arg == "Xception" throw Xception with errorCode = 1001 and message = + /// arg /// else if arg == "TException" throw TException /// else do not throw anything - fn handle_test_exception(&mut self, arg: String) -> thrift::Result<()> { + fn handle_test_exception(&self, arg: String) -> thrift::Result<()> { println!("testException({})", arg); match &*arg { "Xception" => { - Err((Xception { - error_code: Some(1001), - message: Some(arg), - }) - .into()) + Err( + (Xception { + error_code: Some(1001), + message: Some(arg), + }) + .into(), + ) } "TException" => Err("this is a random error".into()), _ => Ok(()), } } - /// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception" - /// else if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and struct_thing.string_thing = "This is an Xception2" - // else do not throw anything and return Xtruct with string_thing = arg1 - fn handle_test_multi_exception(&mut self, - arg0: String, - arg1: String) - -> thrift::Result<Xtruct> { + /// if arg0 == "Xception": + /// throw Xception with errorCode = 1001 and message = "This is an + /// Xception" + /// else if arg0 == "Xception2": + /// throw Xception2 with errorCode = 2002 and struct_thing.string_thing = + /// "This is an Xception2" + // else: + // do not throw anything and return Xtruct with string_thing = arg1 + fn handle_test_multi_exception(&self, arg0: String, arg1: String) -> thrift::Result<Xtruct> { match &*arg0 { "Xception" => { - Err((Xception { - error_code: Some(1001), - message: Some("This is an Xception".to_owned()), - }) - .into()) + Err( + (Xception { + error_code: Some(1001), + message: Some("This is an Xception".to_owned()), + }) + .into(), + ) } "Xception2" => { - Err((Xception2 { - error_code: Some(2002), - struct_thing: Some(Xtruct { - string_thing: Some("This is an Xception2".to_owned()), - byte_thing: None, - i32_thing: None, - i64_thing: None, - }), - }) - .into()) + Err( + (Xception2 { + error_code: Some(2002), + struct_thing: Some( + Xtruct { + string_thing: Some("This is an Xception2".to_owned()), + byte_thing: None, + i32_thing: None, + i64_thing: None, + }, + ), + }) + .into(), + ) } _ => { - Ok(Xtruct { - string_thing: Some(arg1), - byte_thing: None, - i32_thing: None, - i64_thing: None, - }) + Ok( + Xtruct { + string_thing: Some(arg1), + byte_thing: None, + i32_thing: None, + i64_thing: None, + }, + ) } } } - fn handle_test_oneway(&mut self, seconds_to_sleep: i32) -> thrift::Result<()> { + fn handle_test_oneway(&self, seconds_to_sleep: i32) -> thrift::Result<()> { thread::sleep(Duration::from_secs(seconds_to_sleep as u64)); Ok(()) } |