These are chat archives for rust-lang/rust

26th
Sep 2018
Ichoran
@Ichoran
Sep 26 2018 00:40
If you need to test file i/o in your unit tests, is there a convention for where to do the i/o? Case 1--if you need a file as input; case 2--if you just need scratch space?
It probably shouldn't be tests since that is where cargo looks for test source code, not output.
Kelly Thomas Kline
@kellytk
Sep 26 2018 01:04
@tanriol Thank you for suggesting to include 'through' one lib to the other. It builds now
Ichoran
@Ichoran
Sep 26 2018 01:32
Why does eq but not == work for Iterator? Is this basically a HKT issue regarding the Eq trait?
matrixbot
@matrixbot
Sep 26 2018 02:52
bspeice Is there any way of getting an owned pointer in #[no_std] stable Rust? I currently have a recursive type definition that blows up, but I can't prove ownership using just the reference (&) operator.
Thiez
@Thiez
Sep 26 2018 06:19
@Ichoran
PartialEq and Eq simply aren't implemented for Iterator.
Thiez
@Thiez
Sep 26 2018 06:25
This sort of makes sense - iterators are allowed to be infinite and you probably wouldn't want to introduce an
Thiez
@Thiez
Sep 26 2018 06:32
Infinite loop by accident. Also the comparison traits take their arguments by immutable reference so they can't call Iterator::next
Sylwester Rąpała
@xoac
Sep 26 2018 08:36

@tanriol I need to call requests in some periods. For example 3request every first once 5s, second once 15s third once 30s. So I have a struct:

struct IntervalRequest {
   req: Req,
  interval: timer::Interval,
}

And then a

struct  Periods {
   inner: Vec<IntervalRequest>,
}

this implement Stream that return a Vec<Req> that should be done at some time.

Then I have a second struct ReqMaker with Vec<Box<Future<item = Response ..>> where I am checking future is done.

HerrDerb
@HerrDerb
Sep 26 2018 09:46
I've just started getting into rust. I have a question about use: As I've read use is comparable to import of java. It's not necessary but it will import all artefacts of the given path, which keeps the code cleaner.
How would I write this code without use then?
extern crate rand;
use rand::Rng;

fn main() {
    let random = rand::thread_rng().gen_range(0, 100);
    println!("Random {}", random);
}
red75prime
@red75prime
Sep 26 2018 10:09
use makes identifiers accessible without full path, it's not really an import. In your code you can just remove use rand::Rng;, everything was imported by extern crate already.
use rand::Rng; allows you to write Rng instead of ::rand::Rng and that's pretty much of it.
HerrDerb
@HerrDerb
Sep 26 2018 10:11
Thanks , this is what I've expected. The thing is when I remove use rand::Rng; from the code above, I'll get the following error:
  |
4 |     let random = rand::thread_rng().gen_range(0, 100);
  |                                     ^^^^^^^^^
  |
  = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:
  |
