These are chat archives for rust-lang/rust

21st
Nov 2018
aohan237
@aohan237
Nov 21 2018 02:22
@scirner_gitlab you can goto actix or actix_web room
laurent bernabé
@loloof64
Nov 21 2018 07:06
Hi everyone, I succeed to use the example for web-view here, but I have a problem when trying to use Rpc calls.
I'll post the snippet in a few
snippet.rs.png
laurent bernabé
@loloof64
Nov 21 2018 07:11
The error
error[E0277]: the trait bound `&'static [Side; 9]: _IMPL_DESERIALIZE_FOR_Board::_serde::Deserialize<'_>` is not satisfied
  --> src/main.rs:49:28                                                         
   |                                                                            
49 | #[derive(Debug, Serialize, Deserialize)]                                   
   |                            ^^^^^^^^^^^ the trait `_IMPL_DESERIALIZE_FOR_Board::_serde::Deserialize<'_>` is not implemented for `&'static [Side; 9]`
What is the safest way to deal with it ? I would like the board to be mutable (via the mutable struct) and to persist along the app.
snippet.rs.png
I am using Serde, SerdeJson and SerdeSerialize as in the official example pointed some times ago
Tim Robinson
@1tgr
Nov 21 2018 07:19
You generally want your Deserialize structs to own their own data, so you would have element: [Side; 9]
laurent bernabé
@loloof64
Nov 21 2018 07:39
Thank you very much. Will try this evening.
So I bet that I should rather keep an instance of the board as static ? I'll try this way.
Zakarum
@omni-viral
Nov 21 2018 08:07
@loloof64 You in no way can deserialize into reference. Except &[u8], &str and &Path
Deserialize is implemented only for owned arrays
So just plane simple [Side; 9] should do
laurent bernabé
@loloof64
Nov 21 2018 08:16
Thank you very much. I understand very well now :smile:
Kelly Thomas Kline
@kellytk
Nov 21 2018 09:31
Is there no debug_panic! available? I'd like code to panic in a certain location only if it's a debug build
Michal 'vorner' Vaner
@vorner
Nov 21 2018 09:33

@kellytk That requirement seems a bit suspicious. So you want your release build to continue in invalid state in such case?

But you can check for the debug_assertions (I think, please check by some docs) config. Somethnig in lines:

if cfg!(debug_assertions) {
  panic!("...");
}
Kelly Thomas Kline
@kellytk
Nov 21 2018 09:36
@vorner Yes. In all cases log an error, in debug panic to ensure I don't miss the error. In production I'll surely notice an error to the logs, however in development with rapid run restarts without the panic it would be easy to miss the error. I'll read about debug_assertions
Michal 'vorner' Vaner
@vorner
Nov 21 2018 09:37
There's this debug_assert! thing that usually gets turned off in release (configurable in profiles in Cargo.toml). So it's not exactly debug vs. release, but correlates quite well. Looking into its source code is probably what you want.
Tim Robinson
@1tgr
Nov 21 2018 09:38
There is debug_assert! and related macros, but there's no debug_panic!
Tim Robinson
@1tgr
Nov 21 2018 09:38
But debug_assert! is just if cfg!(debug_assertions) { assert!(...); }
Kelly Thomas Kline
@kellytk
Nov 21 2018 09:38
cfg!(debug_assertions) apparently drives debug_assert!
nod
Michal 'vorner' Vaner
@vorner
Nov 21 2018 09:38
Yep, so creating your own debug_panic should be easy
Kelly Thomas Kline
@kellytk
Nov 21 2018 09:39
Thank you
melbourne2991
@melbourne2991
Nov 21 2018 10:35
Hi all, when should one use iter vs into_iter?
I get that into_iter returns an iterator, but then what is the purpose of iter?
Tim Robinson
@1tgr
Nov 21 2018 10:36
iter gives you references; the original collection is not dropped and can be reused
melbourne2991
@melbourne2991
Nov 21 2018 10:36
ah perfect thank you
Tim Robinson
@1tgr
Nov 21 2018 10:37
into_iter moves the objects out of the collection instead of returning references; the original collection is dropped and can't be reused
melbourne2991
@melbourne2991
Nov 21 2018 10:37
:thumbsup:
If either can work for a particular use case, should you lean towards using one or the other?
is there a "best practice" as such
Zakarum
@omni-viral
Nov 21 2018 10:58
@trsh HashMap is unordered
trsh
@trsh
Nov 21 2018 11:00
@omni-viral the first print shows the order as I added
But ok
Zakarum
@omni-viral
Nov 21 2018 11:00
Just by chance
serde_json has preserve_order feature
trsh
@trsh
Nov 21 2018 11:00
I have that enabled > serde_json = { version = "1.0", features = ["preserve_order"] }
Zakarum
@omni-viral
Nov 21 2018 11:00
It makes its map to preserve order.
trsh
@trsh
Nov 21 2018 11:00
hmm
Zakarum
@omni-viral
Nov 21 2018 11:01
HashMap is still unordered
To preserve order you need to have the order in the first place
trsh
@trsh
Nov 21 2018 11:03
@omni-viral well the order is there.. i will recheck
Zakarum
@omni-viral
Nov 21 2018 11:03
Nope
trsh
@trsh
Nov 21 2018 11:03
@omni-viral how would you fix the example?
Zakarum
@omni-viral
Nov 21 2018 11:03
When I run your example it prints
{"1": "1", "2": "2"}
Object({"1": String("1"), "2": String("2")})
trsh
@trsh
Nov 21 2018 11:03
I dont understand
Zakarum
@omni-viral
Nov 21 2018 11:04
HashMap yield pairs not in order they are inserted, but in order they are stored
Which depends on hashing and moon phase
trsh
@trsh
Nov 21 2018 11:05
So {"2": "2", "1": "1"} is fine
its order I want
Zakarum
@omni-viral
Nov 21 2018 11:05
Use another map implementation. Which by design preserve order
trsh
@trsh
Nov 21 2018 11:05
Ouh and its different for u
@omni-viral any tips?
It is the one serde-json uses

