These are chat archives for rust-lang/rust

11th
Feb 2018
Sarp Güney Başaraner
@sgbasaraner
Feb 11 2018 09:25
@red75prime why is that?
Denis Lisov
@tanriol
Feb 11 2018 09:31
@sgbasaraner Traits can't be implemented for arrays of arbitrary size in current Rust, so most implementations are only up to length 32 arrays or so. Creating the array actually will work (it needs Copy, which is implemented with special compiler magic along with Clone), but some other operations may fail.
Sarp Güney Başaraner
@sgbasaraner
Feb 11 2018 09:40
@tanriol so, is there a good way to represent something like a matrix currently? Apart from two dimensional, limitless vectors of course.
Maciej Gorywoda
@makingthematrix
Feb 11 2018 09:47
@sgbasaraner You can try the nalgebra crate.
Sarp Güney Başaraner
@sgbasaraner
Feb 11 2018 10:18
@makingthematrix Thanks, I'll check that out. Nice username btw
Maciej Gorywoda
@makingthematrix
Feb 11 2018 10:19
:D
Adel Prokurov
@aprokurov
Feb 11 2018 10:27
Guys, how i can enable auto-complete in Gnome Builder? I'm using Gnome Builder nightly and nightly rust
apiraino
@apiraino
Feb 11 2018 10:29
@aprokurov is this of any help https://github.com/deikatsuo/bracer ? (just googled it, I don't use Gnome Builder)
Adel Prokurov
@aprokurov
Feb 11 2018 10:30
Thanks!
apiraino
@apiraino
Feb 11 2018 10:31
np
Maciej Gorywoda
@makingthematrix
Feb 11 2018 10:39
btw, do you know why so many crates depend on nightly? I've just started a small project and I'm already almost forced to use it, because every time I want to add a crate it comes with "this feature needs nightly".
Like, I had to switch to nightly to use clippy. That's ok, because it won't be a part of my code, but I fear at some point I'll find myself in a situation when I will have to use nightly to compile my own code too.
I feel that nightly should be only for testing new Rust features, not for development of third party libraries.
Michal 'vorner' Vaner
@vorner
Feb 11 2018 10:52
@makingthematrix There are crates that do the testing, these are the nightly only. Most of the time there are stable alternatives, but they may be less comfortable or lack some features.
It depends on what you want to do.
Aleksey Kladov
@matklad
Feb 11 2018 10:52
@makingthematrix what crates are you using? At least in my experience, almost everything does work on stable =)
Daniel Bischof
@dbischof90
Feb 11 2018 11:02
G'day folks
Maciej Gorywoda
@makingthematrix
Feb 11 2018 11:11
@matklad I'm just starting, so it's only a feeling that too often I see these notes "works on nightly". If I'm wrong, that's cool :) So far, I know clipp needs nightly, quickcheck macros too, and some features in rand. Fortunately none of it needs to be in my production code, but a I'm a bit worried.
Aleksey Kladov
@matklad
Feb 11 2018 11:15

I think these are not the reasons to worry :)

The fact the clippy is nightly only is very annoying, but, as you've said, it does not actually affect your code at all.

quickcheck is perfectly usable on stable! nightly will give you a tiny bit better syntax, but that's it. By the way, checkout the proptest crate: it's slighly more powerful than quickcheck: https://github.com/AltSysrq/proptest

And I think rand only needs nightly features to support i128/u128 types, which are nightly only, and which you probably don't need unless you are into cryptography :)

