rust


How to return a struct with a generic type that implements the `Read` and `Write` traits?


I am trying to wrap a TcpStream and TlsStream in one object so that I can interface with either of them using one struct. I am trying to delegate the io methods to one or the other based on a config value but can't figure out how to return a struct with a generic type that implements the Read and Write traits
My code is as follows
pub struct TcpStream<T: Read + Write> {
io_delegate: T,
config: Config,
}
impl<T> TcpStream<T>
where T: Read + Write
{
pub fn connect<A: ToSocketAddrs>(config: Config, addr: A) -> io::Result<TcpStream<T>> {
let tcp_stream = net::TcpStream::connect(addr).unwrap();
if config.ssl {
let tls_stream = TlsConnector::builder()
.unwrap()
.build()
.unwrap()
.connect("rem", tcp_stream)
.unwrap();
return Ok(TcpStream {
config: config,
io_delegate: tls_stream,
});
}
return Ok(TcpStream {
config: config,
io_delegate: tcp_stream,
});
}
}
When I try to compile I get the following errors
error[E0308]: mismatched types
--> src/rem/tcp_stream.rs:19:23
|
19 | return Ok(TcpStream {
| _______________________^ starting here...
20 | | config: config,
21 | | io_delegate: tls_stream
22 | | });
| |_____________^ ...ending here: expected type parameter, found struct `native_tls::TlsStream`
|
= note: expected type `rem::tcp_stream::TcpStream<T>`
found type `rem::tcp_stream::TcpStream<native_tls::TlsStream<std::net::TcpStream>>`
error[E0308]: mismatched types
--> src/rem/tcp_stream.rs:24:19
|
24 | return Ok(TcpStream{
| ___________________^ starting here...
25 | | config: config,
26 | | io_delegate: tcp_stream
27 | | });
| |_________^ ...ending here: expected type parameter, found struct `std::net::TcpStream`
|
= note: expected type `rem::tcp_stream::TcpStream<T>`
found type `rem::tcp_stream::TcpStream<std::net::TcpStream>`
Is there a way to achieve this sort of thing?
I'm not sure if this is the best solution but it does seem to work.
I created a new trait which is a combination of Read + Write then just stored it as a Box in my struct
trait ReadWrite : Read + Write {}
impl<T: Read + Write> ReadWrite for T {}
pub struct TcpStream{
io_delegate : Box<ReadWrite>,
config: Config
}
impl TcpStream {
pub fn connect<A: ToSocketAddrs>(config: Config, addr: A) -> TcpStream {
let tcp_stream = net::TcpStream::connect(addr).unwrap();
if config.ssl {
let tls_stream = TlsConnector::builder().unwrap().build().unwrap().connect("rem", tcp_stream).unwrap();
return TcpStream {
config: config,
io_delegate: Box::new(tls_stream)
};
}
return TcpStream{
config: config,
io_delegate:Box::new(tcp_stream)
};
}
}

Related Links

Iterator on Option<Vec<>>
Literal out of range warning when iterating over all values of u8
How to write a trait that has a method returning a reference and implement it correctly?
Mismatched types when displaying a matrix with a for loop
How to have a struct field with the same mutability as the parent struct?
`if` condition remains borrowed in body [duplicate]
“parameter `'a` is never used” error when 'a is used in type parameter bound
Linking Rust application with a dynamic library not in the runtime linker search path
Can't access environment variable in Rust
How to wrap a call to a FFI function that uses VarArgs in Rust?
Change terminal cursor position in Rust
Is it better to destructure a value to access attributes or use their names?
How is every dynamically checked aspect of Rust implemented?
Referring to matched value in Rust
Reference to unwrapped property fails: use of partially moved value: `self`
Read one level of directory structure

Categories

HOME
memory
add-on
apache-kafka-connect
impala
onvif
kurento
cisco
gtk
yahoo
grocery-crud
smartgwt
texas-instruments
android-contacts
akka-persistence
tortoisehg
basex
spring-annotations
es6-modules
autodesk-model-derivative
steam-web-api
memory-fences
john-the-ripper
rspec-rails
ms-media-foundation
markov-chains
bootstrap-sass
android-canvas
bean-validation
forms-authentication
fastlane
recovery
python-jira
vb.net-to-c#
secure-coding
contactless-smartcard
asihttprequest
bit-shift
beeline
google-news
http-basic-authentication
openerp-6
qtextedit
ternary-operator
thread-exceptions
android-syncadapter
flipclock
opencyc
chicagoboss
asp.net-web-api-helppages
compositetype
dbmigrate
subview
fiware-bosun
password-recovery
dmake
jquery-mobile-flipswitch
aho-corasick
oxygene
application-blocks
iirf
libxml-js
microformats
contextswitchdeadlock
instance-variables
prototypal-inheritance
nsfont
uploading
navigationcontroller
n-queens
background-repeat
asio
office-automation
kext
kernel32
regression-testing
formal-semantics
hungarian-notation
mysqli-multi-query
main-method
bindable-linq

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App