These are chat archives for rust-lang/rust

28th
Aug 2017
Michal 'vorner' Vaner
@vorner
Aug 28 2017 08:03

Hello. I have a workspace with multiple crates. Some of the crates have a feature that add some auxiliary functions for unit tests (eg. creation of dummy values). I want the feature to be used only when some other crate is actually compiled in the test mode. So I tried this in the Cargo.toml:

[dependencies]
# Our own
libdata = { path = "../libdata" }

[dev-dependencies]
libdata = { path = "../libdata", features = ["test-access"] }

However, it turns the feature on all the time. Is there a better way than what I do?

Aleksey Kladov
@matklad
Aug 28 2017 08:04

However, it turns the feature on all the time.

Yep, this is how it is supposed to work. In theory, Cargo features are purely additive, so Cargo always enables the union of the requested features.

Michal 'vorner' Vaner
@vorner
Aug 28 2017 08:05
Well, but if I do cargo build, then the dev-dependencies should not come into play, should they?
Aleksey Kladov
@matklad
Aug 28 2017 08:06

Some of the crates have a feature that add some auxiliary functions for unit tests (eg. creation of dummy values)

Not sure if that'll work, but perhaps this should not be a feature, but rather a cfg[test].

Well, but if I do cargo build, then the dev-dependencies should not come into play, should they?

Well, if you share deps, you have less stuff to compile, so it's not a clear case

Michal 'vorner' Vaner
@vorner
Aug 28 2017 08:07
cfg(test) doesn't work. It is on when the crate itself is being compiled for tests. But when it is being used as a dependency, it is compiled once more, without tests.
My main motivation is, if I do the final build, I don't want to allow the real code to use the test access functions, because they allow to mess too much with what lives inside the data structures (eg. they break encapsulation).
Aleksey Kladov
@matklad
Aug 28 2017 08:09
And I presume this is also a reason why you can't separate test helpers into a separate crate, right?
Michal 'vorner' Vaner
@vorner
Aug 28 2017 08:10
Right. They would not be able to create the values I need.
Aleksey Kladov
@matklad
Aug 28 2017 08:10
There's an RFC which can help here: rust-lang/rfcs#1956
I think it would be useful if you add your use-case as a comment.
But in general situation with features and profiles in Cargo is ... suboptimal :(
Michal 'vorner' Vaner
@vorner
Aug 28 2017 08:12
From the first glimpse, this could be used as kind of a workaround, but it's not exactly that. But I'll read it thoroughly soon and maybe comment.
Aleksey Kladov
@matklad
Aug 28 2017 08:15

It contains the following example:

[dependencies.foobar.features]
test = ["alpha"]

which does seems like what we want here

Michal 'vorner' Vaner
@vorner
Aug 28 2017 08:32
Aren't profiles release vs. dev? Because I still want to have the feature enabled in cargo test --release and not in cargo build (without --release)
Aleksey Kladov
@matklad
Aug 28 2017 08:35
Profiles are complicated. There are dev, release, test, bench, doc, and the rules which choose which profile is enabled are messy .
Michal 'vorner' Vaner
@vorner
Aug 28 2017 08:35
Hmm. Maybe I should just think about how to get rid of these auxiliary functions.
Aleksey Kladov
@matklad
Aug 28 2017 08:36
Like, cargo test is a mixture of dev and test, and cargo test --release is actually bench =/
vorner @vorner probably doesn't want to know more O:-)
David DeSimone
@DavidDeSimone
Aug 28 2017 10:48
will Rust attempt to run Drop on allocations on termination? I.e. if I have Box<HashMap>, and my program terminates, can Rust run Drop on it somehow?
Michal 'vorner' Vaner
@vorner
Aug 28 2017 10:49
@DavidDeSimone That depends on how you terminate and where the Box<HashMap> lives. If you terminate by exiting from main and you have it on the stack somewhere, then yes. If you run process::terminate, then no.
David DeSimone
@DavidDeSimone
Aug 28 2017 10:50
@vorner that's for clarifying that :)
Zakarum
@omni-viral
Aug 28 2017 11:45
How to match lifetime parameter in macro?
red75prime
@red75prime
Aug 28 2017 12:07
$lifetime:tt doesn't work?
lemonxah
@lemonxah
Aug 28 2017 12:07
any production ready restfull api frameworks that you guys think are worth looking into?
Zakarum
@omni-viral
Aug 28 2017 12:10
@red75prime $lifetime:tttries to capture everything. I need to match only lifetime
Another question. How to make repeated capture with + as separator?
red75prime
@red75prime
Aug 28 2017 12:21
It is impossible. From docs on macros:
Both forms optionally include a separator, which can be any token except + or *.
Zakarum
@omni-viral
Aug 28 2017 12:28
How to match trait bounds than?
They are sequence of traits and lifetimes separated by +
red75prime
@red75prime
Aug 28 2017 12:41
Rust's macro parser are pretty limited. You need to use another separator. Something like that
Restioson
@Restioson
Aug 28 2017 15:51
@omni-viral how about \+? does that compile as a sep?
nope, nvm :c
Zakarum
@omni-viral
Aug 28 2017 15:52
@Restioson :smile:
Restioson
@Restioson
Aug 28 2017 15:57
@red75prime - what about matching '$lifetime:ident?
Restioson
@Restioson
Aug 28 2017 16:02
hm, no. What fails with :tt?
Zakarum
@omni-viral
Aug 28 2017 16:02
:tt is ambiguous
Restioson
@Restioson
Aug 28 2017 16:03
it'll error when used
if you just do hi there, when you sub in the syntax error will be caught (hi isnt valid)
(sorry red; thought it was your question)
Kyle Nosar
@nosark
Aug 28 2017 16:51
This message was deleted