These are chat archives for rust-lang/rust

4th
May 2019
David McGillicuddy
@djmcgill
May 04 16:01 UTC

I have

lazy_static! {
    static ref MIGRATIONS: [Box<(dyn Migratable + 'static + Send + Sync)>; 2] = { ... };
}

and a function that requires a T where T: AsRef<[Box<Migratable + 'static>]>, how do I make them line up? I feel like it should just be &MIGRATIONS but that gives

error[E0277]: the trait bound `db::MIGRATIONS: std::convert::AsRef<[std::boxed::Box<(dyn migrant_lib::migratable:
:Migratable + 'static)>]>` is not satisfied
  --> src/db/mod.rs:58:16
   |
58 |         config.use_migrations(
   |                ^^^^^^^^^^^^^^ the trait `std::convert::AsRef<[std::boxed::Box<(dyn migrant_lib::migratable::
Migratable + 'static)>]>` is not implemented for `&db::MIGRATIONS`

e: I've tried a bunch of stuff like MIGRATIONS, &*MIGRATIONS, etc

Ghost
@ghost~5cc2f941d73408ce4fbebebe
May 04 16:18 UTC
i have a doubt
1, consider a multidimensional array
let array1:[[usize;50];50]=[[0;50];50];
how will i find the length of row and col of that array separately using len()
is it possible ?
2,how to slice a multidimensional array
row and col according to our need ?
David McGillicuddy
@djmcgill
May 04 16:23 UTC
1) array1.len(), array1[0].len() if you have a fixed length array of fixed length arrays, but also if the length is known ahead of time, why do you need to use len?
2) What do you mean by slice? To just get the inner slice you can do array1[0][3..5], if you need vertical rows, then something like array1[3..5].into_iter().map(|inner_array| inner_array[0]).collect()? Not sure about that last one
Ghost
@ghost~5cc2f941d73408ce4fbebebe
May 04 16:36 UTC
how to return an array form a function
David McGillicuddy
@djmcgill
May 04 16:38 UTC
do you know the length of array ahead of time?
Ghost
@ghost~5cc2f941d73408ce4fbebebe
May 04 16:40 UTC
fn creat_array()-> // what should i specify here ???
{
let mut array1:[[usize;50];50]=[[0;50];50];
let a =array1.len();
let b =array1[1].len();
for i in 0..a{
for j in 0..b{
let a=check_user_input();// function for entering elements
array1[i][j]=a;
}
}
array1
}
David McGillicuddy
@djmcgill
May 04 16:40 UTC
try fn create_array() -> [[usize; 50]; 50]
you return array1 which has type [[usize; 50]; 50], so the function has that return type
Ghost
@ghost~5cc2f941d73408ce4fbebebe
May 04 16:47 UTC
@djmcgill it is working thank you....
David McGillicuddy
@djmcgill
May 04 16:53 UTC
No problem.
David McGillicuddy
@djmcgill
May 04 17:10 UTC

In the end I did

lazy_static! {
    static ref MIGRATIONS: [EmbeddedMigration; 2] = { ... };
}
config.use_migrations(
            MIGRATIONS[..]
                .into_iter()
                .cloned()
                .map(|migration| -> Box<(dyn Migratable + 'static)> { Box::new(migration) })
                .collect::<Vec<_>>()
        )

which seems ...not ideal... when I just need AsRef<[Box<(dyn Migratable)>]>

Ghost
@ghost~5cc2f941d73408ce4fbebebe
May 04 18:05 UTC
how to pass an 2d array to a function as a parameter
David McGillicuddy
@djmcgill
May 04 18:43 UTC
fn use_array(arr: &[[usize; 50]; 50]) -> ()
or maybe fn use_array(arr: [[usize; 50]; 50]) -> () but I think you want the first
Denis Lisov
@tanriol
May 04 19:07 UTC
@ebinshabu Are you looking for just 2d arrays or for something like numpy?
If the second, you may want to look at ndarray
David Raifaizen
@craftytrickster
May 04 19:13 UTC
i was hoping someone could help me with some confusing type errors

I have this struct

pub struct DisconnectState<S>  {
    attempt_num: u16,
    attempted_reconnect: Box<dyn Future<Item=Framed<S, FunCodec>, Error=()>>
}

and I am trying to do this:

        let reconn: Box<dyn Future<Item=Framed<S, FunCodec>, Error=()>>  = Box::new(
            TcpStream::connect(&addr).map(|stream| {
                let codec = FunCodec::new();
                Framed::new(stream, codec)
            }).map_err(|_|())
        );

        self.state = ConnectionState::Disconnected(DisconnectState {
            attempt_num: 0,
            attempted_reconnect: reconn
        });

however, no matter how much I try to alter it, I get errors like this:

expected structtokio_tcp::stream::TcpStream, found type parameter
|
= note: expected type tokio_io::_tokio_codec::framed::Framed<tokio_tcp::stream::TcpStream, _>
found type tokio_io::_tokio_codec::framed::Framed<S, _>

Denis Lisov
@tanriol
May 04 19:49 UTC
Where are you trying to do this?
David Raifaizen
@craftytrickster
May 04 19:51 UTC
I'm going to give up on the above
unfortunately, since Framed is not a trait, i cant do type erasure via a box trait object
im going to try another approach
Denis Lisov
@tanriol
May 04 19:52 UTC
What do you want to achieve?
David Raifaizen
@craftytrickster
May 04 19:52 UTC
a future that infinitely runs and when it polls and realizes it disconnected, it attempts reconnect
dont' worry about it, i think i can get it to work via a different, slightly more involved way
Denis Lisov
@tanriol
May 04 19:56 UTC
The problem I see in this is that your struct is defined to work with something (type parameter S), while the code is specific to TcpStream. Do you actually want it to work for TCP only or for any kind of stream (and if the latter, how should it connect?).
David Raifaizen
@craftytrickster
May 04 19:58 UTC
yeah, the reconnect should only occur for tcp
im going to define a new trait called ReconnectAsyncReadWrite or something, and then just uniformly have that