Why can't I import `std::assert` via `use` while it works for other macros from std? - rust

With Rust 2018, this code works (Playground):
use std::panic;
use std::format;
use std::assert_eq;
But this:
use std::assert;
Results in this error:
error[E0432]: unresolved import `std::assert`
--> src/lib.rs:4:5
|
4 | use std::assert;
| ^^^^^^^^^^^ no `assert` in the root
I read the edition guide about this topic and it says that use should work with macro_rules! macros and procedural macros. Thus, I'm confused.

use should work with macro_rules! macros and procedural macros
Except assert is neither of those:
/// Built-in macros to the compiler itself.
///
/// These macros do not have any corresponding definition with a `macro_rules!`
/// macro, but are documented here. Their implementations can be found hardcoded
/// into libsyntax itself.
It is a compiler built-in:
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_doc_only_macro]
macro_rules! assert {
($cond:expr) => ({ /* compiler built-in */ });
($cond:expr,) => ({ /* compiler built-in */ });
($cond:expr, $($arg:tt)+) => ({ /* compiler built-in */ });
}
Other faux-macros include:
compile_error
format_args
env
option_env
concat_idents
concat
line
column
file
stringify
include_str
include_bytes
module_path
cfg
include
The actual definition of assert is buried much lower in libsyntax_ext/assert.rs
Stabilize uniform paths on Rust 2018 (#56417) does mention these in passing:
Built-in macros, for example use env.
Currently an error due to some (fixable) implementation details of built-in macros.
No known issues to resolve before stabilization (after the error is removed).

Related

How to use `clippy::disallowed_method` with methods inside the same crate

I have some functions defined in my crate that I want to disallow the use of inside of the same, using clippy::disallowed_methods.
For example:
fn f() { ... }
fn g() {
f(); // Warning
}
I've tried the following clippy.toml
disallowed-methods = [
"crate::f"
]
But no warning shows up when I run cargo clippy.
I've also tried f, my_crate::f and ::my_crate::f, where my_crate is the name of the crate both functions are defined in, but none of them work.
I've tried it with other methods in external crates, such as std::vec::Vec::new and a warning successfully shows up.
Is there any way to make disallowed_methods work without moving the methods to another crate?

How to make macro import/use struct in any place it's used?

Suppose I have the following code inside a module:
struct A{
}
#[macro_export]
macro_rules! profile {
($variable_name:tt, $expression:expr) => {
let a = A{};
}
}
so it'd be in a/a.rs, to the side of a/mod.rs which exports a::A and also exports the macro.
The problem is that when I use this macro on other modules like b/b.rs, I'd have to use super::a::A before using the macro.
I could change let a = A{}; on the macro to let a = self::a::A{};. However, it'd not work on all modules, and certainly not for users of this library that use this library in their code.
How can I specify let a = something::something::A{}; in a way that it works anywhere inside my library as well for users of this lib?
In declarative macros specifically, you can use the $crate metavariable to reference the crate the macro is defined in, and then use the absolute path of the item. This will work both inside and outside of your library. For example, if your struct A is defined in your library at the path module_a::module_b::A, you would use:
struct A{
}
#[macro_export]
macro_rules! profile {
($variable_name:tt, $expression:expr) => {
let a = $crate::module_a::module_b::A{};
}
}
Here's the relevant section of the Rust reference guide.

Can't use a neon JsArray: This function takes 3 parameters but 2 were supplied

I'm learning how to use neon, but I don't understand a thing. If I try to execute this code:
#[macro_use]
extern crate neon;
use neon::vm::{Call, JsResult};
use neon::mem::Handle;
use neon::js::{JsInteger, JsNumber, JsString, JsObject, JsArray, JsValue, Object, Key};
use neon::js::error::{JsError, Kind};
fn test(call: Call) -> JsResult<JsArray> {
let scope = call.scope;
let js_arr: Handle<JsArray> = try!(try!(call.arguments.require(scope, 1)).check::<JsArray>());
js_arr.set(0, JsNumber::new(scope, 1000));
Ok(js_arr)
}
register_module!(m, {
m.export("test", test)
});
I get this error when I call js_arr.set: This function takes 3 parameters but 2 were supplied.
I don't understand why since it's a JsArray. Even Racer tells me that the set method takes 2 parameters. No matter what, js_arr.set takes 3 parameters in this order: &mut bool, neon::macro_internal::runtime::raw::Local and neon::macro_internal::runtime::raw::Local.
What's happening? I can't understand how JsArray works.
As paulsevere says on a GitHub issue for Neon, import neon::js::Object. In addition, do not import Key, which also provides a set method:
#[macro_use]
extern crate neon;
use neon::vm::{Call, JsResult};
use neon::js::{Object, JsArray, JsInteger, JsObject, JsNumber};
fn make_an_array(call: Call) -> JsResult<JsArray> {
let scope = call.scope; // the current scope for rooting handles
let array = JsArray::new(scope, 3);
array.set(0, JsInteger::new(scope, 9000))?;
array.set(1, JsObject::new(scope))?;
array.set(2, JsNumber::new(scope, 3.14159))?;
Ok(array)
}
register_module!(m, {
m.export("main", make_an_array)
});
This creates a brand new array. If you'd like to accept an array as the first argument to your function and then modify it, this works:
#[macro_use]
extern crate neon;
use neon::vm::{Call, JsResult};
use neon::js::{Object, JsArray, JsInteger, JsUndefined};
use neon::mem::Handle;
fn hello(call: Call) -> JsResult<JsUndefined> {
let scope = call.scope;
let js_arr: Handle<JsArray> = call.arguments.require(scope, 0)?.check::<JsArray>()?;
js_arr.set(0, JsInteger::new(scope, 1000))?;
Ok(JsUndefined::new())
}
register_module!(m, {
m.export("hello", hello)
});
let js_arr: Handle<JsArray> makes it clear that js_arr is a Handle<JsArray> and Handle<T> has this method:
unsafe fn set(self, out: &mut bool, obj: Local, val: Local) -> bool
I'd guess that you're accidentally trying to call Handle::set (which is unsafe and takes three non-self arguments) rather than JsArray::set (which is safe and takes two non-self arguments).
If that's the case, you need to force a deref_mut to occur. (_mut because JsArray::set takes &mut self.)
I haven't run into this sort of naming collision before, so I can't be certain whether the auto-deref is smart enough, but something like this may work:
(&mut js_arr).set(0, JsNumber::new(scope, 1000));
Failing that, two other things to try are:
JsArray::set(&mut js_arr, 0, JsNumber::new(scope, 1000));
(If the former example fails because it's too much like C++-style method overloading. This is known as Fully Qualified Syntax and is normally used to disambiguate when an object implements two traits which provide methods of the same name.)
Call js_arr.deref_mut() directly to get a mutable reference to the underlying JsArray, then call set on that.

Rust conflict with error-chain and websocket crates

I'm trying to use error-chain with the websocket crate, but running into an issue that I'm not sure how to resolve. WsUpgrade::accept has the following signature:
fn accept(self) -> Result<Client<S>, (S, IoError)>
Note that the Err variant is a tuple. I want to configure error-chain to handle calls to this method, so I tried to do so as follows in the relevant consuming module:
mod errors {
error_chain! {
foreign_links {
WebSock((::std::net::TcpStream, ::std::io::Error));
}
}
}
This results in the following error, seemingly related to the presence of the tuple.
error: expected identifier, found `(`
--> src/lib/websock.rs:23:21
|
23 | WebSock((::std::net::TcpStream, ::std::io::Error));
| ^
How can I resolve this? Hopefully I've missed something simple.
A simple solution would be to introduce a type alias:
mod errors {
type WebSocketError = (::std::net::TcpStream, ::std::io::Error);
error_chain! {
foreign_links {
WebSock(WebSocketError);
}
}
}
However, this type does not implement traits required by error-chain (such as Error and Display), so it can't be used there.
I think the simplest solution is to convert the error manually using Result::map_err before passing the value to error-chain. For example, the following construction will produce Result<Client<S>, IoError>:
x.accept().map_err(|e| e.1)

Declaring a map in a separate file and reading its contents

I'm trying to declare a map in a separate file, and then access it from my main function.
I want Rust's equivalent (or whatever comes closest) to this C++ map:
static const std::map<std::string, std::vector<std::string>> table = {
{ "a", { "foo" } },
{ "e", { "bar", "baz" } }
};
This is my attempt in Rust.
table.rs
use std::container::Map;
pub static table: &'static Map<~str, ~[~str]> = (~[
(~"a", ~[~"foo"]),
(~"e", ~[~"bar", ~"baz"])
]).move_iter().collect();
main.rs
mod table;
fn main() {
println(fmt!("%?", table::table));
}
The above gives two compiler errors in table.rs, saying "constant contains unimplemented expression type".
I also have the feeling that the map declaration is less than optimal for the purpose.
Finally, I'm using Rust 0.8.
As Chris Morgan noted, rust doesn't allow you to run user code in order to initialize global variables before main is entered, unlike C++. So you are mostly limited to primitive types that you can initialize with literal expressions. This is, afaik, part of the design and unlikely to change, even though the particular error message is probably not final.
Depending on your use case, you might want to change your code so you're manually passing your map as an argument to all the functions that will want to use it (ugh!), use task-local storage to initialize a tls slot with your map early on and then refer to it later in the same task (ugh?), or use unsafe code and a static mut variable to do much the same with your map wrapped in an Option maybe so it can start its life as None (ugh!).

Resources