Created
November 2, 2019 21:43
-
-
Save mdonoughe/89d273c0046c5f19f39d23ef9ac6b218 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// error[E0597]: `arc` does not live long enough | |
use std::cmp::min; | |
use std::io::{self, Write}; | |
use std::sync::Arc; | |
use tokio::sync::Mutex; | |
use tokio_executor::blocking; | |
fn send<T: Write + 'static + Send + Sync>( | |
dest: &Arc<Mutex<T>>, | |
mut buf: Vec<u8>, | |
) -> impl Future<Output = Result<Vec<u8>, io::Error>> { | |
let arc = dest.clone(); | |
async move { | |
let mut lock = arc.lock(); | |
// ^^^------- | |
// | | |
// borrowed value does not live long enough | |
// argument requires that `arc` is borrowed for `'static` | |
let mut dest = lock.await; | |
let result = blocking::run(move || { | |
let len = dest.write(&buf[..])?; | |
let remainder = buf.len() - len; | |
if buf.len() - len > 0 { | |
buf.copy_within(len.., 0); | |
buf.truncate(len); | |
} else { | |
buf.clear(); | |
} | |
Ok(buf) | |
}) | |
.await; | |
result | |
} | |
// - `arc` dropped here while still borrowed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment