Commit 4b8e5496 authored by Raphaël Gomès's avatar Raphaël Gomès
Browse files

Initial commit

parents
^target/
^.idea/
\ No newline at end of file
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "log"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "micro-timer"
version = "0.1.0"
dependencies = [
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "proc-macro2"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "unicode-xid"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
"checksum proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6c09721c6781493a2a492a96b5a5bf19b65917fe6728884e7c44dd0c60ca3435"
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
"checksum syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "123bd9499cfb380418d509322d7a6d52e5315f064fe4b3ad18a53d6b92c07859"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
[package]
name = "micro-timer"
version = "0.1.0"
authors = ["Raphaël Gomès <rgomes@octobus.net>"]
edition = "2018"
[lib]
proc-macro = true
[dependencies]
syn = {version = "1.0.16", features = ["full", "extra-traits"]}
quote = "1.0.2"
log = "0.4.8"
\ No newline at end of file
max_width = 79
wrap_comments = true
error_on_line_overflow = true
// micro-timer
//
// Copyright 2020, Raphaël Gomès <rgomes@octobus.net>
/// `extern crate` Required even for 2018 edition
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
type StaticResult<T> = ::std::result::Result<T, &'static str>;
#[proc_macro_attribute]
pub fn timed(_attr_ts: TokenStream, fn_ts: TokenStream) -> TokenStream {
let ast = syn::parse(fn_ts.clone()).unwrap();
let func = parse_function(ast).unwrap();
let mut outer = func.clone();
outer.sig.ident = func.sig.ident.to_owned();
let original_func_name = func.sig.ident.to_string();
let inner_block = func.block;
let block = quote! {
{
let timer = ::std::time::Instant::now();
let mut inner = || { #inner_block };
let ret = inner();
log::trace!(
"Elapsed {}: {:?}",
#original_func_name,
timer.elapsed()
);
ret
}
};
// `quote` works with `proc_macro2::TokenStream`, not
// `proc_macro::TokenStream`... thankfully you can just `.into()` them
outer.block = syn::parse(block.into()).unwrap();
(quote! {#outer}).into()
}
fn parse_function(item: syn::Item) -> StaticResult<syn::ItemFn> {
match item {
syn::Item::Fn(func) => Ok(func),
_ => Err("`time_it` attribute can only be used on functions"),
}
}
use micro_timer::timed;
use std::time::Duration;
#[test]
fn check() {
#[timed]
fn thing(_value: usize) -> usize {
let timer = 10;
std::thread::sleep(Duration::from_millis(10));
timer
}
assert_eq!(thing(1), 10 as usize);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment