These are chat archives for rust-lang/rust

3rd
Mar 2018
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:28
For some reason an extremely simple algorithm that I wrote gives a stack overflow
#[derive(Debug)]
pub enum MatchCase {
    Match = 0,
    Ignore = 1
}

impl MatchCase {
    pub fn from(i: u32) -> MatchCase {
        if i % 2 == 0 {
            MatchCase::Match
        } else {
            MatchCase::Ignore
        }
    }
}
And in test, I'm just doing this:
assert_eq!(config::MatchCase::from(1u32), config::MatchCase::Ignore);
That test keeps failing, showing this as the reason:
thread 'test::match_case_casts' has overflowed its stack
Andrey Lesnikov
@ozkriff
Mar 03 2018 10:31
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:31
idk
It's showing that error
Andrey Lesnikov
@ozkriff
Mar 03 2018 10:32
are you sure that this is the only test that is running? maybe some other code gives the error?
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:32
Positive
There are other tests
But the name is given
I've named this test match_case_casts
The error shows match_case_casts
Michal 'vorner' Vaner
@vorner
Mar 03 2018 10:33
That is strange. Can you look at a core dump in the debuger, or something?
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:33
Uhh I'm dumb and new to Rust, so how do I do that, exactly?
I did this as a part of my own modification of the I/O program in the rust book
Michal 'vorner' Vaner
@vorner
Mar 03 2018 10:33
The same way you'd in C or C++, basically. What OS? If linux, I could walk you through that.
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:34
Windows 10
Should I set RUST_BACKTRACE = 1?
Michal 'vorner' Vaner
@vorner
Mar 03 2018 10:34
Then someone else will have to help with that, I've never used that os and I guess generating core dumps is different there.
You could, but I don't think it'd help. Stack overflow aborts, doesn't panic.
(maybe simply running in debuger would help too, instead of a core dump ‒ that depends on what IDE you use)
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:36
VSCode
I've not set up LLDB. I have no idea how to do that
If you want it, I can upload my source on GitHub
Done
Andrey Lesnikov
@ozkriff
Mar 03 2018 10:49
the problem is your PartialEq impl
it's recursive
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:50
Oh
But I've ignored it
In the tests
Andrey Lesnikov
@ozkriff
Mar 03 2018 10:50
#43543 0x00005555555666d3 in <minigrep::config::MatchCase as core::cmp::PartialEq>::eq (self=0x7ffff69fe6af, rhs=0x5555555fc966) at src/config.rs:18                                                          
#43544 0x000055555556dc43 in core::cmp::impls::<impl core::cmp::PartialEq<&'b B> for &'a A>::eq (self=0x7ffff69fe4c0, other=0x7ffff69fe4c8) at /checkout/src/libcore/cmp.rs:912                               
#43545 0x00005555555666d3 in <minigrep::config::MatchCase as core::cmp::PartialEq>::eq (self=0x7ffff69fe6af, rhs=0x5555555fc966) at src/config.rs:18                                                          
#43546 0x000055555556dc43 in core::cmp::impls::<impl core::cmp::PartialEq<&'b B> for &'a A>::eq (self=0x7ffff69fe520, other=0x7ffff69fe528) at /checkout/src/libcore/cmp.rs:912                               
#43547 0x00005555555666d3 in <minigrep::config::MatchCase as core::cmp::PartialEq>::eq (self=0x7ffff69fe6af, rhs=0x5555555fc966) at src/config.rs:18                                                          
#43548 0x000055555556dc43 in core::cmp::impls::<impl core::cmp::PartialEq<&'b B> for &'a A>::eq (self=0x7ffff69fe580, other=0x7ffff69fe588) at /checkout/src/libcore/cmp.rs:912                               
#43549 0x00005555555666d3 in <minigrep::config::MatchCase as core::cmp::PartialEq>::eq (self=0x7ffff69fe6af, rhs=0x5555555fc966) at src/config.rs:18                                                                                                                                                                                                                                                
#43550 0x000055555556dc43 in core::cmp::impls::<impl core::cmp::PartialEq<&'b B> for &'a A>::eq (self=0x7ffff69fe5e0, other=0x7ffff69fe5e8) at /checkout/src/libcore/cmp.rs:912                               
#43551 0x00005555555666d3 in <minigrep::config::MatchCase as core::cmp::PartialEq>::eq (self=0x7ffff69fe6af, rhs=0x5555555fc966) at src/config.rs:18                                                          
#43552 0x000055555556dc43 in core::cmp::impls::<impl core::cmp::PartialEq<&'b B> for &'a A>::eq (self=0x7ffff69fe640, other=0x7ffff69fe648) at /checkout/src/libcore/cmp.rs:912                               
#43553 0x00005555555666d3 in <minigrep::config::MatchCase as core::cmp::PartialEq>::eq (self=0x7ffff69fe6af, rhs=0x5555555fc966) at src/config.rs:18
#43554 0x0000555555566efb in minigrep::test::match_case_casts () at src/lib.rs:30
#43555 0x0000555555578ff2 in test::run_test::{{closure}} () at libtest/lib.rs:1453
#43556 core::ops::function::FnOnce::call_once () at /checkout/src/libcore/ops/function.rs:223
#43557 <F as alloc::boxed::FnBox<A>>::call_box () at /checkout/src/liballoc/boxed.rs:788
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:50
Okay
Thanks
Andrey Lesnikov
@ozkriff
Mar 03 2018 10:51
(I've used rust-gdb ./target/debug/minigrep-3ebc3df421da625e on tests binary)
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:51
Anyway, is there a way I can implement the PartialOrd more efficiently?
Like, I've assigned values to the keys in the enum
Andrey Lesnikov
@ozkriff
Mar 03 2018 10:52
$ g diff
diff --git a/src/config.rs b/src/config.rs
index f2c2df9..2ffbe1c 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,5 +1,4 @@
 use std::convert::From;
-use std::cmp::PartialEq;

 pub struct Config {
     pub query: String,
@@ -7,18 +6,12 @@ pub struct Config {
     pub match_case: MatchCase
 }

-#[derive(Debug)]
+#[derive(Debug, PartialEq)]
 pub enum MatchCase {
     Match = 0,
     Ignore = 1
 }

-impl PartialEq for MatchCase {
-    fn eq(&self, rhs: &MatchCase) -> bool {
-        self == rhs
-    }
-}
-
 impl From<u32> for MatchCase {
     fn from(i: u32) -> MatchCase {
         if i % 2 == 0 {
^ works fine this way
Niranjan A Kartha
@n-kartha
Mar 03 2018 10:53
Thanks
The only other language I've seen enums in is C#
And I can directly cast an enum value to int in it
Not that that's relevant here
Andrey Lesnikov
@ozkriff
Mar 03 2018 11:02
Rust's enums are a little bit more complicated and powerful
Ingvar Stepanyan
@RReverser
Mar 03 2018 11:04

And I can directly cast an enum value to int in it

FWIW you can do that for simple enums in Rust too (although you likely don't need that most of the time, unless you're in business of encoding/decoding enums into binary data)

Andrey Lesnikov
@ozkriff
Mar 03 2018 11:07
(note that you need to use enum_primitive or something alike if you want to do int->enum cast)
Ingvar Stepanyan
@RReverser
Mar 03 2018 11:15
Probably better to use one of the derive crates nowadays, but yeah.
Andrey Lesnikov
@ozkriff
Mar 03 2018 11:21
btw, what is a good derive crate for this nowadays?
Ingvar Stepanyan
@RReverser
Mar 03 2018 11:22
Yeah I think that's the one I used
Although TBH for small enums (or those I autogenerate from custom macro already) I often ended up with just manual match+transmute instead of external crate :D
Andrey Lesnikov
@ozkriff
Mar 03 2018 11:24
Ingvar Stepanyan
@RReverser
Mar 03 2018 11:26
I guess you don't need assert in from_int given that you're indexing later anyway
As bound check will do the same assertion
Andrey Lesnikov
@ozkriff
Mar 03 2018 11:28
agree, but I just wanted to clarify the intention a little bit more
Ingvar Stepanyan
@RReverser
Mar 03 2018 11:33
offtop: is it intentional that North and South are not in Dir?
Andrey Lesnikov
@ozkriff
Mar 03 2018 11:34
of course, it's a hex grid :)
Kelly Thomas Kline
@kellytk
Mar 03 2018 21:34
:-D
Daniel Bischof
@dbischof90
Mar 03 2018 22:36
Evening guys
Out of interest, has someone tried to use Rust on OpenWRT/LEDE?
Kelly Thomas Kline
@kellytk
Mar 03 2018 22:45
As a tangential thought, a pared-down Redox might be useful as a Rust-based generalized network appliance platform; I'm thinking of the sort suitable for a pfSense alternative/firewall and your wireless router application @dbischof90. Those applications alone would have the side-benefit of maturing Redox's security and network performance, and being written in Rust they could be run portably
Daniel Bischof
@dbischof90
Mar 03 2018 22:48
I haven't tried Redox yet
But it's on the list ;)