These are chat archives for rust-lang/rust

12th
Mar 2018
Ingvar Stepanyan
@RReverser
Mar 12 2018 00:05
@tanriol Yeah, that should be the right solution.
@bioball Problem is essentially that you're declaring return type as reference that lives as long as original lifetime 'a, but at the same time iterator "owns" (moves) original vector instead of just referencing it as long as 'a
So in the result you are trying to return references to part of Iterator itself, although it might die before 'a does
Josh
@joshlemer
Mar 12 2018 01:21
Hey I was just wondering, why does Iterator have an associated type in its trait, rather than use a generic (type parameter)? Could it have used either and it is just chance that it used the associated type approach?
the associated type I'm talking about it Iterator::Item by the way
seems like it could have been Iterator<T>
Daniel Chao
@bioball
Mar 12 2018 01:50
ah. crazy. but the iterator references a StackVec whose values have the lifetime of 'a, and i'm returning a reference to one of the values
Daniel Chao
@bioball
Mar 12 2018 01:59
i guess this is one of those cases where i have the help the compiler out a bit?
also, so now i'm trying to implement IntoIter for this iterator, and i'm not sure how to construct the iterator
Hans W. Uhlig
@huhlig
Mar 12 2018 04:59
Is there a better way to do this?
let delta = {
    let duration = new_time.duration_since(last_update);
    duration.as_secs() as f64 + (duration.subsec_nanos() as f64 / 1_000_000_000.0)
};
Ghoughpteighbteau
@Ghoughpteighbteau
Mar 12 2018 05:46
man, that's how the docs do it O.o, what a weird bit of API.
Hans W. Uhlig
@huhlig
Mar 12 2018 05:47
@Ghoughpteighbteau that in reference to me?
Ghoughpteighbteau
@Ghoughpteighbteau
Mar 12 2018 05:47
yah
duration is weird.
Hans W. Uhlig
@huhlig
Mar 12 2018 05:47
yeah
Yuji Kanagawa
@kngwyu
Mar 12 2018 05:56

@huhlig
u32 to f64 is lossless cast so you can use f64::from, but otherwise there seems to be no room for improvement...
Duration is definend as

pub struct Duration {
    secs: u64,
    nanos: u32, // Always 0 <= nanos < NANOS_PER_SEC
}

and there seems to be no API like as_millis ...

Hans W. Uhlig
@huhlig
Mar 12 2018 05:58
yeah, I know
the time system needs a drastic QOL overhaul
Yuji Kanagawa
@kngwyu
Mar 12 2018 05:58
I agree
Hans W. Uhlig
@huhlig
Mar 12 2018 05:59
let delta = {
  let duration = new_time.duration_since(last_update);
  duration.as_secs() as f64 + (f64::from(duration.subsec_nanos()) / 1_000_000_000.0)
};
does that look better?
or should I do f64::from on the first half too
Yuji Kanagawa
@kngwyu
Mar 12 2018 06:02
as_secs() returns u64 and cast from u64 to f64 can be lossy, so you have to use as
Hans W. Uhlig
@huhlig
Mar 12 2018 06:02
alright
I can almost garuntee that unless there is a bug... it will be 0
and it will deffinitly be below u64
Hans W. Uhlig
@huhlig
Mar 12 2018 06:07
one more stupid question
error[E0277]: the trait bound `&mut app::Application<'a, D>: engine::Engine` is not satisfied
  --> src\app.rs:62:36
   |
62 |                 self.states.update(&mut self, delta);
   |                                    ^^^^^^^^^ the trait `engine::Engine` is not implemented for `&mut app::Application<'a, D>`
   |
   = help: the following implementations were found:
             <app::Application<'a, D> as engine::Engine>
   = note: required for the cast to the object type `engine::Engine`
im obvieously not doing something right
Yuji Kanagawa
@kngwyu
Mar 12 2018 06:41
@huhlig
What args states.update takes and what's the type of self?
Hans W. Uhlig
@huhlig
Mar 12 2018 06:44
self is application, it implements Engine
states.update takes Engine and f64
thus I would think that it would accept anything that implemented it and specialize around it
Yuji Kanagawa
@kngwyu
Mar 12 2018 06:50
Oh what I'm saying is if your function takes self likefn f(self, self is Self and if fn f(&self, self is &Self, ...
And update is fn<E: Engine> update(engine: E or fn<E: Engine> update(engine: &E or fn<E: Engine> update(engine: &mut E?
Denis Lisov
@tanriol
Mar 12 2018 07:15
@bioball How about this IntoIterator?
Niranjan A Kartha
@n-kartha
Mar 12 2018 11:17
Is there an unsigned right shift (zero fill) for isizes?
Or do I have to cast it to a usize first?
And how do I get the size of a usize?
Zakarum
@omni-viral
Mar 12 2018 11:20

size of a usize?

In bytes?

std::mem::size_of::<usize>()
Also there is cfg to check size of pointer in bits on the platform.
Niranjan A Kartha
@n-kartha
Mar 12 2018 11:22
Okay
Thanks @omni-viral
Daniel Chao
@bioball
Mar 12 2018 20:02
@tanriol thanks! wow. context bounds for lifetimes