diff --git a/src/lib.rs b/src/lib.rs index a4b209c..e953560 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,7 +94,7 @@ static RT: Lazy = Lazy::new(|| { // Runs the future to completion on a new worker (have Mutex>) // there will be no hidden threadpool!! -pub fn run(future: F) -> JoinHandle +pub fn run(future: F) -> Spawn where F: Future + Send + 'static, R: Send + 'static, @@ -123,7 +123,7 @@ where type Task = async_task::Task<()>; /// Spawns a future on the executor. -pub fn spawn(future: F) -> JoinHandle +pub fn spawn(future: F) -> Spawn where F: Future + Send + 'static, R: Send + 'static, @@ -133,13 +133,13 @@ where task.schedule(); // Return a join handle that retrieves the output of the future. - JoinHandle(handle) + Spawn(handle) } /// Awaits the output of a spawned future. -pub struct JoinHandle(async_task::JoinHandle); +pub struct Spawn(async_task::JoinHandle); -impl Future for JoinHandle { +impl Future for Spawn { type Output = R; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { @@ -150,6 +150,16 @@ impl Future for JoinHandle { } } +// ----- Blocking ----- + +pub fn blocking(future: F) -> Spawn +where + F: Future + Send + 'static, + R: Send + 'static, +{ + todo!() +} + // ----- Timer ----- pub fn timer(dur: Duration) -> Timer { @@ -217,12 +227,9 @@ pub struct Registration { pub struct Async(Arc>); -impl Async { +impl Async { // note: make sure source is in non-blocking mode - pub fn register(source: T) -> Async - where - T: AsRawFd, - { + pub fn register(source: T) -> Async { let mut entries = RT.entries.lock().unwrap(); let vacant = entries.vacant_entry(); let index = vacant.key(); @@ -250,7 +257,9 @@ impl Async { let fd = source.as_raw_fd(); Async(Arc::new(Registration { fd, source, entry })) } +} +impl Async { /// Gets a reference to the source I/O handle. pub fn source(&self) -> &T { &self.0.source @@ -275,7 +284,7 @@ impl Async { fn poll_with<'a, R>( &'a self, cx: &mut Context<'_>, - m: &Mutex>, + wakers: &Mutex>, mut f: impl FnMut(&'a T) -> io::Result, ) -> Poll> { // Attempt the non-blocking operation. @@ -285,7 +294,7 @@ impl Async { } // Acquire a lock on the waker list. - let mut wakers = m.lock().unwrap(); + let mut wakers = wakers.lock().unwrap(); // Attempt the non-blocking operation again. match f(self.source()) { @@ -355,6 +364,7 @@ impl Async { let mut last_err = None; // Try connecting to each address one by one. + // TODO: use blocking pool to resolve for addr in addr.to_socket_addrs()? { match Self::connect_to(addr).await { Ok(stream) => return Ok(stream),