Skip to content

Instantly share code, notes, and snippets.

@allengeorge
Last active June 22, 2024 04:31
Show Gist options
  • Save allengeorge/5ff97e3b6541451556fbe8124921629a to your computer and use it in GitHub Desktop.
Save allengeorge/5ff97e3b6541451556fbe8124921629a to your computer and use it in GitHub Desktop.
Rust Thrift Example (client.rs)
extern crate thrift;
extern crate example;
use thrift::protocol::{TCompactInputProtocol, TCompactOutputProtocol};
use thrift::transport::{TFramedReadTransport, TFramedWriteTransport, TIoChannel, TTcpChannel};
use example::{SimpleServiceSyncClient, TSimpleServiceSyncClient};
fn main() {
match run() {
Ok(()) => println!("client ran successfully"),
Err(e) => {
println!("client failed with error {:?}", e);
std::process::exit(1);
}
}
}
fn run() -> thrift::Result<()> {
// build our client and connect to the host:port
let mut c = TTcpChannel::new();
c.open("127.0.0.1:9000")?;
let (i_chan, o_chan) = c.split()?;
// build the input/output protocol
let i_prot = TCompactInputProtocol::new(TFramedReadTransport::new(i_chan));
let o_prot = TCompactOutputProtocol::new(TFramedWriteTransport::new(o_chan));
// use the input/output protocol to create a Thrift client
let mut client = SimpleServiceSyncClient::new(i_prot, o_prot);
// make service calls
let res = client.hello("Allen".to_owned())?;
println!("{}", res);
// done!
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment