These are chat archives for rust-lang/rust

18th
Aug 2018
trsh
@trsh
Aug 18 2018 07:23
Is there any way, in proc macro, to check if a certain trait is already implemented for a struct?
tandrysyawaludin
@tandrysyawaludin
Aug 18 2018 07:23
How to solve this error?
.get_results(&connection);
   |      ^^^^^^^^^^^ the trait `diesel::Connection` is not implemented for `&diesel::PgConnection`
trsh
@trsh
Aug 18 2018 07:24
So don't do it twice
tandrysyawaludin
@tandrysyawaludin
Aug 18 2018 08:50
it is mean i call diesel twice or what?
i wanna run sql_query with postgres db but i got this error
sql_query("SELECT * FROM provinces").load(&connection);
   |                                          ^^^^ the trait `diesel::Connection` is not implemented for `&diesel::PgConnection`
here, my full code
use diesel::dsl::sql_query;
use database::schema::provinces;
use database::schema::districts;
use database::schema::sub_districts;
use diesel;
use diesel::pg::PgConnection;
use diesel::prelude::RunQueryDsl;

#[derive(Serialize, Deserialize)]
pub struct Area {
  pub province_name: String,
  pub district_name: String,
  pub sub_district_name: String,
  pub sub_district_id: String
}

impl Area {
  pub fn read(connection: &PgConnection) -> Vec<Area> {
    sql_query("SELECT * FROM provinces").load(&connection);
    return vec![]
  }
}
Denis Lisov
@tanriol
Aug 18 2018 08:54
Try removing the extra & before connection
trsh
@trsh
Aug 18 2018 08:57
@tanriol can a Box::into_raw pointer exist in multiple other boxed pointers?
smth like this
let mut t1 = Box::new(K {i: CString::new("123").unwrap().into_raw() });
    let mut t = Box::new(Z {i: Box::into_raw(t1) });
    let mut kaka = Box::into_raw(t);
    let mut kaka2 = Box::into_raw(Box::new(M{i: kaka}));
or kaka2 now owns kaka ?
and kaka can't be used safe in it's own
?
Denis Lisov
@tanriol
Aug 18 2018 09:02
You can store it in multiple places, but note that you will have to ensure that Box::from_raw and the following drop executes only once, which may be nontrivial if you have multiple pointers of this kind.
trsh
@trsh
Aug 18 2018 09:03
Great
Playing around with it
Denis Lisov
@tanriol
Aug 18 2018 09:03
Are you generating your structures from a C header, or generating C from Rust, or generating both from some other representation?
trsh
@trsh
Aug 18 2018 09:04
Bindgen from C header
My problem now is that I can't do Drop impl multiple times, what the macro will do, as I use same objects ir multiple logics
SO I will wrap the objects, and the execute drop
Altought this is also just an idea, that now I think can not work :D
Denis Lisov
@tanriol
Aug 18 2018 09:09
How about making your proc-macro process all schemas in one invocation instead of a schema per invocation?
trsh
@trsh
Aug 18 2018 09:11
That sounds scary :D
Denis Lisov
@tanriol
Aug 18 2018 09:24
For me that seems to be a bit more adequate :-)
Actually I don't know how you're running it but I'd seriously consider generating this code not from a macro, but with a special utility not unlike bindgen itself...
trsh
@trsh
Aug 18 2018 10:04
Is there something like a compile time cache?
This way I could check globally, to not re-implement a trait
Denis Lisov
@tanriol
Aug 18 2018 10:08
Don't know. At the moment you probably could use a static variable in the proc-macro crate, but not sure whether that's guaranteed to continue working.
That's assuming you're trying to cache between multiple invocations of your macro, are you?
trsh
@trsh
Aug 18 2018 10:11
Yes
Its called from diff files
but shares some structures
Simp file write,read
Could work, but seems stupid
Michal 'vorner' Vaner
@vorner
Aug 18 2018 10:15
@trsh Not really following the exact discussion, but stupid and simple solutions are sometimes the best. Especially for a first shot at something. If it's a proc macro or something just for your own use, it needs the job done, not be perfect.
tandrysyawaludin
@tandrysyawaludin
Aug 18 2018 10:25
@tanriol i stil got error
19 |     sql_query("SELECT * FROM provinces").load(connection);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `U`
trsh
@trsh
Aug 18 2018 10:25
cargo build > cc.rs
    Blocking waiting for file lock on build directory
