Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation (ASF) under one |
| 2 | // or more contributor license agreements. See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership. The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | use std::cell::RefCell; |
| 19 | use std::rc::Rc; |
| 20 | use std::io; |
| 21 | use std::io::{Read, Write}; |
| 22 | |
| 23 | use super::TTransport; |
| 24 | |
| 25 | /// Proxy that wraps an inner `TTransport` and delegates all calls to it. |
| 26 | /// |
| 27 | /// Unlike other `TTransport` wrappers, `TPassThruTransport` is generic with |
| 28 | /// regards to the wrapped transport. This allows callers to use methods |
| 29 | /// specific to the type being wrapped instead of being constrained to methods |
| 30 | /// on the `TTransport` trait. |
| 31 | /// |
| 32 | /// # Examples |
| 33 | /// |
| 34 | /// Create and use a `TPassThruTransport`. |
| 35 | /// |
| 36 | /// ```no_run |
| 37 | /// use std::cell::RefCell; |
| 38 | /// use std::rc::Rc; |
| 39 | /// use thrift::transport::{TPassThruTransport, TTcpTransport}; |
| 40 | /// |
| 41 | /// let t = TTcpTransport::new(); |
| 42 | /// let t = TPassThruTransport::new(Rc::new(RefCell::new(Box::new(t)))); |
| 43 | /// |
| 44 | /// // since the type parameter is maintained, we are able |
| 45 | /// // to use functions specific to `TTcpTransport` |
| 46 | /// t.inner.borrow_mut().open("localhost:9090").unwrap(); |
| 47 | /// ``` |
| 48 | pub struct TPassThruTransport<I: TTransport> { |
| 49 | pub inner: Rc<RefCell<Box<I>>>, |
| 50 | } |
| 51 | |
| 52 | impl<I: TTransport> TPassThruTransport<I> { |
| 53 | /// Create a `TPassThruTransport` that wraps an `inner` TTransport. |
| 54 | pub fn new(inner: Rc<RefCell<Box<I>>>) -> TPassThruTransport<I> { |
| 55 | TPassThruTransport { inner: inner } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl<I: TTransport> Read for TPassThruTransport<I> { |
| 60 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 61 | self.inner.borrow_mut().read(buf) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl<I: TTransport> Write for TPassThruTransport<I> { |
| 66 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 67 | self.inner.borrow_mut().write(buf) |
| 68 | } |
| 69 | |
| 70 | fn flush(&mut self) -> io::Result<()> { |
| 71 | self.inner.borrow_mut().flush() |
| 72 | } |
| 73 | } |