Skip to content

Commit

Permalink
Add alternative design as default
Browse files Browse the repository at this point in the history
From this reddit thread [1], it became clearer that /u/tsion_'s
solution is more useful. p!(x) now do the same as println!("x = {:?}",
x);

The old mode can be recovered by placing a colon before the value.

[1] https://www.reddit.com/r/rust/comments/3fxf71
  • Loading branch information
dlight committed Aug 6, 2015
1 parent 674043d commit 3616190
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 40 deletions.
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,41 @@ And use like this:
#[macro_use] extern crate p_macro;

fn main() {
p!(5); // same as println!("{:?}", 5);
let x = 5;
let y = 2;
p!(x + y); // same as println!("x + y = {:?}", x + y);
p!(x, y); // same as println!("x = {:?}, y = {:?}", x, y);
p!(); // same as println!("");
}
```

It does other things too:
It does other things too. With a colon you disable printing the source
code snippet:

```rust
p!(10, "a"); // same as println!("{:?} {:?}", 10, "a");
p!(:10, :"a"); // same as println!("{:?} {:?}", 10, "a");
```

It accepts multiple lines too:

```rust
p! {
1, 2, 3; // same as: println!("{:?} {:?} {:?}", 1, 2, 3);
4, 5, 6; // println!("{:?} {:?} {:?}", 4, 5, 6);
};
let (a, b, c) = (1, 2, 3);

p! {
a, b, c;
a + b, 0, 1;
};
// same as:
// println!("a = {:?}, b = {:?}, c = {:?}", a, b, c);
// println!("a + b = {:?}, 0 = {:?}, 1 = {:?}", a + b, 0, 1);
```

Strings are printed with quotes. If this is undesirable, prefix them
with an _. This will make the macro use `Display` instead of `Debug`.

```rust
p!(_"Test"); // same as println!("{}", "Test");
let a = "x"; p!(_ a); // if necessary insert a space
p!(_"Test"); // same as println!("{}", "Test");
let a = "x"; p!(_ a); // if necessary insert a space
```

To run the file on the examples folder, type `cargo run --example
Expand Down
16 changes: 11 additions & 5 deletions examples/print.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#[macro_use] extern crate p_macro;

#[derive(Debug)]
struct Point { x: i32, y: i32 }

fn main() {
let a = [0, 1, 2, 3, 4, 5];

let b = ["This", "is", ""];

p!(a; b);
let point = Point { x: 100, y: -50 };

p!(:a; :b);

p!();

p!(point.x, point.y + 30);

p!(_ b[0], _":", a[2] * 42);
p!(_ b[0], _"=>", a[2] * 42);

p! {
_"Never changes", _ a[0], a[0];
b[1], b[2];
"Qq";
_"The value is:", point.x + a[2];
b[0], :a[2], :a[3], :a[1]
};
}
73 changes: 47 additions & 26 deletions lib.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,72 @@
#[macro_export]
macro_rules! __p_c {
($y:expr) => { print!("{:?}", $y); }
}

#[macro_export]
macro_rules! __p_u {
($y:expr) => { print!("{}", $y); }
}

#[macro_export]
macro_rules! __p_b {
($y:expr) => { print!("{} = {:?}", stringify!($y), $y); }
}

#[macro_export]
macro_rules! p {
() => {
println!("");
};

(_ $y:expr) => {
println!("{}", $y);
};


(_ $y:expr,) => {
println!("{}", $y);


(: $y:expr) => { __p_c!($y); p!(); };
(: $y:expr,) => { __p_c!($y); p!(); };
(: $y:expr;) => { __p_c!($y); p!(); };

(: $y:expr, $($x:tt)+) => {
__p_c!($y);
print!(" ");
p!($($x)*);
};

(_ $y:expr;) => {
println!("{}", $y);

(: $y:expr; $($x:tt)+) => {
__p_c!($y);
p!();
p!($($x)*);
};


(_ $y:expr) => { __p_u!($y); p!(); };
(_ $y:expr,) => { __p_u!($y); p!(); };
(_ $y:expr;) => { __p_u!($y); p!(); };

(_ $y:expr, $($x:tt)+) => {
print!("{} ", $y);
__p_u!($y);
print!(" ");
p!($($x)*);
};

(_ $y:expr; $($x:tt)+) => {
println!("{}", $y);
__p_u!($y);
p!();
p!($($x)*);
};

($y:expr) => {
println!("{:?}", $y);
};

($y:expr,) => {
println!("{:?}", $y);
};

($y:expr;) => {
println!("{:?}", $y);
};
($y:expr) => { __p_b!($y); p!(); };
($y:expr,) => { __p_b!($y); p!(); };
($y:expr;) => { __p_b!($y); p!(); };

($y:expr, $($x:tt)+) => {
print!("{:?} ", $y);
__p_b!($y);
print!(", ");
p!($($x)*);
};

($y:expr; $($x:tt)+) => {
println!("{:?}", $y);
__p_b!($y);
p!();
p!($($x)*);
};
}

0 comments on commit 3616190

Please sign in to comment.