Is there a way of resolving a crate naming conflict? [duplicate] - rust

I have a crate foo_sys. In Rust 2015 I used extern crate foo_sys as foo for convenience, but in Rust 2018 extern crate isn't needed anymore and I don't want to use it only for aliasing. When dropping extern crate, I get
error[E0463]: can't find crate for foo

This can be achieved with the rename-dependency Cargo feature, available in Rust 1.31. With this feature, it's possible to provide a package attribute to the dependencies:
The rename-dependency feature allows you to import a dependency with a different name from the source. This can be useful in a few scenarios:
Depending on crates with the same name from different registries.
Depending on multiple versions of a crate.
Avoid needing extern crate foo as bar in Rust source.
Instead of writing
[dependencies]
foo_sys = "0.2"
the package key can be added to the dependency in Cargo.toml:
[dependencies]
foo = { package = "foo_sys", version = "0.2" }
WARNING: Cargo prior to Rust 1.26.0 may download the wrong dependency when using this feature!

The idiomatic solution is to rename the crate in Cargo.toml. See the answer by Tim Diekmann for more information about that.
But if you don't want to use Cargo.toml renaming for some reason, you can still use the old syntax. It's soft-deprecated, but not removed. So this still works:
extern crate foo_sys as foo;
(Playground example)

Related

Enable chrono feature with r2d2_postgres crate

I'm learning Rust, and I can't seem to use the FromSql trait from the r2d2_postgres crate.
Based on this recommendation, I tried adding the feature in my TOML file:
r2d2_postgres = {version = "0.18", features = ["postgres/with-chrono-0_4"]}
But then Rust tells me:
feature `postgres/with-chrono-0_4` in dependency `r2d2_postgres` is not allowed to contain slashes
If you want to enable features of a transitive dependency, the direct dependency needs to re-export those features from the `[features]` table.
To be able to work with both r2d2 and chrono, an extra dependency on the postgres crate is needed. But the postgres crate must be the same version as the r2d2_postgres::postgress crate in order to work together.
Versions can be compared with the output of cargo tree --duplicates.
At this time of writing, Cargo.toml must contain:
[dependencies]
r2d2_postgres = "0.18"
postgres = {version = "0.19", features = ["with-chrono-0_4"]}
Thank you Caesar.

How to make infra-doc link to crate without adding crate to dependencies

Rust allows to make links in documentation in the form
/// link to structure [Foo](foo::Foo) in crate foo
To make this work the linked crate should be in dependencies section in Cargo.toml:
[dependencies]
foo = "1.0"
What to do if I want to make such link, but do not want to make my crate dependent on foo? For tests and examples this is achieved by dev-dependencies option in Cargo.toml:
[dev-dependencies]
foo = "1.0"
But this doesn't work for building documentation with cargo doc.

How do I include extern functions from dependency crates?

I had some extern "C" functions in my Rust library; then I decided to split the library up into a bunch of crates and make one workspace crate that just re-exports everything from its members. However, now that I have done this, the symbols for these extern "C" functions are missing from the generated .so file. How can I fix this?
I tried re-exporting the extern "C" functions individually; this did not work. I also tried setting crate-type = ["cdylib"] on the crate containing the functions, and this didn't work either.
According to this GitHub issue, the behaviour I'm seeing is a bug and there doesn't seem to be any known solution besides wrapping the functions that I want to re-export.

Why can actix_rt be used directly in the code, even if it wasn't declared?

I just added actix_rt in Cargo.toml and didn't declare it at the first line with the use keyword. Then I could use it in the code. I know some frequently used functions are included in the prelude of Rust, but I had no idea 3rd party libraries could do the some thing. Can I create a crate like that?
Any one could tell me why or give me some tips or some reference links? I'd appreciate it.
[dependencies]
actix-rt = "0.2.5"
actix-web = "1.0.8"
use std::io;
fn main() -> io::Result<()> {
let sys = actix_rt::System::new("basic");
sys.run()
}
In the Rust 2018 Edition, extern crate is no longer required. Putting a crate as a dependency allows it to be accessed as a module. There's nothing you need to do to make your crate accessible like this.
This is very different from the standard library prelude, which uses all the items in the prelude implicitly (with use std::prelude::v1::*;). With extern crate or adding an external crate as a dependency, the types, functions and traits have to be qualified. In your example, you have to use actix_rt::System::new("basic") rather than simply System::new("basic"). Compare this to std::prelude::v1::Option, which can be used as Option<T> without any prefix.

How do I avoid import collisions when importing my own standard library?

I am trying to import my own std library, but when I am compiling with crates dependencies I have this issue:
error: duplicate lang item in crate `std`: `f32`.
|
= note: first defined in crate `my_std`.
error: duplicate lang item in crate `std`: `f64`.
|
= note: first defined in crate `my_std`.
error: duplicate lang item in crate `std`: `panic_fmt`.
|
= note: first defined in crate `my_std`.
I tried to overwrite the library by writing extern crate my_std as std
What would be the easy way to fix that - and not modify this for all dependencies?
You can compile your code without the Rust standard library by using #![no_std] attribute in your crate root. Note that there are some caveats to doing this with an executable (as opposed to a library), and you will need to manually include libc for a binary.

Resources