These are chat archives for rust-lang/rust

16th
Dec 2017
Michael Thomas
@Michaelt293
Dec 16 2017 01:25
This is probably a super simple question, but what would be the best approach to download an image from a link, ie., http://charts.bsch.com.au//tmp/gfs.stormcast.bsch.init-2017121512z.fcst-201712161600z.cape.nsw.null.0.png ? I have been looking at the reqwest and curl libraries.
blankhart
@blankhart
Dec 16 2017 02:36
hi there i can't figure out how to configure cargo to ignore dead code and unused imports in tests
i created a .cargo/config file containing
rustflags = ["-A unused_imports", "-A dead_code"]
however, this broke the build
is there a tutorial somewhere?
more generally i am surprised i can't name the config file config.toml (with the suffix) to trigger syntax highlighting in an editor
blankhart
@blankhart
Dec 16 2017 02:51
ok i figured it out
as a hobbyist new to rust i can say that 99% of my confusion has stemmed from not understanding how the directives beginning with # work
blankhart
@blankhart
Dec 16 2017 02:59
are these well documented somewhere? is there a list of what i can #[allow(...)]?
also curious what is the difference between # and #!?
Sathya Narrayanan
@sourcepirate
Dec 16 2017 08:49
https://stackoverflow.com/questions/36590549/matching-on-a-reference-to-an-enum
Hey why are we using ref to match strings present inside enum ?
Andrey Lesnikov
@ozkriff
Dec 16 2017 08:50

is there a list of what i can #[allow(...)]?

rustc -W help

also curious what is the difference between # and #!?

https://doc.rust-lang.org/reference/attributes.html - #! are "a special crate-level attribute"

Sathya Narrayanan
@sourcepirate
Dec 16 2017 08:53
@ozkriff # and #! determines the scope at which the meta attributes might act on
Aleksey Kladov
@matklad
Dec 16 2017 08:56
Yeah, #! is not exactly crate level: it's an attribute that applies to enclosing declaration. That is, #[foo] mod x {} and mod x { #![foo] }are equivalent. However, the only real case where #[ can't be used is the crate root, so that's why typically you see #! only for crate-level attribures.
Andrey Lesnikov
@ozkriff
Dec 16 2017 08:58
@matklad Hmmm, TIL. I've only used them in crate's root so far

Hey why are we using ref to match strings present inside enum ?

Because we can't move the string out of the enum. We just want to borrow it

Sathya Narrayanan
@sourcepirate
Dec 16 2017 09:00
but can we move a string out of enum ??
Andrey Lesnikov
@ozkriff
Dec 16 2017 09:01
Not in &self-functions
Sathya Narrayanan
@sourcepirate
Dec 16 2017 09:01
Yeah ok. Got it
Restioson
@Restioson
Dec 16 2017 09:20
@ozkriff trivia: you can use extern crate in submodules
#![feature(test)]

fn same_thing<T>(x: T) -> T {
    x
}

#[cfg(test)]
mod test {
    extern crate test;
    use same_thing;

