These are chat archives for rust-lang/rust

7th
Jan 2018
dunnousername
@dunnousername
Jan 07 2018 00:00
Hello, I'm trying to cross compile a Cargo project from Rust to wasm32-unknown-emscripten, and I'm getting a lot of the trait `Copy` may not be implemented for this type
At the end it says error: Could not compile `libc`.
Does that mean for wasm32 I should use rlibc?
dunnousername
@dunnousername
Jan 07 2018 00:16
The last version that worked on Travis for the project was 1.19.0-nightly, 8 or so months ago. I'm trying to use Wargo instead of Cargo --target this time, but it still doesn't work. Note that the project was tested to compile native binaries, not wasm32. I've tried both 1.19.0 and 1.20.0, as I don't know how to install 1.19.0-nightly with rustup. A lot of the errors are error[E0412]: cannot find type `c_char` in the crate root or error[E0573]: expected type, found function `stat64` in ~/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.15/src/unix/notbsd/mod.rs
Moggers
@Moggers
Jan 07 2018 05:28
Is there no way to do
type A = u32;
impl X for A {};
Hans W. Uhlig
@huhlig
Jan 07 2018 05:28
I thought type was just an alias
red75prime
@red75prime
Jan 07 2018 05:35
@Moggers , only if trait X is defined in the same crate.
Moggers
@Moggers
Jan 07 2018 05:35
yeah I found the struct A(u32); syntax
Denis Lisov
@tanriol
Jan 07 2018 06:57
@Shaddy If you're searching a subsequence in a byte sequence, you may try just using a bytes regex from the regex crate -- it's already pretty well optimized for this use, AFAIK.
Michal 'vorner' Vaner
@vorner
Jan 07 2018 13:30
Hello. I was wondering about something. When I have a foreign trait and a foreign type, I can't implement the trait for that type, because of the coherence rules. I see the reason for them. But sometimes, I'd really like to be able to implement the trait ‒ for example I have a type that doesn't implement Serialize. Now I have to do crazy things, like wrap it in a newtype, implement the trait for it, unwrap it (I know serde has means to avoid it, but this is not the general case). So I was thinking about something like crate-private implementation of the trait. I could implement it, but other crates would still think the type doesn't implement the trait (unless I pass it to them into their function that needs that trait ‒ the call would be on my side and they'd see the trait, not the concrete type). This would still avoid the problem with creating conflicting implementations (each crate could have its own implementation, but they would never fight each other). Was this talked about already, or is it a new idea? Is it obviously a bad idea, or is it worth thinking about it further? If so, where's the best place to get some initial feedback? (I guess just jumping and writing and RFC is an overkill)
Sherab Giovannini
@Shaddy
Jan 07 2018 14:14
@tanriol yep, I was thinking on using regxp, but as an optional feature. first I wanted to have a pure substr searcher
one really stupid question
why having self.field inside the function call does complain the borrow checker, but if I assign it to a value doesn't
(regarding to regxp, Ill replicate the bench using regxp with same prototype just to see how efficient it is)
Denis Lisov
@tanriol
Jan 07 2018 14:17
About regexp: you want to ensure that you compile the regexp only once, not every time.
The problem with self in your example is that first Rust mutably borrows self for a method call and then starts evaluating the argument. It needs to access self for self.offset, but it's already borrowed. This is one of the problems that will be gone when two-phase borrow and NLL finally become stable.
(actually, two-phase borrow only will already be enough)
Sherab Giovannini
@Shaddy
Jan 07 2018 14:55
thanks for the regxp tip
got it, so first time I do self.function it takes a borrow of self so next self.field is inside the scope of the borrowing
got it
Wouter De Coster
@wdecoster
Jan 07 2018 16:49
I read that the value of the last expression gets returned from a function, but you can also explicitly use return. I understand that this can be useful for more complex situations in which your function can return at multiple times, perhaps as part of an if statement. But what is the prefered synthax in a simple case with only the final value to be returned? I like explicitly writing return more, but that's probably since I'm used to write and read Python.
Michal 'vorner' Vaner
@vorner
Jan 07 2018 16:50
@wdecoster You can return the last one, the compiler won't stop you. But others will find your code strange and surprising.
It was new to me at the beginning too, but now I just wish other languages could work that way as well.
(well, some do, like functional languages or ruby)
Steve Klabnik
@steveklabnik
Jan 07 2018 16:51
it's not unique to rust, other languages work like this too :)
yeah
Wouter De Coster
@wdecoster
Jan 07 2018 16:51
So explicitly and unecessarily writing return is considered strange? Alright :p
It's useless to write code that you can only read yourselves so I don't mind adapting to what the community prefers.
Steve Klabnik
@steveklabnik
Jan 07 2018 16:52
does rustfmt auto fix this?
@wdecoster yes, as it's redundant, basically.
Michal 'vorner' Vaner
@vorner
Jan 07 2018 16:52
I'm not sure about rustfmt, but I think clippy points it out.
Wouter De Coster
@wdecoster
Jan 07 2018 16:53
oh rustfmt sounds nice
had to google it, won't need it in the first weeks I guess but good to know it exists
Michal 'vorner' Vaner
@vorner
Jan 07 2018 16:54
btw, does the rust book mention these tools? I think clippy is genuinely useful, and especially for beginners.
(the last time I've read it, I think it didn't)
Steve Klabnik
@steveklabnik
Jan 07 2018 16:54
it does not because it only talks about stable stuff
Wouter De Coster
@wdecoster
Jan 07 2018 16:54
and eh I can add rustfmt to Atom, that's neat
Steve Klabnik
@steveklabnik
Jan 07 2018 16:54
rustfmt is not on stable yet, though a preview will be on stable next release
once it hits 1.0, the book will talk about it
while these tools are cool, until the 1.0, they're a pain to use
Michal 'vorner' Vaner
@vorner
Jan 07 2018 16:55
:+1:
Steve Klabnik
@steveklabnik
Jan 07 2018 16:55
so recommending them to beginners is a double-edged sword
Wouter De Coster
@wdecoster
Jan 07 2018 16:55
I'll just lurk some more in this channel to pick up things like this then
Steve Klabnik
@steveklabnik
Jan 07 2018 16:55
the rls, rustfmt, and clippy are the three big tools
Wouter De Coster
@wdecoster
Jan 07 2018 16:56
I'll make a note of that and test them whenever I get to write serious code.
thanks!
blankhart
@blankhart
Jan 07 2018 17:57
hi there, using cargo, how do i pass an environment variable to a dependency's build script?
or more basically how do i pass an environment variable to cargo
as far as i can tell the documentation doesn't say how to do this, it just says it can be done
Robin Miller
@grenewode
Jan 07 2018 18:16
@blankhart Depending on what you are trying to do, you might want to use the env! with a build.rs script. You also might be looking for something like the dotenv crate
Michal 'vorner' Vaner
@vorner
Jan 07 2018 18:17
@blankhart What OS are you on? I don't know about window's cmd (it maybe works the same way), but on other OSes, it usually is VARIABLE=value cargo build
blankhart
@blankhart
Jan 07 2018 18:18
i am trying to compile a rust program that uses the rust-wiringpi library, which has a build script that tries to read the CC environment variable
trying to build it directly on the pi rather than cross-compiling
the library assumes i am trying to cross-compile and is looking for the wrong program
CC is set in my bash shell but isn't being read by the build script
thank you both though that i think is heading in the right direction
blankhart
@blankhart
Jan 07 2018 18:23
it looks like now it is reading CC but is just not running the custom build command
i will spend more time with it
thanks!
the issue seems to be that CC isn't getting read here https://github.com/Ogeon/rust-wiringpi/blob/master/Makefile
a Makefile issue rather than a rust thing
Robin Miller
@grenewode
Jan 07 2018 18:32
what is the error that you get when rust tries to execute the Makefile?
blankhart
@blankhart
Jan 07 2018 18:33

``CC=/usr/bin/gcc cargo build Compiling wiringpi v0.2.1 error: failed to run custom build command forwiringpi v0.2.1process didn't exit successfully:/home/blankhart/experiments/blink/target/debug/build/wiringpi-dd678d056052046e/build-script-build` (exit code: 101)
--- stdout
make -C wiringPi/wiringPi clean
make[1]: Entering directory '/home/blankhart/.cargo/registry/src/github.com-1ecc6299db9ec823/wiringpi-0.2.1/wiringPi/wiringPi'
[Clean]
make[1]: Leaving directory '/home/blankhart/.cargo/registry/src/github.com-1ecc6299db9ec823/wiringpi-0.2.1/wiringPi/wiringPi'
make -C wiringPi/wiringPi static CC=arm-linux-gnueabihf-gcc DEBUG=-O2
make[1]: Entering directory '/home/blankhart/.cargo/registry/src/github.com-1ecc6299db9ec823/wiringpi-0.2.1/wiringPi/wiringPi'
[Compile] wiringPi.c
make[1]: Leaving directory '/home/blankhart/.cargo/registry/src/github.com-1ecc6299db9ec823/wiringpi-0.2.1/wiringPi/wiringPi'

--- stderr

make[1]: arm-linux-gnueabihf-gcc: Command not found
make[1]: [Makefile:96: wiringPi.o] Error 127
make:
[Makefile:5: wiringpi] Error 2
thread 'main' panicked at 'failed to build wiringPi C library (exit code Some(2))', /home/blankhart/.cargo/registry/src/github.com-1ecc6299db9ec823/wiringpi-0.2.1/build.rs:12:44
note: Run with RUST_BACKTRACE=1 for a backtrace.```

it is running with CC=arm-linux-gnueabihf-gcc rather than CC=/usr/bin/gcc
Robin Miller
@grenewode
Jan 07 2018 18:43
So, I think this is what is happening:
  1. wiringpi build.rs script calls make
arg, never mind
actually, it looks like this is the problem: Ogeon/rust-wiringpi@205f9b9.
The issue you were experiencing was recently fixed, but the fix may not have been published to crates.io yet.
@blankhart ^
blankhart
@blankhart
Jan 07 2018 18:51
Thanks! I'll try to find a workaround in the meantime
good point that crates.io may not reflect what im seeing on github
Robin Miller
@grenewode
Jan 07 2018 19:03
You may be able to just use change your Cargo.toml so that you have wiringpi = { git = "repo" } and use the git version directly
blankhart
@blankhart
Jan 07 2018 19:15
clever! that worked! and wouldn't have occurred to me as the next step since i haven't has occasion to use that feature before. this was very informative, thank you
Ghost
@ghost~5a52704cd73408ce4f86c156
Jan 07 2018 19:15
hi everyone, im studyin rust programming language for a week. i ve backend experiences with php but not enough for my new job. i will develop a mobile app with serverside image processing. it must be amazingly fast and light. which do u suggest
i mean for frameworks or libs or crates?