mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-28 00:00:11 +02:00
Tweak the tokio::spawn function (#171)
Currently, `tokio::spawn` matched the `spawn` function from futures 0.2. However, this adds additional ergonomic overhead and removes the ability to spawn from a drop fn. See rust-lang-nursery/futures-rs#830. This patch switches the behavior to access the thread-local variable referencing the default executor directly in the `spawn` function.
This commit is contained in:
+8
-8
@@ -81,7 +81,7 @@ fn hammer_split() {
|
||||
|
||||
let mut rt = Runtime::new().unwrap();
|
||||
|
||||
fn split(socket: TcpStream) -> Box<Future<Item = (), Error = ()> + Send> {
|
||||
fn split(socket: TcpStream) {
|
||||
let socket = Arc::new(socket);
|
||||
let rd = Rd(socket.clone());
|
||||
let wr = Wr(socket);
|
||||
@@ -94,25 +94,25 @@ fn hammer_split() {
|
||||
.map(|_| ())
|
||||
.map_err(|e| panic!("write error = {:?}", e));
|
||||
|
||||
Box::new({
|
||||
tokio::spawn(rd)
|
||||
.join(tokio::spawn(wr))
|
||||
.map(|_| ())
|
||||
})
|
||||
tokio::spawn(rd);
|
||||
tokio::spawn(wr);
|
||||
}
|
||||
|
||||
rt.spawn({
|
||||
srv.incoming()
|
||||
.map_err(|e| panic!("accept error = {:?}", e))
|
||||
.take(N as u64)
|
||||
.for_each(|socket| split(socket))
|
||||
.for_each(|socket| {
|
||||
split(socket);
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
|
||||
for _ in 0..N {
|
||||
rt.spawn({
|
||||
TcpStream::connect(&addr)
|
||||
.map_err(|e| panic!("connect error = {:?}", e))
|
||||
.and_then(|socket| split(socket))
|
||||
.map(|socket| split(socket))
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user