    #[test]
    fn same_thing_is_same() {
        assert_eq!(self::test::black_box(same_thing(10)), 10);
    }
}
the black box is just to use test
Sai Zeng
@zengsai
Dec 16 2017 09:59
by using #![feature(extern_absolute_paths, crate_in_paths)], how to import macros defined in external crate?
David
@yenicelik
Dec 16 2017 10:07
How can i convert a i64 to i32 (i know that the number is not overflowing i32)?
Sai Zeng
@zengsai
Dec 16 2017 10:09
i64 as i32
David
@yenicelik
Dec 16 2017 10:10
wtf
thx!
Restioson
@Restioson
Dec 16 2017 10:10
haha
David
@yenicelik
Dec 16 2017 10:10
i guess i had a different error last time when i tried it
Restioson
@Restioson
Dec 16 2017 10:10
@zengsai you put #[macro_use] on the extern crate
i don't think you need those features
assuming that crate has #[macro_export]ed their macro
Sai Zeng
@zengsai
Dec 16 2017 10:13
I’m trying to use these new features. @Restioson
Restioson
@Restioson
Dec 16 2017 10:13
But you don't need them for that afaik
Sai Zeng
@zengsai
Dec 16 2017 10:15
These new features are trying to remove extern crate … from source file
Denis Lisov
@tanriol
Dec 16 2017 10:39
@zengsai AFAIK, the #[macro_use] extern crate ...; cannot be omitted as the idea is that with Macros 2.0 (or whatever the version number is) the #[macro_use] won't be needed.
Restioson
@Restioson
Dec 16 2017 10:47
Well, this takes my 'weirdest rust problem' for this month: https://i.imgur.com/D7TKXam.png
Debug mode is much more useful https://i.imgur.com/mTU08tH.png
Restioson
@Restioson
Dec 16 2017 10:56
So much easier to debug now that I've set up actual panic handling. The reason it's my weirdest is because i had no clue it was even related to that...
Denis Lisov
@tanriol
Dec 16 2017 11:02
Aren't you missing an option to print a backtrace in your panic handling? :-)
Restioson
@Restioson
Dec 16 2017 11:02
I'm looking at that now, but for now this is a very simplified impl
Would you happen to know how that could be done?
At the moment I'm trying to figure out how the hell my code even got there
Ah, figured it out
Via the backtrace crate?
Leme look what core does
Denis Lisov
@tanriol
Dec 16 2017 11:05
The default panic hook generates the backtrace :-)
Restioson
@Restioson
Dec 16 2017 11:06
Hmm...
The actual panic_fmt impl?
Denis Lisov
@tanriol
Dec 16 2017 11:07
Do you really need the custom panic handling to be used in the debug mode?
Restioson
@Restioson
Dec 16 2017 11:08
Hm? What do you mean?
I'm in debug mode because it tells me when i subtract with overflow
Denis Lisov
@tanriol
Dec 16 2017 11:10
Well, for my software I don't do anything custom for panic as nothing significantly better than just restarting it can be done anyway :-)
Restioson
@Restioson
Dec 16 2017 11:10
Well, I'm doing osdev, so the only other signal of an error I get is a frozen kernel
which, suffice to say, is not terribly useful
I remember booting for the first time in a long time, and was greeted with a completely blank screen, clear of BIOS and GRUB junk, too
"Aww, crap"
Denis Lisov
@tanriol
Dec 16 2017 11:11
Then yes, you need something :-)
Are you working on an OS for a PC or for some microcontroller?
Restioson
@Restioson
Dec 16 2017 11:13
It's a toy kernel for x86
it has been... a learning experience
xD
In all seriousness, I've learned quite a lot from it. I followed intermezzos and some of phil opp's blog os, and some other stuff like bare metal rust
Great resources!
I was a java/kotlin/python dev before I came to rust... so it's a bit, ahem, different
Denis Lisov
@tanriol
Dec 16 2017 11:15
You're testing it in QEMU or some other VM, aren't you?
Restioson
@Restioson
Dec 16 2017 11:16
Yes, I am. I've tested it a couple times (not in my build-run-debug cycle) on real hardware though
Caught a few bugs there
I'm doing it with @gegy1000, who owns a PS/2 keyboard (i know...) so he's been able to test it on that, which was quite useful
Denis Lisov
@tanriol
Dec 16 2017 11:19
The direction I'd probably try is to find out the magic required to attach GDB to QEMU and set a breakpoint in the panic hook so that you could do normal debugging after the panic happens :-)
Restioson
@Restioson
Dec 16 2017 11:19
:thinking:
I have got that working (it's just -S)
At the moment the panic msg is sufficient, but I suppose I could use it to get a backtrace... Thanks!
I'm probably using too many features for my own good... https://i.imgur.com/aMmYDuB.png
I need to wait on const generic/dependent types now, ah shoot
oh, i can remove the bottom 2
blankhart
@blankhart
Dec 16 2017 14:31
@ozkriff @sourcepirate @matklad thank you!
blankhart
@blankhart
Dec 16 2017 14:41
also i am getting duplicated error messages when running cargo and see that this is viewed as a closed issue here rust-lang/cargo#3531 because there are various flags that can turn it off (--lib, --test, -j). i assume these flags also have to go to rustcso i have to figure out the config file or RUSTFLAGS and which one applies to my setup
i am just running a bunch of tests so assume i should start with --test? is there something everyone does in this situation?
Restioson
@Restioson
Dec 16 2017 18:43
Can one coerce a [[T; N]; N] to &mut [&mut [T]]
i can get the first level of coercion
I need a way to create a &mut [&mut [T]]
Aleksey Kladov
@matklad
Dec 16 2017 18:47
That's theorecically not possinle, the arrays would have different sizes. You can get a &mut [T] though: arrays are guranted to be sequential in memory
Restioson
@Restioson
Dec 16 2017 18:54
:/
I don't mind if they have diff sizes
its in lieu of const generics
Aleksey Kladov
@matklad
Dec 16 2017 18:55
Then you'll need to allocate a vector
Hm, wait, you don't need to allocate a vector, becusae the number of elements is a compile-time constant. Yeah, you can allocate &mut [&mut []] on the stack and then fill it in a loop (this quite probably will require some unsafe code though).
Restioson
@Restioson
Dec 16 2017 18:58
I don't have a heap yet
Restioson
@Restioson
Dec 16 2017 19:11
@matklad what do you mean?
Aleksey Kladov
@matklad
Dec 16 2017 19:13
You can't just cast [[T;N] N] to &mut[&mut [T]] because they have completely different runtime representation.
So, you have to create a value of type &mut [&mut [T]] manually.
Restioson
@Restioson
Dec 16 2017 19:32
:+1: thanks!!
Restioson
@Restioson
Dec 16 2017 19:44
@matklad so @nvzqz said i should use ptr::write, so i am doing that now

apparently it's safer --

You may want to use ptr::write instead of assigning like that. From what I've read, it's the "safe" way of initializing mem::uninitialized because LLVM can make weird optimizations otherwise.

Aleksey Kladov
@matklad
Dec 16 2017 19:45
I bet there's a crate for that
Restioson
@Restioson
Dec 16 2017 19:49
ooohhhh
tyy!
This is perrrfect...