These are chat archives for rust-lang/rust

26th
Oct 2016
Zhang Cheng
@Matrix-Zhang
Oct 26 2016 04:41
AT^IPCFG="IO/TransMode",1
send wrong room...sorry
Tristram Gräbener
@Tristramg
Oct 26 2016 12:41
hi again! Is there a trait that would allow to have an implicit conversion from a litteral float 2.0 to a T (f32 or f64)
Kang Seonghoon
@lifthrasiir
Oct 26 2016 12:45
@Tristramg you can try From<f32> trait implemented for f32 and f64.
T::from(2.0f32) would probably work.
Tristram Gräbener
@Tristramg
Oct 26 2016 12:46
ok
it does work, but it returns an Option, hence formulations like ((d_lon / 2.0).sin()) * ((d_lon / 2.0).sin()) * (lat1.cos()) * (lat2.cos()); as quite unreadle
Kang Seonghoon
@lifthrasiir
Oct 26 2016 12:49
it doesn’t.
what’s your trait bound? it should look like <T: From<f32>> I believe.
(where From is std::convert::From)
Tristram Gräbener
@Tristramg
Oct 26 2016 12:51
used num_traits::float::Float
Kang Seonghoon
@lifthrasiir
Oct 26 2016 12:51
it is more general one, and allows for downcasting
for example you may convert 80u16 to Some(80u8)
obviously, 300u16 will result in None
From trait disallows such a conversion so you should convert from f32 to f32 or f64, which was what I suggested above.
Tristram Gräbener
@Tristramg
Oct 26 2016 12:52
allright
thanks for the explanation, I’ll explore that
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:30
is anyone here?
noob question: I have a Player struct
I want to have a grid of cells, each tagged as belonging to a player
Michael Howell
@notriddle
Oct 26 2016 14:31
Okay.
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:31
how would I set up the Grid struct?
I want to be able to index into the grid as a two-dimensional array
and I'll know the size of the grid at compile time
will something like [[&Player; SIZE]; SIZE] work?
Michael Howell
@notriddle
Oct 26 2016 14:33
[[Option<&Player>; SIZE]; SIZE]
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:33
Ah, yes
Will that allow me to create an array with Nothings everywhere by default?
I mean, how do I initialize the grid?
Michael Howell
@notriddle
Oct 26 2016 14:34
That way, you can initialize it with [[None; SIZE]; SIZE]
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:35
Indeed!
Thank you.
Michael Howell
@notriddle
Oct 26 2016 14:35
That will actually only work for certain sizes.
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:35
is #rust usually this inactive?
what do you mean?
Michael Howell
@notriddle
Oct 26 2016 14:36
Unfortunately, due to limitations of the type system, arrays can't implement Copy for all sizes.
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:36
And what happens if the size is too big (I'm assuming)?
Michael Howell
@notriddle
Oct 26 2016 14:37
You won't be able to copy the array. Which means it can't copy the inner array to initialize the outer array.
This shouldn't be a problem for you, though, since you're dealing in tiny sizes. It implements Copy up to 64, IIRC
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:38
Actually, I'm not
This is for something resembling a game world.
SIZE will be around 10^4, most probably more.
Michael Howell
@notriddle
Oct 26 2016 14:40
Are you sure you don't want some sort of sparse representation, then?
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:40
Something akin to regions owned by a player?
Michael Howell
@notriddle
Oct 26 2016 14:40
Yeah
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:41
I'm not sure, I don't think so, since I ... don't expect sparseness.
What would be a good alternative model that would support indexing?
Michael Howell
@notriddle
Oct 26 2016 14:43
You expect player ownership to fill most of the world, and you expect player ownership to not be clumped into big regions?
Soham Chowdhury
@mrkgnao
Oct 26 2016 14:44
Now that you put it that way, I do. I guess I just wanted to avoid the complexity associated with manipulating the ownership data for each player.
Thanks, I'll look at a region-based solution
Michael Howell
@notriddle
Oct 26 2016 14:47
If you want a close map to the system you were thinking of, maybe https://docs.rs/vec2d would be a good fit?
Or https://docs.rs/vec_2d, which is somewhat simpler.
Jonas Platte
@jplatte
Oct 26 2016 16:00
I'd recommend ndarray. It's not as simple, because it supports more than 2 dimensions, but it has many nice features those smaller crates don't have, such as (optional) serde support.

is #rust usually this inactive?

This gitter channel is not an official communication channel, so yes, this is normal.