These are chat archives for rust-lang/rust

19th
Oct 2017
Fra ns
@snarf95_twitter
Oct 19 2017 06:58
Is something like this possible without allocating to a vec?
use std::cmp::{max, min};

let (tr, tc) = (tile.row as i8, tile.column as i8);

let sweeps = [
    (min(tc + 1, 8) as u8..8)
        .map(|i| Tile::new(tile.row, i))
        .collect::<Vec<_>>(),
    (0..max(0, tc - 1) as u8)
        .rev()
        .map(|i| Tile::new(tile.row, i))
        .collect::<Vec<_>>(),
    (min(tr + 1, 8) as u8..8)
        .map(|i| Tile::new(i, tile.column))
        .collect::<Vec<_>>(),
    (0..max(0, tr - 1) as u8)
        .rev()
        .map(|i| Tile::new(i, tile.column))
        .collect::<Vec<_>>(),
];

for s in sweeps.iter() {
    let res = sweep(self, s.iter(), player);
    moves.extend(res);
}
Aleksey Kladov
@matklad
Oct 19 2017 07:20

@snarf95_twitter Do you need to keep seeps split into 4 groups?

If you don't you can sweeps = first.chain(second).chain(third)....

Alternatively, I think it's possible to do let sweeps: &[&Iterator<Item=Tile>] = [ ... ]
Fra ns
@snarf95_twitter
Oct 19 2017 07:27
Yea I definitely need them to be in groups... I tried removing the collect parts but then it complained about rev() and closures not being identical or something. I’ll try your last suggestion, thanks :)
DevDigitalNomad
@DevDigitalNomad
Oct 19 2017 08:05
Guys. Did I understand it well? 2 parameters in function with lifetime make returned reference with the same lifetime as one of peremeter? With smallest lifetime?
Fra ns
@snarf95_twitter
Oct 19 2017 08:06
nope it complains expected struct std::ops::Range, found struct std::iter::Rev
Aleksey Kladov
@matklad
Oct 19 2017 08:09
Yeah, you have to add some references as well in there, to make rustc turn iterators into trait objects. WIll try to come up with example !
Denis Lisov
@tanriol
Oct 19 2017 08:11
@DevDigitalNomad Not the smallest. Either the only one if there's only one, or the lifetime of &self / &mut self (documentation). If that's not what you want, you need to specify them manually.
DevDigitalNomad
@DevDigitalNomad
Oct 19 2017 08:12
@tanriol Thanks. Now I get it 😉
Aleksey Kladov
@matklad
Oct 19 2017 08:13
Vectors are only to get some base iterators of course, here's an exaample with no heap allocation at all: https://play.rust-lang.org/?gist=ca3332c7815e4137f07bb6eaea319735&version=stable
Fra ns
@snarf95_twitter
Oct 19 2017 08:18
ehh that looks kinda funky what's with the muts?
Aleksey Kladov
@matklad
Oct 19 2017 08:19
To avoid allocations, we must use iterators. But to actually consume interator, you have to have an exclusive access to it
Hence &mut is required, and not plain &.
& themselves are needed to enable dynamic dispatch
Remember, .iter() and .iter().rev() are different types, and they could have different size_of, so you can't store them in a single array.
The usual trick to work around this is to switch to dynamic dispatch, and store a pointer to the iterator and vtable instead, which has a fixed size.
Fra ns
@snarf95_twitter
Oct 19 2017 08:21
hmm very cool indeed
Aleksey Kladov
@matklad
Oct 19 2017 08:22
In Rust, you can get VTable primarily via two methods: you can allocate a Box<Trait>, or you could use a &Trait.
Fra ns
@snarf95_twitter
Oct 19 2017 08:23
now I'm getting this issue
```
Aleksey Kladov
@matklad
Oct 19 2017 08:23
Nah, not really cool, just a price you have to pay for tight control of memory layout (: It would be so much easier in a langauge like Java, C#, Python, where everything is a pointer :)
Fra ns
@snarf95_twitter
Oct 19 2017 08:23
error[E0271]: type mismatch resolving `<&mut &mut std::iter::Iterator<Item=Tile> as std::iter::Iterator>::Item ==
 &Tile`
   --> src\main.rs:207:39
    |
207 |                             let res = sweep(self, s, player);
    |                                       ^^^^^ expected struct `Tile`, found &Tile
    |
    = note: expected type `Tile`
               found type `&Tile`
    = note: required by `sweep`
Aleksey Kladov
@matklad
Oct 19 2017 08:24
That's a usual &T vs T mismatch in iterators
Tile is Copy, right?
Then sweep should accept Iterator<Item=Tile>, and not Iterator<Item=&TIle>
Fra ns
@snarf95_twitter
Oct 19 2017 08:27
this is my sweep decl:
```
fn sweep<'a, I>(board: &Board, tiles: I, player: &Owner) -> Vec<Tile>
where
    I: Iterator<Item = &'a Tile>,
