Skip to content

Commit

Permalink
also time last stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kardeiz committed Nov 21, 2019
1 parent c5f9b33 commit d8bc6a1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "funtime"
version = "0.2.1"
version = "0.3.0"
authors = ["Jacob Brown <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ funtime start: `foo`
took 5µs: `let d = 1_000 ;`
took 2µs: `x += d ;`
took 2µs: `x += y ;`
took 3µs: `x`
funtime end: `foo` took 12µs
funtime start: `main`
funtime end: `main` took 36µs
took 49µs: `foo (23) ;`
funtime end: `main` took 56µs
```

Current version: 0.2.0
Current version: 0.3.0
41 changes: 30 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ pub fn timed(_attrs: TokenStream, item: TokenStream) -> TokenStream {
}

fn rewrite_stmts(name: String, stmts: &mut Vec<Stmt>) -> Vec<Stmt> {

fn truncate_stmt(stmt: &Stmt, len: usize) -> String {
let short =
format!("{}", quote::ToTokens::to_token_stream(stmt)).chars().collect::<Vec<_>>();

let short = if short.len() > len {
let mut short = short[..(len - 3)].into_iter().collect::<String>();
short.push_str("...");
short
} else {
short.into_iter().collect::<String>()
};

short
}

let setup: Block = parse_quote! {{
struct FuntimeTimer {
start: std::time::Instant,
Expand Down Expand Up @@ -79,16 +95,7 @@ fn rewrite_stmts(name: String, stmts: &mut Vec<Stmt>) -> Vec<Stmt> {
let last = stmts.pop();

for stmt in stmts.drain(..) {
let short =
format!("{}", quote::ToTokens::to_token_stream(&stmt)).chars().collect::<Vec<_>>();

let short = if short.len() > 40 {
let mut short = short[..37].into_iter().collect::<String>();
short.push_str("...");
short
} else {
short.into_iter().collect::<String>()
};
let short = truncate_stmt(&stmt, 40);

let next_stmt = parse_quote!(funtime_timer.mark_elapsed(#short););

Expand All @@ -97,7 +104,19 @@ fn rewrite_stmts(name: String, stmts: &mut Vec<Stmt>) -> Vec<Stmt> {
}

if let Some(stmt) = last {
new_stmts.push(stmt);
let short = truncate_stmt(&stmt, 40);
let new_stmt = parse_quote! {
let funtime_return_val = {
#stmt
};
};

let next_stmt = parse_quote!(funtime_timer.mark_elapsed(#short););
let return_stmt = parse_quote!(return funtime_return_val;);

new_stmts.push(new_stmt);
new_stmts.push(next_stmt);
new_stmts.push(return_stmt);
}

new_stmts
Expand Down

0 comments on commit d8bc6a1

Please sign in to comment.