I have a macro which works:
#[macro_export]
macro_rules! btreemap {
($( $key: expr => $val: expr ),*) => {{
let mut map = ::std::collections::BTreeMap::new();
$( map.insert($key, $val); )*
map
}}
}
but which the compiler warns about:
warning: variable does not need to be mutable
--> src/util.rs:16:11
|
16 | let mut map = ::std::collections::BTreeMap::new();
| ----^^^
| |
| help: remove this `mut`
|
::: src/foo.rs:49:13
|
79 | foo: btreemap!{},
| ----------- in this macro invocation
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
If I remove the mut, I get an error:
error[E0596]: cannot borrow `map` as mutable, as it is not declared as mutable
--> src/util.rs:17:10
|
16 | let map = ::std::collections::BTreeMap::new();
| --- help: consider changing this to be mutable: `mut map`
17 | $( map.insert($key, $val); )*
| ^^^ cannot borrow as mutable
|
::: src/bar.rs:110:18
|
116 | let bar = btreemap!{quux => wibble};
| -------------------------- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
i.e. If mut it warns on empty invocations, but if it's not if errors on non-empty invocations.
How can I fix this?
How can I fix this?
You can suppress the warning by adding #[allow(unused_mut)] to the code generated by the macro:
macro_rules! btreemap {
($( $key: expr => $val: expr ),*) => {{
#[allow(unused_mut)]
let mut map = ::std::collections::BTreeMap::new();
$( map.insert($key, $val); )*
map
}}
}
The warning is about as benign as a warning can get, and warning suppressions of this kind are often seen in macro definitions.
Another possibility is the one you already discovered, to special-case empty expansion. But I'd prefer the explicit warning suppression because it eliminates the (bogus) warning without complicating the macro. The special case you introduced wasn't actually necessary - the exact same code would be generated without it.
Fixed by adding a special case for the empty invocation:
#[macro_export]
macro_rules! btreemap {
() => {{
::std::collections::BTreeMap::new()
}};
($( $key: expr => $val: expr ),*) => {{
let mut map = ::std::collections::BTreeMap::new();
$( map.insert($key, $val); )*
map
}};
}
Related
I am fiddling with writing a Lexer that takes an input &str and returns an Iterator over the tokens I generate for the input:
pub enum Token<'a> {
Lparen(&'a str), // (
Rparen(&'a str), // )
Lbrack(&'a str), // [
// -- snip --
}
pub fn tokenize(input: &str) -> impl Iterator<Item = Token> {
let mut chars = input.char_indices();
std::iter::from_fn(move || {
if let Some(next) = chars.next() {
let (start, c) = next;
let end = start + 1;
return match c {
'(' => Some(Token::Lparen(&input[start..end])),
')' => Some(Token::Rparen(&input[start..end])),
'[' => Some(Token::Lbrack(&input[start..end])),
'=' => from_eq(input, start, chars),
// move of chars occurs here ^^^^^
// -- snip --
_ => Some(Token::Unknown(&input[start..end])),
}
}
None
})
}
fn from_eq<'a>(input: &'a str, start: usize, mut chars: CharIndices) -> Option<Token<'a>> {
// -- snip --
}
I am moving chars into the closure and within the closure I pass it on to from_eq. from_eq is supposed to iterate further on chars with a specific handling of the characters it finds.
Now of course, I the borrow checker nags at me
error[E0507]: cannot move out of `chars`, a captured variable in an `FnMut` closure
--> src/lib.rs:106:47
|
94 | let mut chars = input.char_indices();
| --------- captured outer variable
95 |
96 | std::iter::from_fn(move || {
| ________________________-
97 | |
98 | | if let Some(next) = chars.next() {
99 | |
... |
106 | | '=' => from_eq(input, start, chars),
| | ^^^^^ move occurs because `chars` has type `CharIndices<'_>`, which does not implement the `Copy` trait
... |
122 | | None
123 | | })
| |_____- captured by this `FnMut` closure
My vague understanding is that I pass a mutable reference to from_eq which is also mutable outside of its scope, so now I would have two owners which is not ok.
I don't know how if my understanding is correct though or how to handle it even. I want to mutate chars in both functions, I am sure once from_eq has run, it's not mutating chars anymore—but how do I comply with the borrow checker here?
Suppose from_eq() returned Some(...). Next iteration of the returned iterator, std::iter::from_fn() will call the closure again and it in its turn will call chars.next(). But chars was moved into from_eq() in the previous iteration!
This is what the compiler says: from_fn() takes a FnMut, and you cannot move a captured variable - chars - in a FnMut closure, because it can be called again and the variable will now be in invalid state.
The solution is to borrow chars in from_eq() instead of moving it:
fn from_eq<'a>(input: &'a str, start: usize, chars: &mut CharIndices) -> Option<Token<'a>> { ... }
'=' => from_eq(input, start, &mut chars),
You're not actually passing a mutable reference to from_eq. That's precisely the problem. You're moving chars into from_eq as a mutable argument. Once you've moved a value in Rust, you can't use it anymore, this is why you're getting the error.
But to use an iterator you only need a mutable reference to it, let's change from_eq's signature to reflect that:
fn from_eq<'a>(input: &'a str, start: usize, chars: &mut CharIndices) -> Option<Token<'a>> {
// -- snip --
}
And in the calling code, we can use the by_ref() method provided by Iterator to pass chars in:
return match c {
'(' => Some(Token::Lparen(&input[start..end])),
')' => Some(Token::Rparen(&input[start..end])),
'[' => Some(Token::Lbrack(&input[start..end])),
'=' => from_eq(input, start, chars.by_ref()),
// --------------------------------^^^^^^^^^
// pass in `chars` by mutable reference instead of by value
// -- snip --
_ => Some(Token::Unknown(&input[start..end])),
}
I'm creating a macro called throw_error. I expected this to compile yet, it fails:
// Util structs + types
...
// Util macros
#[macro_export]
macro_rules! throw_error {
() => {
RaptorexError {
message: String::new(),
line: line!(),
file: file!().to_owned(),
}
};
($($msg:tt),*) => {
let mut final_msg = String::new();
$(
final_msg.push_str(&format!("{} ", $msg));
)*
// remove trailing whitespace
final_msg.pop();
RaptorexError {
message: final_msg,
line: line!(),
file: file!(),
}
}
}
// Util functions
...
I get several errors from using the macro in my other code.
Erros:
error: macro expansion ignores token `final_msg` and any following
--> /Users/henryboisdequin/Desktop/raptorex/raptorex_util/src/lib.rs:30:13
|
30 | final_msg.push_str(&format!("{} ", $msg));
| ^^^^^^^^^
|
::: compiler/src/parser/parser.rs:44:29
|
44 | _ => return Err(throw_error!("Unexpected token:", current_token)),
| ------------------------------------------------- help: you might be missing a semicolon here: `;`
| |
| caused by the macro expansion here
|
= note: the usage of `throw_error!` is likely invalid in expression context
error[E0658]: `let` expressions in this position are experimental
--> compiler/src/parser/parser.rs:44:29
|
44 | _ => return Err(throw_error!("Unexpected token:", current_token)),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
= help: add `#![feature(let_chains)]` to the crate attributes to enable
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: `let` expressions are not supported here
--> compiler/src/parser/parser.rs:44:29
|
44 | _ => return Err(throw_error!("Unexpected token:", current_token)),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: only supported directly in conditions of `if`- and `while`-expressions
= note: as well as when nested within `&&` and parenthesis in those conditions
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: unused imports: `DATA_TYPES`, `KEYWORDS`
--> compiler/src/parser/parser.rs:3:28
|
3 | lexer::tokens::{Token, DATA_TYPES, KEYWORDS},
| ^^^^^^^^^^ ^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error[E0308]: mismatched types
--> compiler/src/parser/parser.rs:44:29
|
44 | _ => return Err(throw_error!("Unexpected token:", current_token)),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `raptorex_util::RaptorexError`, found `bool`
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 4 previous errors; 1 warning emitted
What is the cause of these errors and how can I fix it?
You need another set of {}s so the macro creates a block containing the statements instead of the individual statements themselves:
#[macro_export]
macro_rules! throw_error {
() => {
RaptorexError {
message: String::new(),
line: line!(),
file: file!().to_owned(),
}
};
($($msg:tt),*) => {
{ // <------------------
let mut final_msg = String::new();
$(
final_msg.push_str(&format!("{} ", $msg));
)*
// remove trailing whitespace
final_msg.pop();
RaptorexError {
message: final_msg,
line: line!(),
file: file!(),
}
} // <-------------------
}
}
The {} in the (...) => {} is part of the macro syntax and isn't part of the generated code.
I want to create a simple websocket server. I want to process the incoming messages and send a response, but I get an error:
error: captured variable cannot escape `FnMut` closure body
--> src\main.rs:32:27
|
32 | incoming.for_each(|m| async {
| _________________________-_^
| | |
| | inferred to be a `FnMut` closure
33 | | match m {
34 | | // Error here...
35 | | Ok(message) => do_something(message, db, &mut outgoing).await,
36 | | Err(e) => panic!(e)
37 | | }
38 | | }).await;
| |_____^ returns a reference to a captured variable which escapes the closure body
|
= note: `FnMut` closures only have access to their captured variables while they are executing...
= note: ...therefore, they cannot allow references to captured variables to escape
This gives a few hits on Stack Overflow but I don't see anywhere in my code where a variable is escaping. The async block won't run concurrently, so I don't see any problem. Furthermore, I feel like I am doing something very simple: I get a type which allows me to send data back to the client, but when using a reference to it in the async block, it gives a compile error. The error only occurs when I use the outgoing or db variable in the async code.
This is my code (error is in the handle_connection function):
main.rs
use tokio::net::{TcpListener, TcpStream};
use std::net::SocketAddr;
use std::sync::Arc;
use futures::{StreamExt, SinkExt};
use tungstenite::Message;
use tokio_tungstenite::WebSocketStream;
struct DatabaseConnection;
#[tokio::main]
async fn main() -> Result<(), ()> {
listen("127.0.0.1:3012", Arc::new(DatabaseConnection)).await
}
async fn listen(address: &str, db: Arc<DatabaseConnection>) -> Result<(), ()> {
let try_socket = TcpListener::bind(address).await;
let mut listener = try_socket.expect("Failed to bind on address");
while let Ok((stream, addr)) = listener.accept().await {
tokio::spawn(handle_connection(stream, addr, db.clone()));
}
Ok(())
}
async fn handle_connection(raw_stream: TcpStream, addr: SocketAddr, db: Arc<DatabaseConnection>) {
let db = &*db;
let ws_stream = tokio_tungstenite::accept_async(raw_stream).await.unwrap();
let (mut outgoing, incoming) = ws_stream.split();
// Adding 'move' does also not work
incoming.for_each(|m| async {
match m {
// Error here...
Ok(message) => do_something(message, db, &mut outgoing).await,
Err(e) => panic!(e)
}
}).await;
}
async fn do_something(message: Message, db: &DatabaseConnection, outgoing: &mut futures_util::stream::SplitSink<WebSocketStream<TcpStream>, Message>) {
// Do something...
// Send some message
let _ = outgoing.send(Message::Text("yay".to_string())).await;
}
Cargo.toml
[dependencies]
futures = "0.3.*"
futures-channel = "0.3.*"
futures-util = "0.3.*"
tokio = { version = "0.2.*", features = [ "full" ] }
tokio-tungstenite = "0.10.*"
tungstenite = "0.10.*"
When using async move, I get the following error:
code
incoming.for_each(|m| async move {
let x = &mut outgoing;
let b = db;
}).await;
error
error[E0507]: cannot move out of `outgoing`, a captured variable in an `FnMut` closure
--> src\main.rs:33:38
|
31 | let (mut outgoing, incoming) = ws_stream.split();
| ------------ captured outer variable
32 |
33 | incoming.for_each(|m| async move {
| ______________________________________^
34 | | let x = &mut outgoing;
| | --------
| | |
| | move occurs because `outgoing` has type `futures_util::stream::stream::split::SplitSink<tokio_tungstenite::WebSocketStream<tokio::net::tcp::stream::TcpStream>, tungstenite::protocol::message::Message>`, which does not implement the `Copy` trait
| | move occurs due to use in generator
35 | | let b = db;
36 | | }).await;
| |_____^ move out of `outgoing` occurs here
FnMut is an anonymous struct, since FnMutcaptured the &mut outgoing, it becomes a field inside of this anonymous struct and this field will be used on each call of FnMut , it can be called multiple times. If you lose it somehow (by returning or moving into another scope etc...) your program will not able to use that field for further calls, due to safety Rust Compiler doesn't let you do this(for your both case).
In your case instead of capturing the &mut outgoing we can use it as argument for each call, with this we'll keep the ownership of outgoing. You can do this by using fold from futures-rs:
incoming
.fold(outgoing, |mut outgoing, m| async move {
match m {
// Error here...
Ok(message) => do_something(message, db, &mut outgoing).await,
Err(e) => panic!(e),
}
outgoing
})
.await;
This may seem a bit tricky but it does the job, we are using constant accumulator(outgoing) which will be used as an argument for our FnMut.
Playground (Thanks #Solomon Ucko for creating reproducible example)
See also :
How to return the captured variable from `FnMut` closure, which is a captor at the same time
How can I move a captured variable into a closure within a closure?
I'm rewriting a simple TCP based server to experiment with Rust. It should retrieve input of an client and then match that input to run a function:
use std::io::BufRead;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Write;
use std::net::{TcpListener, TcpStream};
use std::thread;
fn handle_connection(stream: TcpStream) {
let stream_clone = stream.try_clone().unwrap();
let mut reader = BufReader::new(stream);
let mut writer = BufWriter::new(stream_clone);
loop {
let mut s = String::new();
reader.read_line(&mut s).unwrap();
match s.as_str() {
//"test" => writer.write(s.as_bytes()).unwrap();
"test" => writer.write(b"test successfull").unwrap(),
_ => writer.write(b"Command not recognized...").unwrap(),
}
writer.flush().unwrap();
}
}
fn main() {
let listener = TcpListener::bind("127.0.0.1:8888").unwrap();
for stream in listener.incoming() {
thread::spawn(move || {
handle_connection(stream.unwrap());
});
}
}
And the error:
error[E0308]: mismatched types
--> src/main.rs:16:9
|
16 | / match s.as_str() {
17 | | //"test" => writer.write(s.as_bytes()).unwrap();
18 | | "test" => writer.write(b"test successfull").unwrap(),
19 | | _ => writer.write(b"Command not recognized...").unwrap(),
20 | | }
| |_________^ expected (), found usize
|
= note: expected type `()`
found type `usize`
My main problem now is to check the retrieved bytes if they belong to an match and I'm not quite sure how to achieve that.
I couldn't find a fix for this online, rustc --explain didn't help me either
Add a semicolon after your match expression.
The type of all of the match arms is usize, so the resulting type of the match is also a usize. Your code is effectively
fn main() {
{
42
}
println!("Hi");
}
error[E0308]: mismatched types
--> src/main.rs:3:9
|
3 | 42
| ^^ expected `()`, found integer
See also:
Why don't we add a semicolon (;) at the end of if/else?
Are semicolons optional in Rust?
A simple error handler added to nom
The first one compiles with no errors, the second one errors out
use nom::*;
use std::str;
// This works
named!(
field<&str>,
map!(
complete!(add_return_error!(
ErrorKind::Custom(1),
preceded!(
tag!("."),
recognize!(many1!(
alt!(alphanumeric => { |_| true } | char!('_') => {|_| true})
))
)
)),
|v| str::from_utf8(v).unwrap()
)
);
// This doesn't compile
named!(
entity<&str>,
add_return_error!(
ErrorKind::Custom(2),
map!(
recognize!(many1!(
alt!(alphanumeric => { |_| true } | char!('_') => {|_| true})
)),
|v| str::from_utf8(v).unwrap()
)
)
);
Error is
cargo build
Compiling pdl_parser v0.1.0 (file:///H:/parser)
error[E0282]: type annotations needed
--> src\pdl_parser.rs:72:1
|
72 | / named!(
73 | | entity<&str>,
74 | | add_return_error!(
75 | | ErrorKind::Custom(2),
... |
82 | | )
83 | | );
| |__^ cannot infer type for `E`
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
Why does the first one work and second one not? And what can I do to fix it? I tried changing the signature of the second one to entity<&[u8],&str,ErrorKind> and then to i32 and u32 but no success.
The solution to the cannot infer type for E error is to separate out the parsing steps so the compiler does not get so confused.
Rewriting the parser-combinator as
named!(
entity_name<Vec<u8>>,
many1!(alt!(alphanumeric => { |x : &[u8]| x[0] } | char!('_') => {|_| b'_'}))
);
named!(
entity<&[u8],&str,i32>,
add_return_error!(
ErrorKind::Custom(2),
map!(recognize!(entity_name),|v| str::from_utf8(v).unwrap())
)
);
And the corresponding test
#[test]
fn parse_pdl_error_test() {
assert_eq!(entity(b"_error_stack").to_result(), Ok("_error_stack"));
assert_eq!(entity(b"[];']").to_result(), Err(ErrorKind::Custom(2)));
}