These are chat archives for rust-lang/rust

17th
Jan 2018
dunnousername
@dunnousername
Jan 17 2018 00:27

I'm trying

impl Shr<&mut T> for LookupTable
    where T: ?Sized, Clone {
    type Output = *mut T;
    fn shr(&self, rhs: &mut T) -> Self::Output {
        unsafe {
            let mut ret = self.malloc::<T>(1);
            *ret = rhs.clone();
        }
        return ret;
    }
}

but I get

error: expected one of `!`, `(`, `+`, `::`, `:`, `<`, `==`, or `=`, found `{`
  --> malloc2.rs:60:25
   |
60 |     where T: ?Sized, Clone {
   |                            ^ expected one of 8 possible tokens here

Is there no support for generic traits when the struct is not generic?

For example, can you not implement a Shr<T> for i32 where T: NumberPrimitive
blankhart
@blankhart
Jan 17 2018 00:30
should that be where T: ?Sized + Clone rather than with the comma
also impl<T>...
ret is declared in the unsafe block and returned outside it so would seem to be out of scope there
blankhart
@blankhart
Jan 17 2018 00:36
not sure i understand the Output type
generic reference types usually need a lifetime type parameter
blankhart
@blankhart
Jan 17 2018 00:47
yes though you can implement a generic trait for a non-generic struct
mnivoliez
@mnivoliez
Jan 17 2018 10:29
hello, I got trouble to do a composition of two for
Denis Lisov
@tanriol
Jan 17 2018 10:30
Go ahead :-)
mnivoliez
@mnivoliez
Jan 17 2018 10:30
here's the code:
for game_box in &mut self.boxes {
                if !does_box_collide_ground(game_box, &self.ground)
                    && self.boxes
                        .iter()
                        .fold(false, |_, len| does_box_collide_box(game_box, len))
                {
                    apply_gravity_on_box(game_box, delta_time);
                }
            }
I understand why the borrow checker isn't to happy about it, but, I don't see "an elegant way" to do it in any other way
Denis Lisov
@tanriol
Jan 17 2018 10:32
I'd do this in two passes
mnivoliez
@mnivoliez
Jan 17 2018 10:33
yeah but then I have to store boolean somewhere then
Denis Lisov
@tanriol
Jan 17 2018 10:34
let boxes_collide = self.boxes.iter().map(...);
for game_box in &mut self.boxes { ... }
Or, better, zip them together for iteration
mnivoliez
@mnivoliez
Jan 17 2018 10:36
can you show me the zip? I never use it before
Denis Lisov
@tanriol
Jan 17 2018 10:39
Something like
for (game_box, collides_another) in self.boxes.iter_mut().zip(boxes_collide) { ... }
mnivoliez
@mnivoliez
Jan 17 2018 10:40
I love this.
Denis Lisov
@tanriol
Jan 17 2018 10:44
Note that there's a subtle difference between your initial (intended) version and this one: you were going to determine collisions in-progress, when some boxes are already updated for the next frame and some are still waiting for an update, while this version would detect collisions based on the pre-update coordinates in all cases. The latter seems more correct to me, but your opinion may differ :-)
mnivoliez
@mnivoliez
Jan 17 2018 10:45
I prefer the first one, i'm currently making a physic engine (at school with language of our choice)
and I take the dumb approach, make the dumbest working thing then go to next one
seems like it isnot happy yet
Denis Lisov
@tanriol
Jan 17 2018 10:46
That's exactly the case I'd prefer the second one :-) because the situation "checking A - no collisions, checking B - collision with A" may cause some weirdness :-)
mnivoliez
@mnivoliez
Jan 17 2018 10:47
let boxes_collide = self.boxes.iter().map(|game_box| {
                does_box_collide_ground(game_box, &self.ground)
                    || self.boxes
                        .iter()
                        .fold(false, |_, len| does_box_collide_box(game_box, len))
            });
            for (game_box, collide_another) in self.boxes.iter_mut().zip(boxes_collide) {
                if !collide_another {
                    apply_gravity_on_box(game_box, delta_time);
                }
            }
error[E0502]: cannot borrow `self.boxes` as mutable because it is also borrowed as immutable
  --> src/main.rs:62:48
   |
56 |             let boxes_collide = self.boxes.iter().map(|game_box| {
   |                                 ---------- immutable borrow occurs here
...
62 |             for (game_box, collide_another) in self.boxes.iter_mut().zip(boxes_collide) {
   |                                                ^^^^^^^^^^ mutable borrow occurs here
...
67 |         }
   |         - immutable borrow ends here

error: aborting due to 2 previous errors

error: Could not compile `box-physics`.
is it because of the reduce function?
Denis Lisov
@tanriol
Jan 17 2018 10:53
Sorry, forgot about .collect::<Vec<_>>() for boxes_collide
mnivoliez
@mnivoliez
Jan 17 2018 11:02
thank you. So before collect, the iter isn't destroyed?
Denis Lisov
@tanriol
Jan 17 2018 11:03
Exactly. The iteration actually happens at collect time
Denis Lisov
@tanriol
Jan 17 2018 11:44
@mnivoliez P.S. Note that your fold returns not what you expect it to.
mnivoliez
@mnivoliez
Jan 17 2018 13:01
it's what I just discovered
No true out of this one, perhaps you got an idea why?
mnivoliez
@mnivoliez
Jan 17 2018 13:07
nevermind,
I should have used
.any()
from the start
Patrick Elsen
@xfbs
Jan 17 2018 13:35
Hello rustaceans, I have a little struct for which I want to impl Iterator, but I'm not sure how to fight against rustc to get the lifetimes straight, it's telling me I have an *unconstrained lifetime parameter` and I'm not sure how to constrain it. The code is here, I'd appreciate if someone could give me a hint: https://gist.github.com/xfbs/dd780e776de9040c4a8ff10b8ca281dc
Michal 'vorner' Vaner
@vorner
Jan 17 2018 13:38
You can either do impl<'a> IntoIter for &'a Prime, or let the PrimeIter own the Prime and get rid of the references.
Patrick Elsen
@xfbs
Jan 17 2018 13:47
I went for the first option, I think that's what I want. Thanks :)
Zakarum
@omni-viral
Jan 17 2018 15:12
This message was deleted
michaelr524
@michaelr524
Jan 17 2018 22:30
Is this the place to ask help with getting up and running with Rust with VScode?
  1. Which of the two extensions to use?
  2. Tried both. Neither one couldn't find or install rls. And it didn't say why..