timer_macro 0.1.4

A macro to print time taken to execute a function
Documentation

use tokio::time::sleep;
use std::time::Duration;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sync_function() {
        // A simple synchronous function with the timer macro
        #[timer_macro::timer]
        fn sync_function(pause: u64) -> Result<(), Box<dyn std::error::Error>> {
            std::thread::sleep(std::time::Duration::from_millis(pause));
            Ok(())
        }

        // Run the sync function and check if it returns Ok(())
        assert!(sync_function(1000).is_ok());
    }

    #[tokio::test]
    async fn test_async_function() {
        // An asynchronous function with the timer macro
        #[timer_macro::timer]
        async fn async_function(pause: u64) -> Result<(), Box<dyn std::error::Error>> {
            sleep(Duration::from_millis(pause)).await;
            Ok(())
        }

        // Run the async function and check if it returns Ok(())
        assert!(async_function(1000).await.is_ok());
    }

    #[tokio::test]
    async fn test_async_function_with_error() {
        // An async function that returns an error with the timer macro
        #[timer_macro::timer]
        async fn async_function_with_error(pause: u64) -> Result<(), Box<dyn std::error::Error>> {
            sleep(Duration::from_millis(pause)).await;
            Err("An error occurred".into())
        }

        // Run the async function and check if it returns the error
        let result = async_function_with_error(1000).await;
        assert!(result.is_err());
    }
}