From IndexMap docs

The key-value pairs have a consistent order that is determined by the sequence of insertion and removal calls on the map. The order does not depend on the keys or the hash function at all.

This is what you want

trsh
@trsh
Nov 21 2018 11:08
lol, serde::ser::Serializeis not implemented forindexmap::IndexMap<std::string::String, std::string::String>
Zakarum
@omni-viral
Nov 21 2018 11:09
¯\_(ツ)_/¯
This message was deleted
Add serde feature to indexmap dependency
99% of crates depend on serde and implement its traits optionally
melbourne2991
@melbourne2991
Nov 21 2018 11:14
Just got my very first stack overflow #feelsgoodman
trsh
@trsh
Nov 21 2018 11:14
@omni-viral how?
for sure I cant do that in playground
will try in my project
melbourne2991
@melbourne2991
Nov 21 2018 11:16
hm I guess searching stack overflow for stack overflow related questions isn't going to be so easy...
trsh
@trsh
Nov 21 2018 11:21
@melbourne2991 you talking to your self?
:D
melbourne2991
@melbourne2991
Nov 21 2018 11:21
I guess so ; - (
haha
It was just a weird moment, i've used GCed languages all my life yet I use stack overflow every other day so actually getting a stack overflow was significant enough for me that I had to share xD
Zakarum
@omni-viral
Nov 21 2018 11:23
@trsh I'm not sure if you can do it on playground
melbourne2991
@melbourne2991
Nov 21 2018 11:24
Actually I take that back, I've experienced stack overflows in GCd languages too
Zakarum
@omni-viral
Nov 21 2018 11:24
But in your Cargo.toml do it like this indexmap = { version = "1.0", features = ["serde"] }
Are there any proc-macro gurus?
I have a proc-macro that uses file content and cargo build just uses cached version if I only touch that file and no code.
trsh
@trsh
Nov 21 2018 11:31
Still getting non impl erros :(
Zakarum
@omni-viral
Nov 21 2018 11:31
Could you post Cargo.toml, relevant code part and error output?
trsh
@trsh
Nov 21 2018 11:32
the trait bound `indexmap::IndexMap<usize, impls::nodes::processor::engine::NodesOutputs>: applications::graphql::_IMPL_DESERIALIZE_FOR_GraphQLData::_serde::Serialize` is not satisfied
  --> manu/src/applications/api/gqls/doc_templates/mod.rs:92:16
   |
92 |     to_string(&to_value(&processing_output).unwrap()).expect("Can pack outputs info into json")
   |                ^^^^^^^^ the trait `applications::graphql::_IMPL_DESERIALIZE_FOR_GraphQLData::_serde::Serialize` is not implemented for `indexmap::IndexMap<usize, impls::nodes::processor::engine::NodesOutputs>`
   |
   = note: required because of the requirements on the impl of `applications::graphql::_IMPL_DESERIALIZE_FOR_GraphQLData::_serde::Serialize` for `&indexmap::IndexMap<usize, impls::nodes::processor::engine::NodesOutputs>`
   = note: required by `serde_json::to_value`
indexmap = { version = "1.0.2", features = ["serde"] }
That graphQL part is because of the Juniper macro
Zakarum
@omni-viral
Nov 21 2018 11:34
Interesting
indexmap named feature serde-1
Found #[cfg(feature = "serde-1")] mod serde; in code
trsh
@trsh
Nov 21 2018 11:36
Rebuilding
Zakarum
@omni-viral
Nov 21 2018 11:37
I made an issue about that to indexmap repo
This is counter-intuitive and not in consistency with ecosystem
trsh
@trsh
Nov 21 2018 11:38
Wait
@omni-viral i can write this now? indexmap = { version = "1.0", features = ["serde-1"] } ?
Zakarum
@omni-viral
Nov 21 2018 11:39
Yes. To enable feature that gates serde implementations
trsh
@trsh
Nov 21 2018 11:40
yeah it would make more sense if there was second serde version
then I use serde-1 or -2 for my indexmap, but also that is bananass
Zakarum
@omni-viral
Nov 21 2018 11:42
If ther will be new serde version then it will replace old one in dependency list
So you wouldn't be able to have serde-1 feature anymore.
trsh
@trsh
Nov 21 2018 11:43
serde-bananas istead
Code is compiling, jahuu
they should add indexMap to std
Zakarum
@omni-viral
Nov 21 2018 11:45
I'd like std to be minimalistic
There is more than one ordered map implementation
I actually don't like futures in std. But it'd be weird if lang item is in crate
Kelly Thomas Kline
@kellytk
Nov 21 2018 11:50
@vorner @1tgr I've merely adapted the Rust macros.rs code to fill the gap https://crates.io/crates/debug_panic It's my first crate so I would appreciate a review ensuring I've not overlooked anything essential
Michal 'vorner' Vaner
@vorner
Nov 21 2018 12:20
Just as a recommendation ‒ it's nice to have an example on the crate-level documentation.
Tim Robinson
@1tgr
Nov 21 2018 12:36
@melbourne2991 if you need to own the elements returned by the iterator, use into_iter. If references to elements is sufficient then use iter.
Michal 'vorner' Vaner
@vorner
Nov 21 2018 12:38
There's also another reason why you might want to use into_iter. If the elements are big, or carry some other resource, with into_iter they get used up and dropped ‒ so you can free some of the resources sooner. With iter if you iterated and dropped the whole vec, you'd be keeping them around for no reason and free everything at once.
Kelly Thomas Kline
@kellytk
Nov 21 2018 12:43
@vorner Done, thanks
F.J. Navarro
@dancespiele
Nov 21 2018 15:31

I try to extract data from request
let person = Json::<CreatePerson>::extract(req);
it has the type

std::boxed::Box<(dyn futures::Future<Error=actix_web::Error, Item=actix_web::Json<enpoints::people::model::CreatePerson>> + '<empty>)>

if I try to call the properties like this:

.send(CreatePerson {
            name: person.name.clone(),
            rich: person.rich,
            super_power: person.super_power,
            genius: person.genius,
        })

I get this error:

no field `name` on type `std::boxed::Box<dyn futures::Future<Error=actix_web::Error, Item=actix_web::Json<enpoints::people::model::CreatePerson>>>`

How can I get the properties of the person? I'm really junior in rust :)

Kelly Thomas Kline
@kellytk
Nov 21 2018 15:35
When I use just .send my code runs fine. However, when I add .into_actor and .then so that I can catch the send if it's dropped, I'm having trouble getting the syntax correct. https://pastebin.com/RnwRvyMj can someone see where the flaw is?
laurent bernabé
@loloof64
Nov 21 2018 19:55
Hi ! Maybe an obvious question : but is there a way to inline svg pictures inside WebView applications (web-view crate) the way it is done for scripts/html and styles like in this example ? Or must I take another approch for svg files ?
Tamir Duberstein
@tamird
Nov 21 2018 20:53
is there a way to run a rust test repeatedly? i.e. i have a flaky test and I want to run it N times without scripting it in my shell
Eli Krenzke
@ekrenzke
Nov 21 2018 20:58
Hmm.. what do you mean by flaky test?
Tamir Duberstein
@tamird
Nov 21 2018 21:03
The test fails non-deterministically
So I need to run it many times to get a failure
Eli Krenzke
@ekrenzke
Nov 21 2018 21:20
Hmm, that’s really interesting and something I have never really had to deal with. Thus, I am probably not the one to provide a good answer, but still interested in working on a solution. Could you just do something like for N in {1..3}; do cargo test --no-fail-fast; done and append any test flags appropriately? I know that’s a script which you don’t want to use, but it’s still a one-liner so I am curious.
Sean Perry
@shaleh
Nov 21 2018 23:42
How does Iterator::foreach() compare to a for loop over a similar block in terms of efficiency, space, etc?
@tamird cargo test foo will run all tests that match 'foo'.
So as Eli suggests, call that in a bash/zsh loop and you are all set.