okay got it working now
as you said it should not take references
Aleksey Kladov
@matklad
Oct 19 2017 08:28
Yeah, I believe you don't need &'a
Fra ns
@snarf95_twitter
Oct 19 2017 08:28
thanks a lot @matklad
Aleksey Kladov
@matklad
Oct 19 2017 08:29
In general, if a type T is Copy, just don't use references to T in API, and in implementation, try to get rid of & as soon as possible.
The example of the latter is calling clone for stuff like vector.iter().cloned(), or using & patterns like xs.filter(|&x| ... )
Fra ns
@snarf95_twitter
Oct 19 2017 08:32
yeah it doesn't make much sense to not have Tile be Copy anyway I think. It's basically just two u8 fields
time to do some cleaning I guess ;p
Alyani
@notsonotso
Oct 19 2017 09:01
ROFL, we've now integrated a codegen step in our build system (generates tests for sensor data input), which means our codebase is now about 150KLOC, and the build times totally exploded... 3 minutes and counting... 150KLOC isn't that much.
4 minutes, 14 seconds :angry:
Aleksey Kladov
@matklad
Oct 19 2017 09:03
@notsonotso yeah, in my experience any significant code-gen work must be segregated to a separate crate/compilation unit, to avoid recompilations at all costs.
Alyani
@notsonotso
Oct 19 2017 09:03
this is pretty horrible
don't blame LLVM, clang compiles the same output for C++14 in 20 seconds.
Rui Azevedo
@neu-rah
Oct 19 2017 09:05
cores?
Aleksey Kladov
@matklad
Oct 19 2017 09:05
Well, no one saying that Rust is fast to compile :) Short-term, I have high hopes for incremental compilation and codegen infra cleanup based on MIR refactoring achievements. Long term, I hope something like cretonne will fly :)
Alyani
@notsonotso
Oct 19 2017 09:07
@neu-rah Intel Core i7-980X, fast samsung ssd, 32 gigs of ram
the machine is not the problem
Rui Azevedo
@neu-rah
Oct 19 2017 09:09
@notsonotso i mean this compile might not be equally optimized for multicores as your other references... if so there's space for optimization
Alyani
@notsonotso
Oct 19 2017 09:14
@neu-rah sounds like excuses
at any rate, a 150KLOC compile on a single core should be 30 secs max
Rui Azevedo
@neu-rah
Oct 19 2017 09:15
@notsonotso well you can always help to make it better, its open source code
Sebastian Blei
@iamsebastian
Oct 19 2017 09:17
Is it possible to use the method io::Read::read_to_string()without reaching EOF — means, read a Copy / Clone or just roll back to start of file, after it was read?
https://doc.rust-lang.org/std/io/trait.Read.html#method.read_to_string
I try to implement a logging middleware in Nickel on Hyper::server::Request, and while doing this, I need to read the body of the Request struct.
Ewan Higgs
@ehiggs
Oct 19 2017 09:48
In case you guys and gals missed it, the Fosdem Rust deroom CfP is here: http://rust-fosdem.github.io/
TatriX
@TatriX
Oct 19 2017 10:42
let f: Box<Fn() + 'static> = Box::new(|| println!("in"));
What does 'static' mean here?
Andrey Lesnikov
@ozkriff
Oct 19 2017 10:45
ensures this Fn() lives forever?
Zakarum
@omni-viral
Oct 19 2017 10:46
I guess this is unnecessary as Box<T> imply T: 'static
When no lifetime is given and T is a trait
Rui Azevedo
@neu-rah
Oct 19 2017 10:51
hi!
This message was deleted
(wrong room)
Marcel
@marcelbuesing
Oct 19 2017 12:39
Is there a function to map over T and E in Result<T,E> at the same time basically a function from T or E to X where T and E can be translated to X?
TatriX
@TatriX
Oct 19 2017 12:41
Just use a simple match?
Marcel
@marcelbuesing
Oct 19 2017 13:06
That's what I'm using right now, but I thought there might be something shorter
Zakarum
@omni-viral
Oct 19 2017 13:07
How it can be shorter? You have to provide a pair of functions / pair of arms in match
Marcel
@marcelbuesing
Oct 19 2017 13:08
Unless the function I map over the Result accepts T and E
Zakarum
@omni-viral
Oct 19 2017 13:08
So it implements both FnOnce(T) -> X and FnOnce(E) -> X?
Marcel
@marcelbuesing
Oct 19 2017 13:09
yes
kind of
more specifically I need to serialize T or E
Zakarum
@omni-viral
Oct 19 2017 13:12
@marcelbuesing If I try to create such a function I got an error. I guess it is a good reason there is no such funtion is std
https://play.rust-lang.org/?gist=2dceb08ec59b537dd6623e0654256820&version=nightly
Also there is no actual object can implement FnOnce for different set of paramters
functions with type parameters has to be monomorphized. And monomorphized function can accept only one set of arguments
So you can't implement Result::map_ok_err and you can't find a value to put there.
Zakarum
@omni-viral
Oct 19 2017 13:17
In your case serde::to_string is not a value. serde::to_string::<T> is. But it can't accept E
Marcel
@marcelbuesing
Oct 19 2017 13:19
Ok, thank you for the detailed explanation =) ! In that case I will just stick to the match.
Moggers
@Moggers
Oct 19 2017 13:44
Are there any exemplary or off the shelf implementations of english parsing?
I've never looked into linguistics
but I want to be able to turn english sentances into verb-noun-condition kinda things
Arne Ehrlich
@TypedLambda
Oct 19 2017 14:00
can rustup build rust from source?
the current stable/nightly for freebsd do not work on current/11, probably because of inode64 support beeing added and breaking the ABI.
Marcel
@marcelbuesing
Oct 19 2017 15:16
not sure if it helps you, you could specify an older working version https://github.com/rust-lang-nursery/rustup.rs#toolchain-override-shorthand
TatriX
@TatriX
Oct 19 2017 19:34
How can I call default trait's implantation method?
(from the overridden method)
Dylan McKay
@dylanmckay
Oct 19 2017 19:35
<Self as ParentTrait>::my_method(args)
not sure what the syntax is called, but it's kind of like a fully-qualified method path
if you don't explicitly qualify the method call, it will default to <Self as Self>, or rather just call the current trait
TatriX
@TatriX
Oct 19 2017 19:37
Ok, thanks
Steve Klabnik
@steveklabnik
Oct 19 2017 19:54
@dylanmckay it used to be called something else but "fully-qualified path syntax" is now the official name, so you're right :smile:
Alyani
@notsonotso
Oct 19 2017 20:01
JACOB APPELBAUM 'S LOVER
oh wait, wrong room
Zakarum
@omni-viral
Oct 19 2017 20:13
<Self as ParentTrait>::my_method(args)
If you put it in Self::my_method it will create infinite recursion
TatriX
@TatriX
Oct 19 2017 20:14
rustc warns about it
Zakarum
@omni-viral
Oct 19 2017 20:16
I think @dylanmckay misunderstood you. Thinking that your structure have the same named method as in ParentTrait