3 | use rand::Rng;
  |`
red75prime
@red75prime
Sep 26 2018 10:13
Ouch. I forgot about traits. You need to use them to bring their methods into the scope.
HerrDerb
@HerrDerb
Sep 26 2018 10:15
So extern crate rand; imports the identifiers (which use would do too) , but use also enables the usage for the current scope? I guess I need to read a bit more about traits :)
red75prime
@red75prime
Sep 26 2018 10:15
You need to use rand::Rng::gen_range(&mut rand::thread_rng(), 0, 100); otherwise
HerrDerb
@HerrDerb
Sep 26 2018 10:16
If not using use I need to hand over the valid scope to the function too, for it able to work.
Thanks for your help, I dig in a bit more into the trait and scope topic.
Denis Lisov
@tanriol
Sep 26 2018 11:46
@xoac Why not just a stream per interval, forwarding into an MPSC sender if something needs to receive the results of these futures? Do you need to synchronize them in some strange way?
Sylwester Rąpała
@xoac
Sep 26 2018 12:15
Most request will be problaby at the same time. It's modbus request so I want to read addres 0x5000 and 0x5002 and 0x5004 every 5s and 0x6000 every 9s..
@tanriol This is just example. I am afraid that insert this in vec will cause a lot of unnecessey poll() calls
Denis Lisov
@tanriol
Sep 26 2018 12:19
Honestly, I don't even understand why you need these vectors at all... why not spawn separate tasks like "with 5s interval => read 0x5000 => send to ..." for each address?
Sylwester Rąpała
@xoac
Sep 26 2018 13:31

@tanriol Hym.. I don't know how many intervals there will be it will be loaded from json config file. But You are right better will be sorted queue. Ant this already exist (https://docs.rs/tokio/0.1.8/tokio/timer/struct.DelayQueue.html) just when expired add new at the end :)

But for every element returned from this I just will create a request in for_each.

soo typicaly me do sth what exist and do it worser :)
Sylwester Rąpała
@xoac
Sep 26 2018 13:57
@tanriol Do u agree now it's proper solution? Or should I change sth?
Denis Lisov
@tanriol
Sep 26 2018 14:18
What do you need to do with the poll results?
Sylwester Rąpała
@xoac
Sep 26 2018 14:19
I will send request to I/O and wait for response. Response will be writen to buffered json and send once a 30min to other system
Sylwester Rąpała
@xoac
Sep 26 2018 14:37
@tanriol by I/O I mean modbus
Denis Lisov
@tanriol
Sep 26 2018 14:44
Well, I'd probably use something like this (not functional, just a proof-of-concept).
Michael(LAI)
@Michael-Lfx
Sep 26 2018 15:03
I have googled a lot. But I can't find something useful for implementing Aspect Oriented Programming using Rust. Could you provide suggestions?
Ichoran
@Ichoran
Sep 26 2018 15:05
@Thiez - Okay, the second argument makes sense: comparisons are on borrowed values, and Iterator::eq is destructive, so that won't work. But the ergonomics are not ideal; normally x == y and x.eq(y) are the same. I guess I just need to learn that they're not. The alternative would I suppose be something like we have for closures (Eq, EqOnce, etc.).
Denis Lisov
@tanriol
Sep 26 2018 15:34
@Michael-Lfx Have not seen any. Any particular problems in mind?
Michael(LAI)
@Michael-Lfx
Sep 26 2018 15:35
logging, debug, function cost record, etc.
D'Silva
@evnix
Sep 26 2018 22:29
what would be a good format to store a priority queue on a file so that it is easy to insert/delete?
Ichoran
@Ichoran
Sep 26 2018 22:32
If you can have fixed-width binary records, insertions can be appended and deletes can overwrite with an "I am missing" marker, with the whole file only occasionally being rewritten.
Assuming that your problem is that you want to push updates to disk.
Alternatively, you could put the elements in a database and let it worry about the details.
D'Silva
@evnix
Sep 26 2018 22:38
problem is it is a priority queue, a simple append won't do the trick
Ichoran
@Ichoran
Sep 26 2018 22:38
You mean you want to maintain the in-priority order on the disk?
I was thinking you'd re-prioritize everything when you read it in.
Are you using the disk to recover from a crash/power outage or something, or is it an integral part of the operation?
D'Silva
@evnix
Sep 26 2018 22:40
Yes, i am thinking of disk as i want to use least memory as possible.
Ichoran
@Ichoran
Sep 26 2018 22:41
Can you keep an index in memory? If yes, the index can point to where it is on the disk, and then you can just maintain a list of holes and fill in on the disk whenever you add something, appending otherwise.
D'Silva
@evnix
Sep 26 2018 22:42
that's a good idea!
Ichoran
@Ichoran
Sep 26 2018 22:42
If you can't even keep the index in memory, that is a pretty tough problem. I'm not sure what good encoding structures there are for memory layout of a heap on disk.
D'Silva
@evnix
Sep 26 2018 22:43
index in memory works for me, Thanks!
Iurii Malchenko
@yurique
Sep 26 2018 23:13
hey all! is it possible in rust [stable] to have match fail when there’s actually a match block that should match (according to the println! I put in the catch all?)
matrixbot
@matrixbot
Sep 26 2018 23:14
bspeice Can you give us an example in pseudo-code? I'm a bit confused as to what you're asking for.
Iurii Malchenko
@yurique
Sep 26 2018 23:26

hey all! is it possible in rust [stable] to have match fail when there’s actually a match block that should match (according to the println! I put in the catch all?)

nvm, I must be just blind :)