Skip to content

Commit

Permalink
Re-export proconio-derive by feature gate derive
Browse files Browse the repository at this point in the history
Use feature gate to re-export `proconio-derive`.  This enables us both
to use `proconio-derive` easily and to reduce compile time when it is
unnecessary.

Fixes #3.
  • Loading branch information
statiolake committed Sep 3, 2019
1 parent 3026fc4 commit 8583b29
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
16 changes: 15 additions & 1 deletion proconio/Cargo.toml
Expand Up @@ -12,9 +12,23 @@ description = "Easy IO library for competitive programming"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[test]]
name = "derive"
path = "tests/derive.rs"
required-features = ["derive"]

[[test]]
name = "fastout"
path = "tests/fastout.rs"
required-features = ["derive"]

[dependencies]
lazy_static = "1.3.0"

[dev-dependencies.proconio-derive]
[dependencies.proconio-derive]
version = "0.1.0"
path = "../proconio-derive"
optional = true

[features]
derive = ["proconio-derive"]
45 changes: 31 additions & 14 deletions proconio/src/lib.rs
Expand Up @@ -236,11 +236,12 @@
//! `Usize1` has type `usize` as real struct.
//!
//! ```
//! # #[cfg(feature = "derive")]
//! # {
//! # extern crate proconio;
//! # extern crate proconio_derive;
//! use proconio::input;
//! # use proconio::source::auto::AutoSource;
//! use proconio_derive::derive_readable;
//! use proconio::derive_readable;
//!
//! // Unit struct can derive readable. This generates a no-op for the reading. Not ignoring
//! // the read value, but simply skip reading process. You cannot use it to discard the input.
Expand Down Expand Up @@ -274,23 +275,27 @@
//! assert_eq!(edge.weight, Weight);
//! assert_eq!(edge.cost, Cost(35));
//! }
//! # }
//! ```
//!
//! # `#[fastout]`
//!
//! If you import `proconio_derive::fastout`, you can use `#[fastout]` attribute. Adding this
//! attribute to your `main()`, your `print!` and `println!` become faster.
//! If you import `proconio::fastout`, you can use `#[fastout]` attribute. Adding this attribute
//! to your `main()`, your `print!` and `println!` become faster.
//!
//! ```
//! # extern crate proconio_derive;
//! use proconio_derive::fastout;
//! # #[cfg(feature = "derive")]
//! # {
//! # extern crate proconio;
//! use proconio::fastout;
//!
//! #[fastout]
//! fn main() {
//! print!("{}{}, ", 'h', "ello"); // "hello" (no newline)
//! println!("{}!", "world"); // "world!\n"
//! println!("{}", 123456789); // "123456789\n"
//! }
//! # }
//! ```
//!
//! ## Closures having `print!` or `println!` in `#[fastout]` function
Expand All @@ -312,7 +317,7 @@
//! Consider you want to run this code:
//!
//! ```compile_fail
//! use proconio_derive::fastout;
//! use proconio::fastout;
//!
//! #[fastout]
//! fn main() {
Expand Down Expand Up @@ -350,7 +355,9 @@
//! from the thread.
//!
//! ```
//! use proconio_derive::fastout;
//! # #[cfg(feature = "derive")]
//! # {
//! use proconio::fastout;
//!
//! #[fastout]
//! fn main() {
Expand All @@ -363,13 +370,14 @@
//! # assert_eq!(y, 9);
//! println!("{}", y);
//! }
//! # }
//! ```
//!
//! If you are doing so complex job that it's too difficult to returning the results from your
//! closure...
//!
//! ```compile_fail
//! use proconio_derive::fastout;
//! use proconio::fastout;
//!
//! # fn some_function(_: String) -> impl Iterator<Item = String> { vec!["hello".to_string(), "world".to_string()].into_iter() }
//! # fn some_proc(x: &str) -> &str { x }
Expand All @@ -396,7 +404,9 @@
//! ...you can use a function instead.
//!
//! ```
//! use proconio_derive::fastout;
//! # #[cfg(feature = "derive")]
//! # {
//! use proconio::fastout;
//!
//! # fn some_function(_: String) -> impl Iterator<Item = String> { vec!["hello".to_string(), "world".to_string()].into_iter() }
//! # fn some_proc(x: &str) -> &str { x }
Expand All @@ -420,15 +430,16 @@
//! let thread = std::thread::spawn(move || process(context));
//! thread.join().unwrap();
//! }
//! # }
//! ```
//!
//! **Important Note:** If you *spawn a new thread* which runs another function annotated with
//! `#[fastout]`, you must not add `#[fastout]` to the caller. If you add `#[fastout]` in caller
//! too, then the caller has the lock for the stdout, and so callee cannot acquire the lock forever
//! --- deadlock. This is not the case when the caller and callee is executed in the same thread,
//! since the lock of stdout is reentrant. We cannot warn about this kind of deadlock since we don't
//! know annotations attached to the function to be called. (In the above example, we can't know
//! whether the function `process()` has `#[fastout]` attribute or not.)
//! since the lock of stdout is reentrant. We cannot warn about this kind of deadlock since we
//! don't know annotations attached to the function to be called. (In the above example, we can't
//! know whether the function `process()` has `#[fastout]` attribute or not.)
//!
//! If your code is so complex that you cannot avoid deadlock, you should give up using
//! `#[fastout]` and simply use `println!` or manually handle your stdout in usual Rust way.
Expand All @@ -439,14 +450,17 @@
//! two prints in main, the order of printing may differ. In other words, the below example
//!
//! ```
//! # use proconio_derive::fastout;
//! # #[cfg(feature = "derive")]
//! # {
//! # use proconio::fastout;
//! fn foo() { println!("between"); }
//! #[fastout]
//! fn main() {
//! println!("hello");
//! foo();
//! println!("world");
//! }
//! # }
//! ```
//!
//! *likely* prints like
Expand All @@ -460,6 +474,9 @@
//! If you don't like this behavior, you can remove #[fastout] from your `main()`.
//!

#[cfg(feature = "derive")]
pub use proconio_derive::*;

pub mod marker;
pub mod read;
pub mod source;
Expand Down
2 changes: 1 addition & 1 deletion proconio/tests/derive.rs
Expand Up @@ -5,9 +5,9 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be copied, modified, or
// distributed except according to those terms.

use proconio::derive_readable;
use proconio::input;
use proconio::source::auto::AutoSource;
use proconio_derive::derive_readable;

#[derive_readable]
#[derive(PartialEq, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion proconio/tests/fastout.rs
Expand Up @@ -5,7 +5,7 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be copied, modified, or
// distributed except according to those terms.

use proconio_derive::fastout;
use proconio::fastout;

#[fastout]
fn foo() -> i32 {
Expand Down

0 comments on commit 8583b29

Please sign in to comment.