These are chat archives for rust-lang/rust

19th
Nov 2018
laurent bernabé
@loloof64
Nov 19 2018 07:10
Hi everyone :smile: Does anybody has experience with the webview crate (at least with PureScript/Elm, I mean without Yew) ? Because I've looked at the examples, but I don't know if I need to generate anything or if I must implement all files by hand ? Also do I need to create a local server in order to run such a project ?
฿€¢₭$
@Gee5ive
Nov 19 2018 07:14
@loloof64 what files are you referring to? I haven’t used the crate yet, but I have created 2 different apps with Golang version of WebView which the crate and the Golang package just wrap a c lib if I’m not mistaken, creating a web server is one way to server your files
Interacting via js is another
Injection I mean
aohan237
@aohan237
Nov 19 2018 07:15
why rust use toml rather not yams?
yaml?
laurent bernabé
@loloof64
Nov 19 2018 07:29
@Gee5ive . in fact i thought the dist folder of each app need to be generated. cargo web crate can help me ?
Also will have a closer look at go example
฿€¢₭$
@Gee5ive
Nov 19 2018 07:36
@loloof64 as I said, haven’t used the rust version yet. I’m new to rust, but the common approach in go is to embed all of the static resources into your binary. So you still ship a single binary, haven’t dug into seeing if there is anything like that for rust as of yet, but a quick google search led me to this https://github.com/pyros2097/rust-embed/blob/master/readme.md
You could use it or something similar
laurent bernabé
@loloof64
Nov 19 2018 07:38
Ok thank you very much :smile: I will look at it more closely this evening.
aohan237
@aohan237
Nov 19 2018 08:55
pub fn read_toml_config<'a,T:serde::Deserialize<'a>>(file: String) -> T {
let f = File::open(file).unwrap();
let mut reader = BufReader::new(f);
let mut content = String::new();
reader.read_to_string(&mut content);
let content:&'a str = &content;
let result:T = toml::from_str::<T>(content).unwrap();
result
}
Zakarum
@omni-viral
Nov 19 2018 08:56
Use the power of ```
aohan237
@aohan237
Nov 19 2018 08:56
how to deserialize a toml in a fn that read a file and generate a struct
it shows content has a lifetime issue
why
Toml::from_str should generate new stack?
aohan237
@aohan237
Nov 19 2018 09:05
any guys know about it?
aohan237
@aohan237
Nov 19 2018 09:18
This is the problem of serde, the T must a serde::de::DeserializeOwned ,which toml-rs not mentioned.but in serde 1.0 has implement
Zakarum
@omni-viral
Nov 19 2018 09:22
@aohan237 use T:serde::DeserializeOwned
aohan237
@aohan237
Nov 19 2018 09:23
what’s the different?
Zakarum
@omni-viral
Nov 19 2018 09:23
No lifetime issues
aohan237
@aohan237
Nov 19 2018 09:24
Yeah ...
i have find it out..
Zakarum
@omni-viral
Nov 19 2018 09:24
You see this line let content:&'a str = &content; has no sense
aohan237
@aohan237
Nov 19 2018 09:25
of course..
just confused and going crazy….
Zakarum
@omni-viral
Nov 19 2018 09:25
serde::DeserializeOwned is basically for<'a> serde::Deserialize<'a>
aohan237
@aohan237
Nov 19 2018 09:25
the document isn’t enought….
Zakarum
@omni-viral
Nov 19 2018 09:25
So you can put any lifetime you want
And every type that implements for<'a> serde::Deserialize<'a> implements serde::DeserializeOwned
aohan237
@aohan237
Nov 19 2018 09:29
Yeah,thanks
still,the document is not enough...
Denis Lisov
@tanriol
Nov 19 2018 10:57
This is not toml-specific. Either you keep the file data (content in your example) as long as you keep the resulting structure, or you use the DeserializeOwned bound to indicate that your structure does not borrow from that data. For more detail see the page on lifetimes in the serde docs.
Sylwester Rąpała
@xoac
Nov 19 2018 13:46

if I have an

eum Error {
MyError {code},
Other
}

how can I match just by Error::MyError {_no_meter} ? So I am mostly interested in kind not details

Tim Robinson
@1tgr
Nov 19 2018 13:48
if let Error::MyError { code } = e {
    println!("{:?}", code);
}
Sylwester Rąpała
@xoac
Nov 19 2018 13:49
I want to ignore internal state
I don't wanna check what details are in Error::MyError {}
Tim Robinson
@1tgr
Nov 19 2018 13:50
if let Error::MyError { code: _code } = e {
Or
impl Error {
    fn is_my_error(&self) -> bool {
        if let &Error::MyError { code: _code } = self {
            true        
        } else {
            false
        }
    }
}
Sylwester Rąpała
@xoac
Nov 19 2018 13:52
so the answer is I can't ignore what internal details are :(?
it's looks like custom_error has a big flaw with this
Tim Robinson
@1tgr
Nov 19 2018 13:53
Right, if you match on an enum case, you have to look at all the fields, even if you match them into ignored variables like _code
See Option::is_some and Option::is_none
Sylwester Rąpała
@xoac
Nov 19 2018 13:55
I know what u mean. I just create my error with https://github.com/lovasoa/custom_error and I wnated to match them. Match is not so bad, but it's imposibble
      match e {
>>        Error::JsonSerError{source} => RetryPolicy::ForwardError(e),
>>        Error::InternalClientErr{err_msg} => RetryPolicy::ForwardError(e),
          _ => RetryPolicy::WaitRetry(Duration::from_secs(10)),
      }
give me:
error[E0382]: use of partially moved value: `e`                                                                                                                                                                                              
  --> src/main.rs:23:66                                                                                                                                                                                                                      
   |                                                                                                                                                                                                                                         
23 |         Error::JsonSerError{source} => RetryPolicy::ForwardError(e),                                                                                                                                                                    
   |                             ------ value moved here              ^ value used here after move                                                                                                                                           
   |                                                                                                                                                                                                                                         
   = note: move occurs because the value has type `serde_json::Error`, which does not implement the `Copy` trait                                                                                                                             

error[E0382]: use of partially moved value: `e`                                                                                                                                                                                              
  --> src/main.rs:24:72                                                                                                                                                                                                                      
   |                                                                                                                                                                                                                                         
24 |         Error::InternalClientErr{err_msg} => RetryPolicy::ForwardError(e),                                                                                                                                                              
   |                                  ------- value moved here              ^ value used here after move                                                                                                                                     
   |                                                                                                                                                                                                                                         
   = note: move occurs because the value has type `std::string::String`, which does not implement the `Copy` trait
Tim Robinson
@1tgr
Nov 19 2018 13:56
Needs more &
match &e {
Sylwester Rąpała
@xoac
Nov 19 2018 13:57
dosen't work. I can't move sth what is borrowed
mayby NNL will check change this
Denis Lisov
@tanriol
Nov 19 2018 13:57
@xoac MyError { .. }
Sylwester Rąpała
@xoac
Nov 19 2018 13:58
@tanriol <3
Tim Robinson
@1tgr
Nov 19 2018 13:58
Thanks @tanriol
Sylwester Rąpała
@xoac
Nov 19 2018 13:59
And this also resolve problem with partial moving.. Why?
Denis Lisov
@tanriol
Nov 19 2018 14:02
This pattern does not move any fields, just checks the discriminant. There's a difference between "move out and drop" and "don't touch". Not sure whether this should be ok without NLL, but it definitely should with.
Lyle Mantooth
@IslandUsurper
Nov 19 2018 15:09
You can also solve partial moving with Struct { ref foo, .. }when you actually do need to know what's inside the struct.
octave99
@octave99
Nov 19 2018 16:27
Hi, I am trying to reuse the TcpStream connection and wish to read the data continuously so using future::loop_fn like so
let client_loop = future::loopfn((), || {
connection.and_then(|c| {
print!("Inside loop");
Ok(Loop::Break(()))
})
});
But receiving error: cannot move out of captured outer variable in an FnMut closure
Tim Robinson
@1tgr
Nov 19 2018 16:28
Yep, that's the purpose of the () in your code above
The stuff you want to capture (and modify) is passed as a parameter there
Then you modify it inside the loop body
And return the new values to be passed into the next iteration of the loop, via Loop::Continue
octave99
@octave99
Nov 19 2018 16:29
You mean, something like let client_loop = future::loopfn((connection), || {
?
Tim Robinson
@1tgr
Nov 19 2018 16:30
let client_loop = future::loop_fn(connection, |mut connection| {
    // ...here put your code that uses connection...
    Loop::Continue(connection)
});
octave99
@octave99
Nov 19 2018 16:31
connection variable can't be future?
Because its complaining for Future trait
Tim Robinson
@1tgr
Nov 19 2018 16:33
It's an ordinary variable, it can be anything
octave99
@octave99
Nov 19 2018 16:33
Ok, Thanks. Let me try more carefully then. Thanks for your help
Tim Robinson
@1tgr
Nov 19 2018 16:33
Example with a stream: (not a future, but similar)
octave99
@octave99
Nov 19 2018 16:35
Yeah, looks relevant. Let me try with this example, it should work. Thanks again.
octave99
@octave99
Nov 19 2018 17:59
@1tgr Its working perfectly!
Does future::loop_fn require impl Stream? for polling? Or its not required when stream is read within future::loop_fn?
Here is what I am currently doing?
con.reader.read_until(b'\n', &mut buf).and_then(|r| {
print!("{:?}\n", String::from_utf8_lossy(&buf));
Ok(())
});
octave99
@octave99
Nov 19 2018 18:05
'''
reader is BufReader::new(stream);
'''
Tim Robinson
@1tgr
Nov 19 2018 18:08
What is your stream variable?
octave99
@octave99
Nov 19 2018 18:08
TcpStream::connect(&addr).and_then(|stream| {
let reader = BufReader::new(stream.try_clone().unwrap());
let writer = BufWriter::new(stream);
ok(Connector { reader, writer })
})
Connector is a struct with BufReader<TcpStream> and BufWriter<TcpStream>
so stream is tokio TcpStream
Tim Robinson
@1tgr
Nov 19 2018 18:10
Ah I see
No loop_fn does not require Stream
octave99
@octave99
Nov 19 2018 18:11
And it would still be non-blocking read/write?
Tim Robinson
@1tgr
Nov 19 2018 18:11
In fact, the first thing my example did was convert a Stream to Future using stream.into_future(), so you will have less code than me
Yes a Stream is just a complicated Future
octave99
@octave99
Nov 19 2018 18:13
Great! Thank you for clarifying. Let me try with into_future as well then, just for learning sake...
David Raifaizen
@craftytrickster
Nov 19 2018 19:07
I have a multi-crate github repo, and I would like to generate cargo doc, that unifies all of the crate links into a single index.html
Is there any convenient way to do that, or will I have to make a fake lib that depends on the individual crates, and then cargo doc on that ?