These are chat archives for rust-lang/rust

26th
Nov 2017
xahon
@xahon
Nov 26 2017 03:12
What is the best way to overload impl's fn new?
with different count of arguments
Moggers
@Moggers
Nov 26 2017 06:55
if so, why?
Does borrowing in the if let part of an if let else live until the else?
Moggers
@Moggers
Nov 26 2017 07:01
oh because it desugars into a match
but what if a function returns an option of a ref
and its None
does that borrow?
Steve Klabnik
@steveklabnik
Nov 26 2017 13:09
@xahon rust doesn't support overloading that way
Fra ns
@snarf95_twitter
Nov 26 2017 17:56
Well you can always implement from/into
Judson Lester
@nyarly
Nov 26 2017 19:55
Is it bad form to submit Trait implementation PRs? As I'm working with crates, I'm finding that there's plenty of impl's I wish existed, and it seems like a straitforward contribution in a lot of cases.
Granted, there may be considerations that don't occur to me.
Michal 'vorner' Vaner
@vorner
Nov 26 2017 20:18
@nyarly That's probably up to the maintainer, but the worst that'll happen to you is someone explaining why they don't accept it.
(No, I take that back. After enough PRs, you might become the next maintainer, which is probably worse…)
TatriX
@TatriX
Nov 26 2017 20:37

I want to iterate over the vector and update items inside it using some state from the self. What is the correct way to do it?

for item in &mut self.items {
   item.foo = self.bar(item); // fn bar(&self, item: T), item is Copy
}

cannot borrow*selfas immutable becauseself.itemsis also borrowed as mutable

Aleksey Kladov
@matklad
Nov 26 2017 20:40
Split off relevant parts of self into a separate struct
so that the following works
let stuff = &self.stuff;
let items = &mut self.items; // can borrow fields separately
for item in items {
    item.foo = stuff.bar(item)
}
Hm, another hack is to do let tmp = Vec::new(); ::std::mem::swap(&mut tmp, &mut self.items); /*do stuff*/ ::std::mem::swap(&mut tmp, &mut self.items).
Nether thought of it before! :)
Judson Lester
@nyarly
Nov 26 2017 20:47
@vorner I am a little afraid of that, actually :)
Is there a mio/tokio File adapter I'm missing? I think I saw a BSD one... I might be okay with a Linux specific version.
Steve Klabnik
@steveklabnik
Nov 26 2017 20:49
i don't believe so, i've used tokio-cpupool frankly
Aleksey Kladov
@matklad
Nov 26 2017 20:50
Note that async file-io story is murky in general, because, IIRC, only windows has necessary API.
Judson Lester
@nyarly
Nov 26 2017 20:51
Right - it seems straightforward to impl Poll for a nonblocking file... which probably means I'm missing something.
Aleksey Kladov
@matklad
Nov 26 2017 20:51
Yeah, on linux non-blocking file is always ready for reading and writing.

The rationale is that, unlike sockets, you always can read some data immediately, even if that means spinning some disks.

With sockets, if the remote party haven't send any data,you can't read it no matter how hard you try.