├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── examples ├── echo.rs └── hello.rs └── src ├── background.rs ├── builder.rs ├── enter.rs ├── lib.rs ├── park.rs └── task_executor.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | 5 | .vscode/ 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | sudo: false 3 | 4 | os: 5 | - linux 6 | 7 | rust: 8 | - nightly 9 | 10 | evn: 11 | - RUST_BACKTRACE=1 12 | 13 | script: 14 | - cargo test -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "may_future" 3 | version = "0.1.0" 4 | authors = ["Xudong Huang "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | may = "0.3" 9 | co_waiter = { git = "https://github.com/Xudong-Huang/co_waiter.git" } 10 | num_cpus = "1" 11 | lazy_static = "1" 12 | tracing-core = "0.1" 13 | tokio-sync = { git = "https://github.com/tokio-rs/tokio.git" } 14 | tokio-timer = { git = "https://github.com/tokio-rs/tokio.git" } 15 | tokio-reactor = { git = "https://github.com/tokio-rs/tokio.git" } 16 | tokio-executor = { git = "https://github.com/tokio-rs/tokio.git" } 17 | tokio-threadpool = { git = "https://github.com/tokio-rs/tokio.git" } 18 | tokio-current-thread = { git = "https://github.com/tokio-rs/tokio.git" } 19 | 20 | [dev-dependencies] 21 | tokio = { git = "https://github.com/tokio-rs/tokio.git" } 22 | 23 | # [patch.crates-io] 24 | # may = { git = "https://github.com/Xudong-Huang/may.git" } -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 Xudong Huang 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # may_future 2 | 3 | [![Travis Build Status](https://travis-ci.org/Xudong-Huang/may_future.svg?branch=master)](https://travis-ci.org/Xudong-Huang/may_future) 4 | 5 | 6 | Future runtime in [may][may] 7 | 8 | 9 | ## Overview 10 | 11 | **may_future** is a runtime library which allows to execute futures on the coroutine context. Specifically the `block_on` API of the runtime would not block the underlying worker thread that is scheduling the coroutine. It also supply a global static runtime named `may_future::RT` which is convenient to spawn or block_on futures at hand. 12 | 13 | Internally it use the tokio runtime for implementation. This opens the possibility to integrate any features from the tokio stack. 14 | 15 | ## Example 16 | 17 | ```rust 18 | #![feature(async_await)] 19 | 20 | use may::{coroutine, go}; 21 | use may_future::RT; 22 | 23 | fn main() { 24 | RT.block_on(async { 25 | println!("hello world"); 26 | }); 27 | 28 | coroutine::scope(|s| { 29 | for i in 0..100 { 30 | go!(s, move || { 31 | RT.block_on(async { 32 | println!("hello world from coroutine {}", i); 33 | }); 34 | }); 35 | } 36 | }); 37 | } 38 | ``` 39 | 40 | 41 | 42 | # License 43 | 44 | This project is licensed under either of 45 | 46 | * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or 47 | http://www.apache.org/licenses/LICENSE-2.0) 48 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or 49 | http://opensource.org/licenses/MIT) 50 | 51 | at your option. 52 | 53 | 54 | [may]:https://github.com/Xudong-Huang/may 55 | -------------------------------------------------------------------------------- /examples/echo.rs: -------------------------------------------------------------------------------- 1 | #![feature(async_await)] 2 | 3 | use may::{coroutine, go}; 4 | use tokio::io::{AsyncReadExt, AsyncWriteExt}; 5 | use tokio::net::TcpStream; 6 | use tokio_reactor::Handle; 7 | 8 | #[cfg(unix)] 9 | fn convert_stream(s: may::net::TcpStream) -> TcpStream { 10 | use std::os::unix::io::{FromRawFd, IntoRawFd}; 11 | let raw = s.into_raw_fd(); 12 | let std = unsafe { std::net::TcpStream::from_raw_fd(raw) }; 13 | TcpStream::from_std(std, &Handle::default()).expect("error") 14 | } 15 | 16 | // FIXME: windows is not working for that read async socket would return 17 | // invalid parameter error 18 | #[cfg(windows)] 19 | fn convert_stream(s: may::net::TcpStream) -> TcpStream { 20 | use std::os::windows::io::{FromRawSocket, IntoRawSocket}; 21 | let raw = s.into_raw_socket(); 22 | let std = unsafe { std::net::TcpStream::from_raw_socket(raw) }; 23 | TcpStream::from_std(std, &Handle::default()).expect("error") 24 | } 25 | 26 | fn handle_client(stream: may::net::TcpStream) { 27 | let mut socket = convert_stream(stream); 28 | may_future::RT.block_on(async move { 29 | let mut buf = [0; 1024]; 30 | 31 | // In a loop, read data from the socket and write the data back. 32 | loop { 33 | // windows read is not correct! 34 | let n = socket.read(&mut buf).await.unwrap(); 35 | 36 | if n == 0 { 37 | return; 38 | } 39 | 40 | socket.write_all(&buf[0..n]).await.unwrap(); 41 | } 42 | }); 43 | } 44 | 45 | fn main() { 46 | may::config().set_stack_size(0x2000); 47 | coroutine::scope(|s| { 48 | for _ in 0..1 { 49 | go!(s, move || { 50 | let listener = may::net::TcpListener::bind(("0.0.0.0", 8080)).unwrap(); 51 | for stream in listener.incoming() { 52 | match stream { 53 | Ok(s) => { 54 | go!(move || handle_client(s)); 55 | } 56 | Err(e) => println!("err = {:?}", e), 57 | } 58 | } 59 | }); 60 | } 61 | }); 62 | } 63 | -------------------------------------------------------------------------------- /examples/hello.rs: -------------------------------------------------------------------------------- 1 | #![feature(async_await)] 2 | 3 | use may::{coroutine, go}; 4 | use may_future::RT; 5 | 6 | use std::sync::atomic::{AtomicUsize, Ordering}; 7 | 8 | fn main() { 9 | RT.block_on(async { 10 | println!("hello world"); 11 | }); 12 | 13 | let j = AtomicUsize::new(0); 14 | coroutine::scope(|s| { 15 | for i in 0..100 { 16 | let j = &j; 17 | go!(s, move || { 18 | RT.block_on_local(async { 19 | println!( 20 | "hello world from coroutine {}, data={}", 21 | i, 22 | j.fetch_add(i, Ordering::Relaxed) + i 23 | ); 24 | }); 25 | }); 26 | } 27 | }); 28 | } 29 | -------------------------------------------------------------------------------- /src/background.rs: -------------------------------------------------------------------------------- 1 | //! Temporary reactor + timer that runs on a background thread. This it to make 2 | //! `block_on` work. 3 | 4 | use tokio_current_thread::CurrentThread; 5 | use tokio_reactor::Reactor; 6 | use tokio_sync::oneshot; 7 | use tokio_timer::clock::Clock; 8 | use tokio_timer::timer::{self, Timer}; 9 | 10 | use std::{io, thread}; 11 | 12 | #[derive(Debug)] 13 | pub struct Background { 14 | reactor_handle: tokio_reactor::Handle, 15 | timer_handle: timer::Handle, 16 | shutdown_tx: Option>, 17 | thread: Option>, 18 | } 19 | 20 | pub fn spawn(clock: &Clock) -> io::Result { 21 | let clock = clock.clone(); 22 | 23 | let reactor = Reactor::new()?; 24 | let reactor_handle = reactor.handle(); 25 | 26 | let timer = Timer::new_with_now(reactor, clock); 27 | let timer_handle = timer.handle(); 28 | 29 | let (shutdown_tx, shutdown_rx) = oneshot::channel(); 30 | let shutdown_tx = Some(shutdown_tx); 31 | 32 | let thread = thread::spawn(move || { 33 | let mut rt = CurrentThread::new_with_park(timer); 34 | let _ = rt.block_on(shutdown_rx); 35 | }); 36 | let thread = Some(thread); 37 | 38 | Ok(Background { 39 | reactor_handle, 40 | timer_handle, 41 | shutdown_tx, 42 | thread, 43 | }) 44 | } 45 | 46 | impl Background { 47 | pub(super) fn reactor(&self) -> &tokio_reactor::Handle { 48 | &self.reactor_handle 49 | } 50 | 51 | pub(super) fn timer(&self) -> &timer::Handle { 52 | &self.timer_handle 53 | } 54 | } 55 | 56 | impl Drop for Background { 57 | fn drop(&mut self) { 58 | let _ = self.shutdown_tx.take().unwrap().send(()); 59 | let _ = self.thread.take().unwrap().join(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/builder.rs: -------------------------------------------------------------------------------- 1 | use super::{background, Inner, Runtime}; 2 | use tokio_reactor::Reactor; 3 | 4 | use tokio_reactor; 5 | use tokio_threadpool::Builder as ThreadPoolBuilder; 6 | use tokio_timer::clock::{self, Clock}; 7 | use tokio_timer::timer::{self, Timer}; 8 | 9 | use num_cpus; 10 | use tracing_core as trace; 11 | 12 | use std::any::Any; 13 | use std::io; 14 | use std::sync::Mutex; 15 | use std::time::Duration; 16 | 17 | /// Builds Tokio Runtime with custom configuration values. 18 | /// 19 | /// Methods can be chained in order to set the configuration values. The 20 | /// Runtime is constructed by calling [`build`]. 21 | /// 22 | /// New instances of `Builder` are obtained via [`Builder::new`]. 23 | /// 24 | /// See function level documentation for details on the various configuration 25 | /// settings. 26 | /// 27 | /// [`build`]: #method.build 28 | /// [`Builder::new`]: #method.new 29 | /// 30 | /// # Examples 31 | /// 32 | /// ``` 33 | /// use std::time::Duration; 34 | /// 35 | /// use may_future::Builder; 36 | /// use tokio_timer::clock::Clock; 37 | /// 38 | /// fn main() { 39 | /// // build Runtime 40 | /// let mut runtime = Builder::new() 41 | /// .blocking_threads(4) 42 | /// .clock(Clock::system()) 43 | /// .core_threads(4) 44 | /// .keep_alive(Some(Duration::from_secs(60))) 45 | /// .name_prefix("my-custom-name-") 46 | /// .stack_size(3 * 1024 * 1024) 47 | /// .build() 48 | /// .unwrap(); 49 | /// 50 | /// // use runtime ... 51 | /// } 52 | /// ``` 53 | #[derive(Debug)] 54 | pub struct Builder { 55 | /// Thread pool specific builder 56 | threadpool_builder: ThreadPoolBuilder, 57 | 58 | /// The number of worker threads 59 | core_threads: usize, 60 | 61 | /// The clock to use 62 | clock: Clock, 63 | } 64 | 65 | impl Builder { 66 | /// Returns a new runtime builder initialized with default configuration 67 | /// values. 68 | /// 69 | /// Configuration methods can be chained on the return value. 70 | pub fn new() -> Builder { 71 | let core_threads = num_cpus::get().max(1); 72 | 73 | let mut threadpool_builder = ThreadPoolBuilder::new(); 74 | threadpool_builder.name_prefix("tokio-runtime-worker-"); 75 | threadpool_builder.pool_size(core_threads); 76 | 77 | Builder { 78 | threadpool_builder, 79 | core_threads, 80 | clock: Clock::new(), 81 | } 82 | } 83 | 84 | /// Set the `Clock` instance that will be used by the runtime. 85 | pub fn clock(&mut self, clock: Clock) -> &mut Self { 86 | self.clock = clock; 87 | self 88 | } 89 | 90 | /// Sets a callback to handle panics in futures. 91 | /// 92 | /// The callback is triggered when a panic during a future bubbles up to 93 | /// Tokio. By default Tokio catches these panics, and they will be ignored. 94 | /// The parameter passed to this callback is the same error value returned 95 | /// from `std::panic::catch_unwind()`. To abort the process on panics, use 96 | /// `std::panic::resume_unwind()` in this callback as shown below. 97 | /// 98 | /// # Examples 99 | /// 100 | /// ``` 101 | /// # pub fn main() { 102 | /// let mut rt = may_future::Builder::new() 103 | /// .panic_handler(|err| std::panic::resume_unwind(err)) 104 | /// .build() 105 | /// .unwrap(); 106 | /// # } 107 | /// ``` 108 | pub fn panic_handler(&mut self, f: F) -> &mut Self 109 | where 110 | F: Fn(Box) + Send + Sync + 'static, 111 | { 112 | self.threadpool_builder.panic_handler(f); 113 | self 114 | } 115 | 116 | /// Set the maximum number of worker threads for the `Runtime`'s thread pool. 117 | /// 118 | /// This must be a number between 1 and 32,768 though it is advised to keep 119 | /// this value on the smaller side. 120 | /// 121 | /// The default value is the number of cores available to the system. 122 | /// 123 | /// # Examples 124 | /// 125 | /// ``` 126 | /// # pub fn main() { 127 | /// let mut rt = may_future::Builder::new() 128 | /// .core_threads(4) 129 | /// .build() 130 | /// .unwrap(); 131 | /// # } 132 | /// ``` 133 | pub fn core_threads(&mut self, val: usize) -> &mut Self { 134 | self.core_threads = val; 135 | self.threadpool_builder.pool_size(val); 136 | self 137 | } 138 | 139 | /// Set the maximum number of concurrent blocking sections in the `Runtime`'s 140 | /// thread pool. 141 | /// 142 | /// When the maximum concurrent `blocking` calls is reached, any further 143 | /// calls to `blocking` will return `NotReady` and the task is notified once 144 | /// previously in-flight calls to `blocking` return. 145 | /// 146 | /// This must be a number between 1 and 32,768 though it is advised to keep 147 | /// this value on the smaller side. 148 | /// 149 | /// The default value is 100. 150 | /// 151 | /// # Examples 152 | /// 153 | /// ``` 154 | /// # pub fn main() { 155 | /// let mut rt = may_future::Builder::new() 156 | /// .blocking_threads(200) 157 | /// .build(); 158 | /// # } 159 | /// ``` 160 | pub fn blocking_threads(&mut self, val: usize) -> &mut Self { 161 | self.threadpool_builder.max_blocking(val); 162 | self 163 | } 164 | 165 | /// Set the worker thread keep alive duration for threads in the `Runtime`'s 166 | /// thread pool. 167 | /// 168 | /// If set, a worker thread will wait for up to the specified duration for 169 | /// work, at which point the thread will shutdown. When work becomes 170 | /// available, a new thread will eventually be spawned to replace the one 171 | /// that shut down. 172 | /// 173 | /// When the value is `None`, the thread will wait for work forever. 174 | /// 175 | /// The default value is `None`. 176 | /// 177 | /// # Examples 178 | /// 179 | /// ``` 180 | /// use std::time::Duration; 181 | /// 182 | /// # pub fn main() { 183 | /// let mut rt = may_future::Builder::new() 184 | /// .keep_alive(Some(Duration::from_secs(30))) 185 | /// .build(); 186 | /// # } 187 | /// ``` 188 | pub fn keep_alive(&mut self, val: Option) -> &mut Self { 189 | self.threadpool_builder.keep_alive(val); 190 | self 191 | } 192 | 193 | /// Set name prefix of threads spawned by the `Runtime`'s thread pool. 194 | /// 195 | /// Thread name prefix is used for generating thread names. For example, if 196 | /// prefix is `my-pool-`, then threads in the pool will get names like 197 | /// `my-pool-1` etc. 198 | /// 199 | /// The default prefix is "tokio-runtime-worker-". 200 | /// 201 | /// # Examples 202 | /// 203 | /// ``` 204 | /// # pub fn main() { 205 | /// let mut rt = may_future::Builder::new() 206 | /// .name_prefix("my-pool-") 207 | /// .build(); 208 | /// # } 209 | /// ``` 210 | pub fn name_prefix>(&mut self, val: S) -> &mut Self { 211 | self.threadpool_builder.name_prefix(val); 212 | self 213 | } 214 | 215 | /// Set the stack size (in bytes) for worker threads. 216 | /// 217 | /// The actual stack size may be greater than this value if the platform 218 | /// specifies minimal stack size. 219 | /// 220 | /// The default stack size for spawned threads is 2 MiB, though this 221 | /// particular stack size is subject to change in the future. 222 | /// 223 | /// # Examples 224 | /// 225 | /// ``` 226 | /// # pub fn main() { 227 | /// let mut rt = may_future::Builder::new() 228 | /// .stack_size(32 * 1024) 229 | /// .build(); 230 | /// # } 231 | /// ``` 232 | pub fn stack_size(&mut self, val: usize) -> &mut Self { 233 | self.threadpool_builder.stack_size(val); 234 | self 235 | } 236 | 237 | /// Execute function `f` after each thread is started but before it starts 238 | /// doing work. 239 | /// 240 | /// This is intended for bookkeeping and monitoring use cases. 241 | /// 242 | /// # Examples 243 | /// 244 | /// ``` 245 | /// # pub fn main() { 246 | /// let thread_pool = may_future::Builder::new() 247 | /// .after_start(|| { 248 | /// println!("thread started"); 249 | /// }) 250 | /// .build(); 251 | /// # } 252 | /// ``` 253 | pub fn after_start(&mut self, f: F) -> &mut Self 254 | where 255 | F: Fn() + Send + Sync + 'static, 256 | { 257 | self.threadpool_builder.after_start(f); 258 | self 259 | } 260 | 261 | /// Execute function `f` before each thread stops. 262 | /// 263 | /// This is intended for bookkeeping and monitoring use cases. 264 | /// 265 | /// # Examples 266 | /// 267 | /// ``` 268 | /// # pub fn main() { 269 | /// let thread_pool = may_future::Builder::new() 270 | /// .before_stop(|| { 271 | /// println!("thread stopping"); 272 | /// }) 273 | /// .build(); 274 | /// # } 275 | /// ``` 276 | pub fn before_stop(&mut self, f: F) -> &mut Self 277 | where 278 | F: Fn() + Send + Sync + 'static, 279 | { 280 | self.threadpool_builder.before_stop(f); 281 | self 282 | } 283 | 284 | /// Create the configured `Runtime`. 285 | /// 286 | /// The returned `ThreadPool` instance is ready to spawn tasks. 287 | /// 288 | /// # Examples 289 | /// 290 | /// ``` 291 | /// # use may_future::Builder; 292 | /// # pub fn main() { 293 | /// let runtime = Builder::new().build().unwrap(); 294 | /// // ... call runtime.run(...) 295 | /// # let _ = runtime; 296 | /// # } 297 | /// ``` 298 | pub fn build(&mut self) -> io::Result { 299 | // TODO(stjepang): Once we remove the `threadpool_builder` method, remove this line too. 300 | self.threadpool_builder.pool_size(self.core_threads); 301 | 302 | let mut reactor_handles = Vec::new(); 303 | let mut timer_handles = Vec::new(); 304 | let mut timers = Vec::new(); 305 | 306 | for _ in 0..self.core_threads { 307 | // Create a new reactor. 308 | let reactor = Reactor::new()?; 309 | reactor_handles.push(reactor.handle()); 310 | 311 | // Create a new timer. 312 | let timer = Timer::new_with_now(reactor, self.clock.clone()); 313 | timer_handles.push(timer.handle()); 314 | timers.push(Mutex::new(Some(timer))); 315 | } 316 | 317 | // Get a handle to the clock for the runtime. 318 | let clock = self.clock.clone(); 319 | 320 | // Get the current trace dispatcher. 321 | // TODO(eliza): when `tokio-trace-core` is stable enough to take a 322 | // public API dependency, we should allow users to set a custom 323 | // subscriber for the runtime. 324 | let dispatch = trace::dispatcher::get_default(trace::Dispatch::clone); 325 | let trace = dispatch.clone(); 326 | 327 | let background = background::spawn(&clock)?; 328 | 329 | let pool = self 330 | .threadpool_builder 331 | .around_worker(move |w| { 332 | let index = w.id().to_usize(); 333 | 334 | tokio_reactor::with_default(&reactor_handles[index], || { 335 | clock::with_default(&clock, || { 336 | timer::with_default(&timer_handles[index], || { 337 | trace::dispatcher::with_default(&dispatch, || { 338 | w.run(); 339 | }) 340 | }); 341 | }) 342 | }); 343 | }) 344 | .custom_park(move |worker_id| { 345 | let index = worker_id.to_usize(); 346 | 347 | timers[index].lock().unwrap().take().unwrap() 348 | }) 349 | .build(); 350 | 351 | Ok(Runtime { 352 | inner: Some(Inner { 353 | pool, 354 | background, 355 | trace, 356 | }), 357 | }) 358 | } 359 | } 360 | -------------------------------------------------------------------------------- /src/enter.rs: -------------------------------------------------------------------------------- 1 | use super::park::Parker; 2 | use tokio_executor::park::Park; 3 | 4 | use std::future::Future; 5 | use std::pin::Pin; 6 | use std::task::Context; 7 | use std::task::Poll::Ready; 8 | 9 | /// Blocks the coroutine on the specified future, returning the value with 10 | /// which that future completes. 11 | pub fn block_on(mut f: F) -> F::Output { 12 | let mut park = Parker::new(); 13 | let waker = park.unpark().into_waker(); 14 | let mut cx = Context::from_waker(&waker); 15 | 16 | // `block_on` takes ownership of `f`. Once it is pinned here, the original `f` binding can 17 | // no longer be accessed, making the pinning safe. 18 | let mut f = unsafe { Pin::new_unchecked(&mut f) }; 19 | 20 | loop { 21 | if let Ready(v) = f.as_mut().poll(&mut cx) { 22 | return v; 23 | } 24 | if park.park().is_err() { 25 | may::coroutine::trigger_cancel_panic(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(async_await)] 2 | 3 | mod background; 4 | mod builder; 5 | mod enter; 6 | mod park; 7 | mod task_executor; 8 | 9 | pub use self::builder::Builder; 10 | pub use self::task_executor::TaskExecutor; 11 | 12 | use self::background::Background; 13 | 14 | use tracing_core as trace; 15 | 16 | use std::cell::Cell; 17 | use std::future::Future; 18 | use std::io; 19 | 20 | lazy_static::lazy_static! { 21 | /// global may_future runtime 22 | pub static ref RT: Runtime = Runtime::new().expect("failed to build may_future runtime"); 23 | } 24 | 25 | thread_local! { 26 | /// Tracks if the runtime start, each thread would keep a never return co 27 | /// to init the tokio run time, this is a hack to work around the TLS context 28 | static CURRENT_RUNTIME: Cell = Cell::new(false) 29 | } 30 | 31 | /// Handle to the may_future runtime. 32 | /// 33 | /// The may_future runtime includes a reactor as well as an executor for running 34 | /// tasks. 35 | /// 36 | /// 37 | /// See [module level][mod] documentation for more details. 38 | /// 39 | /// [mod]: index.html 40 | /// [`new`]: #method.new 41 | /// [`Builder`]: struct.Builder.html 42 | #[derive(Debug)] 43 | pub struct Runtime { 44 | inner: Option, 45 | } 46 | 47 | #[derive(Debug)] 48 | struct Inner { 49 | /// Task execution pool. 50 | pool: tokio_threadpool::ThreadPool, 51 | 52 | /// Tracing dispatcher 53 | trace: trace::Dispatch, 54 | 55 | /// Maintains a reactor and timer that are always running on a background 56 | /// thread. This is to support `runtime.block_on` w/o requiring the future 57 | /// to be `Send`. 58 | /// 59 | /// A dedicated background thread is required as the threadpool threads 60 | /// might not be running. However, this is a temporary work around. 61 | /// 62 | /// TODO: Delete this 63 | background: Background, 64 | } 65 | 66 | // ===== impl Runtime ===== 67 | 68 | impl Runtime { 69 | /// Create a new runtime instance with default configuration values. 70 | /// 71 | /// This results in a reactor, thread pool, and timer being initialized. The 72 | /// thread pool will not spawn any worker threads until it needs to, i.e. 73 | /// tasks are scheduled to run. 74 | /// 75 | /// Most users will not need to call this function directly, instead they 76 | /// will use [`tokio::run`](fn.run.html). 77 | /// 78 | /// See [module level][mod] documentation for more details. 79 | /// 80 | /// # Examples 81 | /// 82 | /// Creating a new `Runtime` with default configuration values. 83 | /// 84 | /// ``` 85 | /// use may_future::Runtime; 86 | /// 87 | /// let rt = Runtime::new() 88 | /// .unwrap(); 89 | /// 90 | /// // Use the runtime... 91 | /// 92 | /// // Shutdown the runtime 93 | /// rt.shutdown_now(); 94 | /// ``` 95 | /// 96 | /// [mod]: index.html 97 | pub fn new() -> io::Result { 98 | Builder::new().build() 99 | } 100 | 101 | /// Return a handle to the runtime's executor. 102 | /// 103 | /// The returned handle can be used to spawn tasks that run on this runtime. 104 | /// 105 | /// # Examples 106 | /// 107 | /// ``` 108 | /// use may_future::Runtime; 109 | /// 110 | /// let rt = Runtime::new() 111 | /// .unwrap(); 112 | /// 113 | /// let executor_handle = rt.executor(); 114 | /// 115 | /// // use `executor_handle` 116 | /// ``` 117 | pub fn executor(&self) -> TaskExecutor { 118 | let inner = self.inner().pool.sender().clone(); 119 | TaskExecutor { inner } 120 | } 121 | 122 | /// Spawn a future onto the may_future runtime. 123 | /// 124 | /// This spawns the given future onto the runtime's executor, usually a 125 | /// thread pool. The thread pool is then responsible for polling the future 126 | /// until it completes. 127 | /// 128 | /// See [module level][mod] documentation for more details. 129 | /// 130 | /// [mod]: index.html 131 | /// 132 | /// # Examples 133 | /// 134 | /// ```rust 135 | /// #![feature(async_await)] 136 | /// use may_future::Runtime; 137 | /// 138 | /// # fn dox() { 139 | /// // Create the runtime 140 | /// let rt = Runtime::new().unwrap(); 141 | /// 142 | /// // Spawn a future onto the runtime 143 | /// rt.spawn(async { 144 | /// println!("now running on a worker thread"); 145 | /// }); 146 | /// # } 147 | /// # pub fn main() {} 148 | /// ``` 149 | /// 150 | /// # Panics 151 | /// 152 | /// This function panics if the spawn fails. Failure occurs if the executor 153 | /// is currently at capacity and is unable to spawn a new future. 154 | pub fn spawn(&self, future: F) -> &Self 155 | where 156 | F: Future + Send + 'static, 157 | { 158 | self.inner().pool.spawn(future); 159 | self 160 | } 161 | 162 | /// Run a future to completion on the may_future runtime. 163 | /// 164 | /// This runs the given future on the runtime, blocking until it is 165 | /// complete, and yielding its resolved result. Any tasks or timers which 166 | /// the future spawns internally will be executed on the runtime. 167 | /// 168 | /// This method should not be called from an asynchronous context. 169 | /// 170 | /// # Panics 171 | /// 172 | /// This function panics if the executor is at capacity, if the provided 173 | /// future panics, or if called within an asynchronous execution context. 174 | pub fn block_on(&self, future: F) -> F::Output 175 | where 176 | F: Future + Send + 'static, 177 | F::Output: Send + 'static, 178 | { 179 | let blocker = co_waiter::Waiter::new(); 180 | 181 | let blocker_ref: &'static co_waiter::Waiter<_> = unsafe { std::mem::transmute(&blocker) }; 182 | 183 | self.spawn(async move { 184 | let res = future.await; 185 | blocker_ref.set_rsp(res); 186 | }); 187 | 188 | blocker.wait_rsp(None).expect("failed to wait result") 189 | } 190 | 191 | /// Run a future to completion on local thread/coroutine. 192 | /// 193 | /// This runs the given future on the local thead/coroutine, 194 | /// blocking until it is complete, and yielding its resolved 195 | /// result. Any tasks or timers which the future spawns 196 | /// internally will be executed on the runtime. 197 | /// 198 | /// This method should not be called from an asynchronous context. 199 | /// 200 | /// This method is slower than the `block_on` method, it evolves 201 | /// more works for the coroutine scheduling. But this method doesn't 202 | /// requre the futrue be `Send + 'static` 203 | /// 204 | /// # Panics 205 | /// 206 | /// This function panics if the executor is at capacity, if the provided 207 | /// future panics, or if called within an asynchronous execution context. 208 | pub fn block_on_local(&self, future: F) -> F::Output 209 | where 210 | F: Future, 211 | { 212 | CURRENT_RUNTIME.with(|b_init| { 213 | if !b_init.get() { 214 | let builder = may::coroutine::Builder::new().stack_size(0x400); 215 | let clo = || { 216 | let bg = &RT.inner().background; 217 | let trace = &RT.inner().trace; 218 | tokio_executor::with_default(&mut RT.inner().pool.sender(), || { 219 | tokio_reactor::with_default(bg.reactor(), || { 220 | tokio_timer::with_default(bg.timer(), || { 221 | trace::dispatcher::with_default(trace, || { 222 | enter::block_on(async { 223 | // this future would never return 224 | // and all default runtime are set 225 | // properly for `enter::block_on` 226 | let (_tx, rx) = tokio_sync::oneshot::channel::<()>(); 227 | rx.await.ok(); 228 | }) 229 | }) 230 | }) 231 | }) 232 | }); 233 | }; 234 | unsafe { 235 | // here we use the spawn_local to run 236 | // the coroutine in current thread. 237 | builder.spawn_local(clo).ok(); 238 | } 239 | b_init.set(true); 240 | } 241 | }); 242 | 243 | enter::block_on(future) 244 | } 245 | 246 | /// Signals the runtime to shutdown once it becomes idle. 247 | /// 248 | /// Returns a future that completes once the shutdown operation has 249 | /// completed. 250 | /// 251 | /// This function can be used to perform a graceful shutdown of the runtime. 252 | /// 253 | /// The runtime enters an idle state once **all** of the following occur. 254 | /// 255 | /// * The thread pool has no tasks to execute, i.e., all tasks that were 256 | /// spawned have completed. 257 | /// * The reactor is not managing any I/O resources. 258 | /// 259 | /// See [module level][mod] documentation for more details. 260 | /// 261 | /// # Examples 262 | /// 263 | /// ``` 264 | /// use may_future::Runtime; 265 | /// 266 | /// let rt = Runtime::new() 267 | /// .unwrap(); 268 | /// 269 | /// // Use the runtime... 270 | /// 271 | /// // Shutdown the runtime 272 | /// let _fut = rt.shutdown_on_idle(); 273 | /// ``` 274 | /// 275 | /// [mod]: index.html 276 | pub async fn shutdown_on_idle(mut self) { 277 | let inner = self.inner.take().unwrap(); 278 | let inner = inner.pool.shutdown_on_idle(); 279 | 280 | inner.await; 281 | } 282 | 283 | /// Signals the runtime to shutdown immediately. 284 | /// 285 | /// Returns a future that completes once the shutdown operation has 286 | /// completed. 287 | /// 288 | /// This function will forcibly shutdown the runtime, causing any 289 | /// in-progress work to become canceled. The shutdown steps are: 290 | /// 291 | /// * Drain any scheduled work queues. 292 | /// * Drop any futures that have not yet completed. 293 | /// * Drop the reactor. 294 | /// 295 | /// Once the reactor has dropped, any outstanding I/O resources bound to 296 | /// that reactor will no longer function. Calling any method on them will 297 | /// result in an error. 298 | /// 299 | /// See [module level][mod] documentation for more details. 300 | /// 301 | /// # Examples 302 | /// 303 | /// ``` 304 | /// use may_future::Runtime; 305 | /// 306 | /// let rt = Runtime::new() 307 | /// .unwrap(); 308 | /// 309 | /// // Use the runtime... 310 | /// 311 | /// // Shutdown the runtime 312 | /// rt.shutdown_now(); 313 | /// ``` 314 | /// 315 | /// [mod]: index.html 316 | pub async fn shutdown_now(mut self) { 317 | let inner = self.inner.take().unwrap(); 318 | inner.pool.shutdown_now().await; 319 | } 320 | 321 | fn inner(&self) -> &Inner { 322 | self.inner.as_ref().unwrap() 323 | } 324 | } 325 | -------------------------------------------------------------------------------- /src/park.rs: -------------------------------------------------------------------------------- 1 | use may::coroutine::ParkError; 2 | use may::sync::Blocker; 3 | use tokio_executor::park::{Park, Unpark}; 4 | 5 | use std::sync::Arc; 6 | use std::task::{RawWaker, RawWakerVTable, Waker}; 7 | use std::time::Duration; 8 | 9 | /// Blocks the current coroutine using coroutine blocker. 10 | #[derive(Debug)] 11 | pub struct Parker { 12 | blocker: Arc, 13 | } 14 | 15 | /// Unblocks a coroutine that was blocked by `Parker`. 16 | #[derive(Clone, Debug)] 17 | pub struct Unparker { 18 | inner: Arc, 19 | } 20 | 21 | // ===== impl Parker ===== 22 | 23 | impl Parker { 24 | /// Create a new `Parker` handle for the current coroutine. 25 | pub fn new() -> Parker { 26 | Parker { 27 | blocker: Blocker::current(), 28 | } 29 | } 30 | } 31 | 32 | impl Park for Parker { 33 | type Unpark = Unparker; 34 | type Error = ParkError; 35 | 36 | fn unpark(&self) -> Self::Unpark { 37 | let inner = self.blocker.clone(); 38 | Unparker { inner } 39 | } 40 | 41 | fn park(&mut self) -> Result<(), Self::Error> { 42 | self.blocker.park(None) 43 | } 44 | 45 | fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error> { 46 | self.blocker.park(Some(duration)) 47 | } 48 | } 49 | 50 | // ===== impl Unparker ===== 51 | 52 | impl Unpark for Unparker { 53 | fn unpark(&self) { 54 | self.inner.unpark(); 55 | } 56 | } 57 | 58 | static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop); 59 | 60 | impl Unparker { 61 | pub(crate) fn into_waker(self) -> Waker { 62 | unsafe { 63 | let raw = unparker_to_raw_waker(self); 64 | Waker::from_raw(raw) 65 | } 66 | } 67 | 68 | fn into_raw(this: Unparker) -> *const () { 69 | Arc::into_raw(this.inner) as *const () 70 | } 71 | 72 | unsafe fn from_raw(ptr: *const ()) -> Unparker { 73 | Unparker { 74 | inner: Arc::from_raw(ptr as *const Blocker), 75 | } 76 | } 77 | } 78 | 79 | unsafe fn unparker_to_raw_waker(unparker: Unparker) -> RawWaker { 80 | RawWaker::new(Unparker::into_raw(unparker), &VTABLE) 81 | } 82 | 83 | unsafe fn clone(raw: *const ()) -> RawWaker { 84 | let unparker = Unparker::from_raw(raw); 85 | 86 | // Increment the ref count 87 | std::mem::forget(unparker.clone()); 88 | 89 | unparker_to_raw_waker(unparker) 90 | } 91 | 92 | unsafe fn wake(raw: *const ()) { 93 | let unparker = Unparker::from_raw(raw); 94 | unparker.unpark(); 95 | } 96 | 97 | unsafe fn wake_by_ref(raw: *const ()) { 98 | let unparker = Unparker::from_raw(raw); 99 | unparker.unpark(); 100 | 101 | // We don't actually own a reference to the unparker 102 | std::mem::forget(unparker); 103 | } 104 | 105 | unsafe fn drop(raw: *const ()) { 106 | let _ = Unparker::from_raw(raw); 107 | } 108 | -------------------------------------------------------------------------------- /src/task_executor.rs: -------------------------------------------------------------------------------- 1 | use tokio_executor::SpawnError; 2 | use tokio_threadpool::Sender; 3 | 4 | use std::future::Future; 5 | use std::pin::Pin; 6 | 7 | /// Executes futures on the runtime 8 | /// 9 | /// All futures spawned using this executor will be submitted to the associated 10 | /// Runtime's executor. This executor is usually a thread pool. 11 | /// 12 | /// For more details, see the [module level](index.html) documentation. 13 | #[derive(Debug, Clone)] 14 | pub struct TaskExecutor { 15 | pub(super) inner: Sender, 16 | } 17 | 18 | impl TaskExecutor { 19 | /// Spawn a future onto the Tokio runtime. 20 | /// 21 | /// This spawns the given future onto the runtime's executor, usually a 22 | /// thread pool. The thread pool is then responsible for polling the future 23 | /// until it completes. 24 | /// 25 | /// See [module level][mod] documentation for more details. 26 | /// 27 | /// [mod]: index.html 28 | /// 29 | /// # Examples 30 | /// 31 | /// ```rust 32 | /// #![feature(async_await)] 33 | /// use may_future::Runtime; 34 | /// 35 | /// # fn dox() { 36 | /// // Create the runtime 37 | /// let mut rt = Runtime::new().unwrap(); 38 | /// let executor = rt.executor(); 39 | /// 40 | /// // Spawn a future onto the runtime 41 | /// executor.spawn(async { 42 | /// println!("now running on a worker thread"); 43 | /// }); 44 | /// # } 45 | /// # pub fn main() {} 46 | /// ``` 47 | /// 48 | /// # Panics 49 | /// 50 | /// This function panics if the spawn fails. Failure occurs if the executor 51 | /// is currently at capacity and is unable to spawn a new future. 52 | pub fn spawn(&self, future: F) 53 | where 54 | F: Future + Send + 'static, 55 | { 56 | self.inner.spawn(future).unwrap(); 57 | } 58 | } 59 | 60 | impl tokio_executor::Executor for TaskExecutor { 61 | fn spawn( 62 | &mut self, 63 | future: Pin + Send>>, 64 | ) -> Result<(), SpawnError> { 65 | self.inner.spawn(future) 66 | } 67 | } 68 | 69 | impl tokio_executor::TypedExecutor for TaskExecutor 70 | where 71 | T: Future + Send + 'static, 72 | { 73 | fn spawn(&mut self, future: T) -> Result<(), tokio_executor::SpawnError> { 74 | tokio_executor::Executor::spawn(self, Box::pin(future)) 75 | } 76 | } 77 | --------------------------------------------------------------------------------