Commooon :D
Denis Lisov
@tanriol
Aug 18 2018 10:27
@tandrysyawaludin That's a different one :-) your code does not specify what data structure you're loading the data into.
@trsh What is this command supposed to do?
trsh
@trsh
Aug 18 2018 10:29
build the proect
ignore >> cc part
Denis Lisov
@tanriol
Aug 18 2018 10:29
That's kinda obvious, the redirect is not :-)
trsh
@trsh
Aug 18 2018 10:30
Don't want to restart pc
ghh
apiraino
@apiraino
Aug 18 2018 10:31
:point_up: 18. August 2018 12:25
@trsh not sure but it looks like you have another instance of cargo running in the background, maybe your IDE is linting?
tandrysyawaludin
@tandrysyawaludin
Aug 18 2018 10:32
@tanriol how to add specify data structure?
trsh
@trsh
Aug 18 2018 10:32
hmm
No other instanes
Michal 'vorner' Vaner
@vorner
Aug 18 2018 10:33
kill all cargos and rustcs and delete the target directory and you'll be fine
apiraino
@apiraino
Aug 18 2018 10:36
if you often find yourself seeing that message, ensure no one is running another cargo build or cargo test behind your back. I discovered I was racing my EMACS linter (each run invalidating the build cache)
trsh
@trsh
Aug 18 2018 10:43
yeah, tnx
Tochukwu Nkemdilim
@TNkemdilim
Aug 18 2018 11:28
@rishavs Have you resolve it?
A Dinesh
@dineshadepu
Aug 18 2018 12:07
Hi guys. While reading a file we either successfully read it or we get an Error of type io::Error. I see we don't import it into our file. Why is it?
And even the ParseIntError, while parsing a string to integer
Rishav Sharan
@rishavs
Aug 18 2018 12:11
@TNkemdilim nope. :(
Edvin Malinovskis
@nCrazed
Aug 18 2018 12:25
dineshadepu: probalby due to prelude
A Dinesh
@dineshadepu
Aug 18 2018 12:25
But prelude doesn't import the error types @nCrazed
Denis Lisov
@tanriol
Aug 18 2018 12:26
@dineshadepu Any glob (*) imports?
A Dinesh
@dineshadepu
Aug 18 2018 12:27
Please go to code block named io-basic-unwrap
Search for this name
Denis Lisov
@tanriol
Aug 18 2018 12:29
You don't need to import the type unless you want to name it :-)
A Dinesh
@dineshadepu
Aug 18 2018 12:30
Ohh. Alright. Thanks @tanriol
trsh
@trsh
Aug 18 2018 15:30
error: failed to write dep-graph to /xx/target/debug/incremental/manu_derive-684vgi2z78qs/s-f3z0d06sz9-ejj3qe-working/query-cache.bin: No space left on device (os error 28)
Device has 1.8 GB free space
whata hek
Denis Lisov
@tanriol
Aug 18 2018 15:32
And the device size is? :-)
trsh
@trsh
Aug 18 2018 15:32
35.9gb in total
Denis Lisov
@tanriol
Aug 18 2018 15:33
ext4 filesystem, correct?
trsh
@trsh
Aug 18 2018 15:33
I think so
Ubuntu
I had no sudden change in disk space, before it worked also with 800 MB free
Denis Lisov
@tanriol
Aug 18 2018 15:35
Not sure, but ext4 by default reserves 5% of the space for superuser only...
trsh
@trsh
Aug 18 2018 17:38
Should work on the paper
Denis Lisov
@tanriol
Aug 18 2018 19:05
    let c = 2 as *mut i32;
    let d = unsafe{Box::from_raw(c)};
This definitely should not work, on paper or anything else.
apiraino
@apiraino
Aug 18 2018 19:08
Hello, I have a funny question for your (and my) Saturday evening:
use std::env;
extern crate dotenv;
use dotenv::dotenv;
#[macro_use]
extern crate dotenv_codegen;

fn main() {
    dotenv().ok();
    env!("MY_ENV_VAR");
    dotenv!("MY_ENV_VAR");
}
this does not compile:
$ cargo run
   Compiling test-env v0.1.0 (file:///home/me/Projects/rust/test-env)                                    
error: environment variable `MY_ENV_VAR` not defined
  --> src/main.rs:13:5
   |
13 |     env!("MY_ENV_VAR");
   |     ^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
But if I invert the execution order (i.e. first dotenv!() then env!(), the error disappears
is there any apparent reason for that that I'm missing?
Denis Lisov
@tanriol
Aug 18 2018 19:13
Does dotenv give you an error too?
apiraino
@apiraino
Aug 18 2018 19:13
nope, dotenv!() always works
Denis Lisov
@tanriol
Aug 18 2018 19:14
Is the variable actually set?
apiraino
@apiraino
Aug 18 2018 19:14
why, yes :) I have a .env with:
$ cat .env
MY_ENV_VAR="hey"
funny thing: if I retrieve the env var with env::var("xxx") rather than using the macro env!() the error disappear
Denis Lisov
@tanriol
Aug 18 2018 19:16
env::var is runtime, not compile time.
apiraino
@apiraino
Aug 18 2018 19:16
wanna hear something EVEN funnier?

if I write this:

     dotenv!("MY_ENV_VAR");                                                                                
     env!("MY_ENV_VAR");                                                                                   
     env::var("MY_ENV_VAR").unwrap();

no compile errors

if it's a runtime check, why the behaviour changes when I run cargo check?
apiraino
@apiraino
Aug 18 2018 19:21
besides, I don't understand why behaviour changes moving a macro up or down. but, nevermind - macros have a bit of black magic I'm not ready for. thanks anyway :+1:
Denis Lisov
@tanriol
Aug 18 2018 19:21
I'm pretty sure that the dotenv macro, when expanded, adds the variable to the rustc environment.
apiraino
@apiraino
Aug 18 2018 19:23
^^^ yes, that's correct. Read the .env file and load them up into the environment
Denis Lisov
@tanriol
Aug 18 2018 19:27
It loads it into environment and makes them accessible to the env macro. So if the first dotenv! precedes the first env!, everything "works"...
apiraino
@apiraino
Aug 18 2018 19:29
I may be wrong ... but ... after dotenv().ok() env vars should be available also to the "plain" env::var thing:
https://crates.io/crates/dotenv
(at least that's what I understand form those docs)
but maybe env::var doesn't get them anyway until runtime, like you said. whatevs
Denis Lisov
@tanriol
Aug 18 2018 19:32
dotenv().ok() and env::var work at runtime, dotenv! and env! at compile time.
apiraino
@apiraino
Aug 18 2018 19:38
uhm ... ok so I mixed things up a little bit, but then on cargo check, dotenv().ok() should fail too, which is not
I mean: it should fail to load anything from the .env file
which it does because then everything is available to the macros
but - look - it' nothing really important, I got the general idea, though