@makingthematrix I centrally hear "Rust is only usable with nightly" sentiment a lot on various forums, but I haven't found this to be true in practice. At least I have been using stable since I've started with rust, and have never felt a need to switch to nightly to get some library. The only exception to this is that I am using beta right now because it has incremental compilation enabled =P
apiraino
@apiraino
Feb 11 2018 11:19
^^^ or rustfmt (afaics). is the nightly still needed to compile it?
Aleksey Kladov
@matklad
Feb 11 2018 11:19
@apiraino rustup component add rustfmt-preview
apiraino
@apiraino
Feb 11 2018 11:20
will do! thanks!
Adel Prokurov
@aprokurov
Feb 11 2018 11:26
@apiraino i'm installed bracer, don't get autocomplete:(
apiraino
@apiraino
Feb 11 2018 11:28
@aprokurov not sure on how to help. Does racer alone works?
Did you test it with something like:
Adel Prokurov
@aprokurov
Feb 11 2018 11:28
@apiraino racer works in VSCode
apiraino
@apiraino
Feb 11 2018 11:29
ok
looks like you're alone, though: deikatsuo/bracer#1
Maciej Gorywoda
@makingthematrix
Feb 11 2018 11:35
thanks for reassurance, folks :)
Daniel Bischof
@dbischof90
Feb 11 2018 13:20
Assume I have a main thread M and several dedicated threads A, B, ... . Each of these has a certain special task and M only distributes it to specific workers. Assume a is a new task for A and is about to be sent from M to A but A is still busy. What happens then?
Kelly Thomas Kline
@kellytk
Feb 11 2018 13:27
How do I intercept an up-arrow keypress? It shows up in my CLI tool's input as "^[[A". I'd like to implement a basic input history feature
Aleksey Kladov
@matklad
Feb 11 2018 13:28

@dbischof90 It depends on the channel you are using to send the tasks. Possible options are

1) the channel has a buffer with some space. Then, the task is enqueued and send on M's side completes immediately and successfully.
2) the buffer is full. In this case, possible actions are a) block M until there's a space in the buffer b) immediately return an error to M

@kellytk not an answer to your question, but you might want to look at https://github.com/kkawakam/rustyline :)
Daniel Bischof
@dbischof90
Feb 11 2018 13:30
Ah, so there are buffers built-in?
Aleksey Kladov
@matklad
Feb 11 2018 13:31
@dbischof90 it really depends on the channel you are using :-) There are unbufered (rendezvous), fixed-sized buffered and unlimitited-buffer channel flavors.
Daniel Bischof
@dbischof90
Feb 11 2018 13:33
Multithreading is the first topic that starts to dilute rust a little bit - there seem to be quite some possibilities. There's std::thread that implements this, I think tokio wraps somewhere some cpu_pools which I haven't figured out whether they are based on the std module or not... although they focus on async.
@matklad Haven't encountered these, it's just the next topic on the learing-list ;) I was just wondering whether there are at all things like buffers are in place.
Aleksey Kladov
@matklad
Feb 11 2018 13:37
@dbischof90 yeah, there quite a few options in the ecosystem :-) For channels specifically, I suggest taking a look at crossbeam-channel (a faster and more powerful alternative to std::sync::channel): https://docs.rs/crossbeam-channel/0.1.2/crossbeam_channel/#types-of-channels
Daniel Bischof
@dbischof90
Feb 11 2018 13:52
I'll take a look :)
Kelly Thomas Kline
@kellytk
Feb 11 2018 15:04
What is Rust's convention for struct accessors, specifically setters? strct.set_property(value)? I would like to encapsulate the machinery code of a RwLock wrapping a property into the setter for that property
Aleksey Kladov
@matklad
Feb 11 2018 15:06
@kellytk
struct Spam { 
  ham: Ham
}

