poee 0.1.0

Functions for efficient development
Documentation
pub use poee::spawn;
pub use poee::spawn_blocking;
pub use poee::sync_fn;
#[allow(warnings)]
pub mod poee{
    use std::future::Future;
    use tokio::runtime::Runtime;
    use tokio::task::{JoinHandle, JoinError};
    /// ### Spawns a new asynchronous task, returning a JoinHandle for it.
    ///```rust
    ///   #[tokio::main]
    ///   async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///       poee::spawn(||{
    ///           std::fs::write("demo0", "rust").unwrap();
    ///       });
    ///       fn demo(){
    ///           std::fs::write("demo1", "rust").unwrap();
    ///       }
    ///       poee::spawn(demo);
    ///   
    ///       Ok(())
    ///    }
    ///```
    pub fn spawn<T:FnOnce() -> ()+ std::marker::Send + 'static>(function: T) -> JoinHandle<T::Output>
    {
        let task = tokio::spawn(async move {
            function();
        });
        task
    }
    /// ### Runs the provided closure on a thread where blocking is acceptable.
    /// ```rust
    ///  #[tokio::main]
    ///  async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///      poee::spawn_blocking(||{
    ///          std::fs::write("demo0", "rust").unwrap();
    ///      });
    ///      fn demo(){
    ///          std::fs::write("demo1", "rust").unwrap();
    ///      }
    ///      poee::spawn_blocking(demo);
    ///  
    ///      Ok(())
    ///  }    
    /// ```
    pub fn spawn_blocking<T:FnOnce() -> ()+ std::marker::Send + 'static>(function: T) -> JoinHandle<T::Output>
    {
        let blocking_task = tokio::task::spawn_blocking(move || {
            function();
        });
        blocking_task
    }
    /// ### executed on the async code in sync
    /// ```rust
    /// fn main(){
    ///    async fn demo(){
    ///        println!("demo");
    ///    }
    ///    poee::sync_fn(demo());
    ///}
    /// ```
    pub fn sync_fn<T:Future+ std::marker::Send + 'static>(function: T){
        // Create a new Tokio runtime
        let rt = Runtime::new().unwrap();
        // Run an async function in the runtime
        rt.block_on(async {
            function.await;
        });
    }
}