These are chat archives for rust-lang/rust

1st
Nov 2016
Sebastian Blei
@iamsebastian
Nov 01 2016 09:20
Is it possible, to use the use *::*:: ... keyword in macros?
Kang Seonghoon
@lifthrasiir
Nov 01 2016 09:20
no. it would be very, very prone to error.
@iamsebastian you might be solving a problem in the wrong way. what’s your original question?
Sebastian Blei
@iamsebastian
Nov 01 2016 09:22
Using a macro at the nickel/diesel API, to reduce boilerplate for responding some standard stuff, like "query for all: users, items, parameters, cars, regions, etc."
Kang Seonghoon
@lifthrasiir
Nov 01 2016 09:22
I’m not well versed to diesel, but I guess the standard use foo::bar::*; should be enough for most cases.
Sebastian Blei
@iamsebastian
Nov 01 2016 09:23
Yeah. I will try to call the macro with the generated table as $ident.
Or will create the querying structs as Identifiables / Queryables.
Kang Seonghoon
@lifthrasiir
Nov 01 2016 09:25
aha, so diesel generates nested modules
Sebastian Blei
@iamsebastian
Nov 01 2016 09:25
Yeah.
It will create the schema of the database as modules.
Kang Seonghoon
@lifthrasiir
Nov 01 2016 09:28
hmm, don’t think there is a standard way to do that
though you can make a macro for that (sorry, but it’s also a standard answer for such cases :p)
Sebastian Blei
@iamsebastian
Nov 01 2016 09:29
For sure. :D
Sebastian Blei
@iamsebastian
Nov 01 2016 09:38

When I, for example, try to establish a connection to the database inside a macro, the compiler tells about:

error: expected expression, found statement (`let`)
  --> src/macros/mod.rs:25:9
   |
25 |         let connection = establish_conn();
   |         ^^^
   |
   = note: variable declaration using `let` is a statement

Am I not able to do this inside a macro?

If I call the macro with the established connection, all is fine.
Kang Seonghoon
@lifthrasiir
Nov 01 2016 09:40
does the macro body look like the following?
(…) => (
   let connection = establish_conn();
)
Sebastian Blei
@iamsebastian
Nov 01 2016 09:40
It is starting like this, yeah
    ($table:ident, $model:ty, $res:ident, $establish_conn:ident) => {
        let connection = $establish_conn();
        $res.send(DBResult::<$model> {
            data: $table
                .load::<$model>(&$conn)
                .expect("Error on querying db")
        }.to_json())
    }
Kang Seonghoon
@lifthrasiir
Nov 01 2016 09:40
let is not a proper statement, in particular because it affects the following statements. (sorry, my mistake)
I guess you want an expr, in which case you can wrap everything inside {}
that is, => ({ … })
Sebastian Blei
@iamsebastian
Nov 01 2016 09:43
These moments, when a bracket change everythings. Thanks a lot, @lifthrasiir
David McGillicuddy
@djmcgill
Nov 01 2016 13:22
I have an array (i.e &mut [Instance]) that I'd like to fill recursively. What's the best way to make sure that the sub-procedures don't step on each other's elements? I could have each one return a usize of the number of elements that it filled, but could I use something higher level like Seek or Cursor? From what I see, Write is only for arrays of u8s.
Maciej Hirsz
@maciejhirsz
Nov 01 2016 13:52
if you never backtrack you can just use an Iterator for that
David McGillicuddy
@djmcgill
Nov 01 2016 13:54
You can write to an Iterator?
Maciej Hirsz
@maciejhirsz
Nov 01 2016 14:27
@djmcgill calling .iter_mut() on &mut [Instance] will give you an iterator over &mut Instance, (calliing .next() on it will return Option<&mut Instance>), with which you can mutate and/or replace the elements in the slice
if you pass that iterator to a function that consumes exactly 5 elements, once you pop out of that function in your recursion you are guaranteed that those 5 elements can’t be read again
Maciej Hirsz
@maciejhirsz
Nov 01 2016 14:33