These are chat archives for rust-lang/rust

2nd
Sep 2018
SaturnTeam
@SaturnTeam
Sep 02 2018 04:04
Hi! Is it the best way to display Vec?
    for i in 0..uf.id.len() {
        println!("{} {}", i, uf.id[i])
    }

And which version is better:
1)

for i in self.id.iter_mut() {
            if *i == p_id {
                *i = q_id
            }
        }

or 2)


        for i in 0..self.id.len() {
            if self.id[i] == p_id {
                self.id[i] = q_id
            }
        }
Kelly Thomas Kline
@kellytk
Sep 02 2018 04:45
What syntax would allow me to concisely assert!() that x is an Option<Myenum> variable with a value of Myenum::Happy? I'm going down a road of (x.is_some()) && (x... == Myenum::Happy) and I suspect there's a better alternative
Zakarum
@omni-viral
Sep 02 2018 05:01
@SaturnTeam I use println!("{:#?}", vec)
Another variant self.id.iter_mut().filter(|id| **id == p_id).for_each(|id| *id == q_id)
SaturnTeam
@SaturnTeam
Sep 02 2018 05:08

@omni-viral

Another variant self.id.iter_mut().filter(|id| **id == p_id).for_each(|id| *id == q_id)

Thanks! Is it slower then for if?

Kelly Thomas Kline
@kellytk
Sep 02 2018 07:13
Can the 11 lines of https://ghostbin.com/paste/yosw8 be condensed into a single line assert!?
Zakarum
@omni-viral
Sep 02 2018 08:06
@SaturnTeam should be exactly the same in release and almost the same in debug
Denis Lisov
@tanriol
Sep 02 2018 08:13
@kellytk If you're ok with small dependencies, using matches this should be just assert_matches!(status, Some(WebSocketStatus::Opened))
Kelly Thomas Kline
@kellytk
Sep 02 2018 08:15
Certainly, thanks @tanriol
Otto Chrons
@ochrons
Sep 02 2018 08:23
then again, it's better to write your program in such way that those "illegal states" are impossible to begin with
Denis Lisov
@tanriol
Sep 02 2018 08:25
@ochrons This may be useful for testing valid states in your tests :-)
Otto Chrons
@ochrons
Sep 02 2018 08:28
yea, tests are a different story :)
Kelly Thomas Kline
@kellytk
Sep 02 2018 08:37
@ochrons This is to test program state, and ensure code I'm writing to avoid invalid states is functioning correctly
Otto Chrons
@ochrons
Sep 02 2018 08:38
use the type system to ensure that as much as possible ;)
Kelly Thomas Kline
@kellytk
Sep 02 2018 08:41
The circumstance I'm working with is a WebSocket connection potentially being closed by the remote peer. I've written code to automatically reconnect if the WebSocket is closed prematurely and my use of assert_matches! will catch if the reconnection code fails. From the sparse summary can you suggest any improvements @ochrons?
Otto Chrons
@ochrons
Sep 02 2018 08:47
without knowing the details, if your code needs x to be Some(Myenum::Happy) why not change it so that it always is Myenum::Happy by having the compiler enforce that for you?
like having
match x {
  Some(Myenum::Happy) => codepath that required x to be happy
  ...
}
Denis Lisov
@tanriol
Sep 02 2018 08:49
@kellytk Is the "reconnection code fails" necessarily a fatal error in your code? Is it what happens if the connection between the two servers drops?
Otto Chrons
@ochrons
Sep 02 2018 08:49
or if you have some kind of state strucutre, have different types for such structure to describe the different states and then again use compiler to enforce that state transitions are always correct
Kelly Thomas Kline
@kellytk
Sep 02 2018 08:53
@tanriol I would think so. The logic is that the frontend app is trying to send a message over a WebSocket connection which has a non-Opened status. That to me is irrational logic which should never be. @ochrons I think that's what I'm doing, with the caveat that I'm dealing with external systems so the frontend alone can't flip the connection status to Opened. For example, if the remote peer Closed it, and reconnection failed due to the server crashing
Denis Lisov
@tanriol
Sep 02 2018 08:54
Is the WebSocket connection open inside your server infrastructure or to one of your clients out in the field?
Also, does this application keep any valuable state/connections/etc.?
Kelly Thomas Kline
@kellytk
Sep 02 2018 08:57
Writing both the frontend and backend, but the context of my question is on the frontend. It's also the frontend which initiates connections to the backend
To your second question I would say that it does although I'm not certain
Denis Lisov
@tanriol
Sep 02 2018 08:58
And the WebSocket connection is the frontend-to-backend one, correct?
Kelly Thomas Kline
@kellytk
Sep 02 2018 08:58
Yes
FWIW I'm using Yew (frontend) and actix web (backend) for the WebSocket connection
Otto Chrons
@ochrons
Sep 02 2018 09:01
in any case, using assert for something like that just feels pretty wrong to me :smile:
Denis Lisov
@tanriol
Sep 02 2018 09:01
Yew is client-side... are we talking about the code that runs in the client's browser, not on your infrastructure?
Kelly Thomas Kline
@kellytk
Sep 02 2018 09:02
Correct
@ochrons I'll continue thinking about ways to eliminate the issue entirely
Denis Lisov
@tanriol
Sep 02 2018 09:05
Then I hope you do have an understanding what happens when the client loses his Internet connection... whether your app should work offline in some way and/or whether it comes back up after the connection is reestablished. I don't know the scope of panic in this code and whether it just makes the page unusable till a manual reload :-)
Kelly Thomas Kline
@kellytk
Sep 02 2018 10:17
I'll likely remove the asserts after the features are developed but if they remain I should probably convert them to debug_ variants as I don't expect users to run into them
Zakarum
@omni-viral
Sep 02 2018 10:31
Assert are for covering logical errors, not desired state of the program
If file is not found it is not logical error.
Index out of bound is logical error. Because index should be validated before usage
Denis Lisov
@tanriol
Sep 02 2018 10:34
Well, that depends :-) if the same program should have just written down that file, it's certainly something like a logical error or something else unfixable :-)
Sylwester Rąpała
@xoac
Sep 02 2018 11:36
There is no quicker way to implement Visitors? https://serde.rs/impl-deserialize.html I have a struct that can be in range 0,32. So I need to write all visti_*?
Denis Lisov
@tanriol
Sep 02 2018 12:20
@xoac Do you mean a number in 0..32?
Sylwester Rąpała
@xoac
Sep 02 2018 16:18
@tanriol yes, exactly
Vitaly
@Virtuos86
Sep 02 2018 16:27
@xoac use macro_rules! to generate a boilerplate code
Denis Lisov
@tanriol
Sep 02 2018 17:38
You can parse it as a number and check after that, IIRC
Denis Lisov
@tanriol
Sep 02 2018 17:48
@xoac Also note that you don't need to implement every method, only the ones that can represent your data :-)
Sylwester Rąpała
@xoac
Sep 02 2018 18:21
so it will be enough to just implement visit_u8? or even visit_i8?
Denis Lisov
@tanriol
Sep 02 2018 18:21
What serialization formats do you intend to use?
Sylwester Rąpała
@xoac
Sep 02 2018 18:37
msgpack
Michal 'vorner' Vaner
@vorner
Sep 02 2018 18:41
I usually cheat by not implementing visitor. I implement deserialize by deserializing into some already existing type (like the i8 or so) and then post-process it.