Hi All,
I recently started to learn rust by creating this project. Although I did hit a few bumps it generally went ok and I was starting to feel like I'm getting the hang of it, however, in the last 3 days I hit a wall trying to add SSL support and no matter what road I take to work around the problem I can't go through. The entire project in it's current failed state is here.
The first problem is around this code (indentation removed):
let listener = listener.to_listener().unwrap();
let http = Http::new();
let state = Arc::clone(&state);
let acceptor = tls_acceptor.clone();
let done = listener.incoming()
.map_err(|e| error!("{:?}", e))
.for_each(move |stream| {
let addr = stream.peer_addr().unwrap();
let done = acceptor.accept(stream)
.map_err(|e| error!("{:?}", e))
.and_then(|stream| {
let service = MainService {
state: Arc::clone(&state),
remote_addr: addr,
};
let done = http.serve_connection(stream, service)
.map_err(|e| error!("{:?}", e));
tokio::spawn(done)
});
tokio::spawn(done)
});
Box::new(done)
When compiling it as it is I get the following error:
error: lifetime may not live long enough
--> src/web/mod.rs:138:36
|
136 | .for_each(move |stream| {
| ------------- lifetime `'1` represents this closure's body
137 | let addr = stream.peer_addr().unwrap();
138 | let done = acceptor.accept(stream)
| ____________________________________^
139 | | .map_err(|e| error!("{:?}", e))
140 | | .and_then(|stream| {
141 | | let service = MainService {
... |
147 | | tokio::spawn(done)
148 | | });
| |______________________________^ argument requires that `'1` must outlive `'static`
|
= note: closure implements `FnMut`, so references to captured variables can't escape the closure
error[E0373]: closure may outlive the current function, but it borrows `addr`, which is owned by the current function
--> src/web/mod.rs:140:39
|
140 | .and_then(|stream| {
| ^^^^^^^^ may outlive borrowed value `addr`
...
143 | remote_addr: addr,
| ---- `addr` is borrowed here
|
Looking around for solution I tried to add move
to line 140:
.and_then(move |stream| {
... but now I get different error:
error[E0507]: cannot move out of captured variable in an `FnMut` closure
--> src/web/mod.rs:140:39
|
132 | let state = Arc::clone(&state);
| ----- captured outer variable
...
140 | .and_then(move |stream| {
| _______________________________________^
141 | | let service = MainService {
142 | | state: Arc::clone(&state),
143 | | remote_addr: addr,
... |
147 | | tokio::spawn(done)
148 | | });
| |_____________________________^ cannot move out of captured variable in an `FnMut` closure
error[E0507]: cannot move out of captured variable in an `FnMut` closure
--> src/web/mod.rs:140:39
|
131 | let http = Http::new();
| ---- captured outer variable
...
140 | .and_then(move |stream| {
| _______________________________________^
141 | | let service = MainService {
142 | | state: Arc::clone(&state),
143 | | remote_addr: addr,
... |
147 | | tokio::spawn(done)
148 | | });
| |_____________________________^ cannot move out of captured variable in an `FnMut` closure
,
While this code contains other directions I took trying to work around this (like the run2
function here - which upon switching the commented Server:Http
match branch I get an error about Streams not implemented for TCPListener...), however I want to first focus on the first errors.
Is there someone who care's to put in the time and help me solve this?
Thanks a lot, really ,