impl Spam {
  fn ham(&self) -> &Ham { &self.ham }
  fn ham_mut(&mut self) -> &mut Ham { &mut self.ham }
  fn set_ham(&mut self, ham: Ham) { self.ham = ham }
}
However, the usual advice of "don't add getters and setters (and especially setters) unless absolutely necessary, prefer to create a meaningful abstraction instead" stands :)
Kelly Thomas Kline
@kellytk
Feb 11 2018 15:07
_mut for mutable qualification is interesting
Would you agree that locking code qualifies as a "meaningful abstraction"?
Aleksey Kladov
@matklad
Feb 11 2018 15:10
It may or may not be the case: it's really depends on the particular use-case, and it's hard to give reliable advice without knowing intricate details :)
Kelly Thomas Kline
@kellytk
Feb 11 2018 15:20
Thanks @matklad
Andy Grove
@andygrove
Feb 11 2018 16:41
I'm trying to use and_then and map_err with futures but I'm confused ... can someone tell me why this doesn't work?
 fn register(self) -> FutureResult<(Self, bool), Error> {

        let key = format!("/datafusion/workers/{}", self.uuid);

        kv::set(&self.etcd, &key, &self.bind_address, Some(10))
            .and_then(|etcd_response| {
                println!("Registered OK: {:?}", etcd_response);
                ok((self,false))
            })
            .map_err(|_| Error::from(ErrorKind::NotFound))
I get the error expected structfutures::FutureResult, found structfutures::MapErr``
before the map_err the error type is Vec<etcd::Error> and I'm using map_err to map that to the expected type of Error
I'm clearly missing something here though
Denis Lisov
@tanriol
Feb 11 2018 16:49
The type returned by map_err is MapErr, while you've specified that the function returns a different type, FutureResult
If you're on stable Rust, I'd recommend returning a Box<Future> instead.
Andy Grove
@andygrove
Feb 11 2018 16:53
hmm trying that out and it changes the error to "expected struct std::boxed::Box, found struct futures::AndThen"
my function is declared as "fn register(self) -> Box<Future<Item=(Self,bool),Error=Error>> {"
so I still need to call map_err to make sure I return the correct error type, right?
Denis Lisov
@tanriol
Feb 11 2018 16:54
Wrap your final expression with Box::new(...). Yes, you do still need the map_err too.
Andy Grove
@andygrove
Feb 11 2018 16:55
ahh, that's better .. thanks
wow, fully working code !
This message was deleted
thanks!
Dylan DPC
@Dylan-DPC
Feb 11 2018 17:28
I need to use impl Foo as a return type of a function in trait Bar but that isn't possible. What is the alternative I have to this?
Denis Lisov
@tanriol
Feb 11 2018 17:29
@Dylan-DPC Box<Trait>, maybe?
Dylan DPC
@Dylan-DPC
Feb 11 2018 17:30
i was thinking of it. Does that mean i can return Box<something that impl Trait> and that will still comply?
Denis Lisov
@tanriol
Feb 11 2018 17:30
Exactly.
Dylan DPC
@Dylan-DPC
Feb 11 2018 17:30
oh was doubtful on that. Thanks :smile:
@tanriol that didn't work. It says expected Trait found Struct (which impl Trait)
Denis Lisov
@tanriol
Feb 11 2018 17:38
Is this the only error? Could you copy it here if so?
Dylan DPC
@Dylan-DPC
Feb 11 2018 17:41
ah got it working now
i was trying to see if i could return Box<FooStruct> and that didn't work
so used Box<Trait> as the return type in the implementation
Gwen Lofman
@glfmn
Feb 11 2018 17:46
I'm using rand as a dependency solely for doing some quickcheck tests, but I was not using it as a dev-dependecy. I'm moving it to be a dev dependency, and I put the import behind a #[cfg(test)] attribute, but I'm having an issue: I use rand_derive to dervie rand for some of my enum types, but I can't figure out how to derive rand only on test builds. Is this not possible?

I tried doing something like:

#[cfg(test)]
#[derive(...,rand)]
#[cdf(not(test))]
#[derive(...)]

Where ... is all of the other traits I derive, but this does not work, as the result is just that the associated type is not complied in either test or regular builds.

The compile time for rand_derive is especially hefty so I'd definitely like to make it a dev dependecy
Denis Lisov
@tanriol
Feb 11 2018 17:49
Try #[cfg_attr(test, derive(rand))]
Gwen Lofman
@glfmn
Feb 11 2018 17:50
Ah! That makes more sense, will do
Fixed my issue, thanks @tanriol!
Andy Grove
@andygrove
Feb 11 2018 20:49
so I'm using hyper for a web server and I don't know how to manage state ... to handle requests I have to provide a function with signature fn(chunk: Chunk) -> Response so I can't use a function that's part of a struct impl because of the extra self parameter.
In scala I would use currying to get around this but not sure of best practice in Rust
Denis Lisov
@tanriol
Feb 11 2018 20:54
You can implement Service manually instead, or use a closure.
Nikolay Kim
@fafhrd91
Feb 11 2018 20:57
@andygrove you can try actix it has state management built in
Andy Grove
@andygrove
Feb 11 2018 21:14
thanks