These are chat archives for rust-lang/rust

22nd
Nov 2018
Conner Bryan
@cab
Nov 22 2018 03:27
is it possible to store a list of heterogenous Boxes? or a list of heterogenous *mut pointers?
Conner Bryan
@cab
Nov 22 2018 03:33
ah, i can do Vec<Box<Send>> or similar
laurent bernabé
@loloof64
Nov 22 2018 07:15
Hi :smile: ! Do serde let define methods for the Structs it serializes ?
#[derive(Debug, Serialize, Deserialize)]
struct Board {
    elements:  [Side; 9],
    side_to_play: Side,
}

impl Board {
    fn new() -> Board {
        Board { elements: [Side::None; 9], side_to_play: Side::Cross }
    }

    #[allow(unused)]
    fn piece_at(&self, cell_x: usize, cell_y: usize) -> Side {
        return self.elements[(cell_y * 3) + cell_x]
    }

    #[allow(unused)]
    fn turn(&self) -> Side {
        self.side_to_play
    }

}
Because it seems that I can't access board.play() method (not shown here) from Javascript side of my WebView project, even through an rpc object.
Tim Robinson
@1tgr
Nov 22 2018 07:21

@shaleh

How does Iterator::foreach() compare to a for loop over a similar block in terms of efficiency, space, etc?

Generally identical, although the docs mention iterators can define their own for_each implementation for efficiency

It gives the example of Chain, although as far as I can tell it uses the default implementation
Judging by the docs, the main use of for_each is at the end of a long .iter().map().filter() chain or similar
Sean Perry
@shaleh
Nov 22 2018 07:28
Helpful. Thank you.
Denis Lisov
@tanriol
Nov 22 2018 07:57
@tamird Why does it fail non-deterministically? Maybe you can make it more reliable?
Otherwise you can just add a loop into the test itself.
Denis Lisov
@tanriol
Nov 22 2018 08:02
@loloof64 No such limitations from serde. What is the error you get?
laurent bernabé
@loloof64
Nov 22 2018 08:21
Oh sorry. I forgot the error : no method board.play() something like that. I'll post the more precise message this evening.
Board.piece_at() undefined.
It must be the method conversion name who has beem translated differemtely
laurent bernabé
@loloof64
Nov 22 2018 08:39
I've post the frontend and backend code from pastebin above.
trsh
@trsh
Nov 22 2018 08:39
How do you go production with a rust project? Like make a bundle, executable you can run without rust ?
Tim Robinson
@1tgr
Nov 22 2018 08:43
@loloof64 I don’t know how JavaScript and Rust work together, but are you sure your methods don’t need to be pub?
trsh
@trsh
Nov 22 2018 08:47
"When your project is finally ready for release, you can use cargo build --release to compile your project with optimizations."
So then I just copy the "release" folder to device I want to run it?
Tim Robinson
@1tgr
Nov 22 2018 08:49
I copy just the one binary out of the target/release folder
This is assuming you’re static linking to your crate dependencies, which is the default
You can install cargo-rpm which gives you a cargo rpm command, but for simple apps it’s enough to copy the binary
trsh
@trsh
Nov 22 2018 08:56
@1tgr hmm
On unix?
complaining about .env stuff missing
trsh
@trsh
Nov 22 2018 09:03
In debug version I had that .env file. Whats the strategy for release?
Cant find any leads
Sylwester Rąpała
@xoac
Nov 22 2018 09:22

I don't know how to manage with this:

let point = String::from("192.168.12.45:9999"); // it can also be "/dev/tty??" 
let will_be_item = if let Ok(addr) = point.parse() {
   from_tcp(addr)
} else {
   future::ok(from_serial_path(point).unwrap())
}

I don't care what type will_be_item is I just want it to impl Future< Item = Item, Error = _>
from_tcp(addr: &SocketAddr) -> impl Future<Item=Item, Error=Error>
from_serial_path(path: impl Path) -> Result<Item, OtherError>

How should I do it? Or maybe it's bad designed (functions all also mine)

I have tried use future::Either but it then dosen't impl Future for will_be_item
laurent bernabé
@loloof64
Nov 22 2018 09:24
@1tgr oh my god ! Maybe that is the point. Thank you very much :smile: I will try this evening.
Tim Robinson
@1tgr
Nov 22 2018 09:28

@trsh In debug version I had that .env file. Whats the strategy for release?

.env is not part of standard Rust so it depends what error you're getting

I guess this https://docs.rs/dotenv/ could be looking for a .env file
@xoac future::Either should be a good solution here
let will_be_item = if let Ok(addr) = point.parse() {
   Either::A(from_tcp(addr))
} else {
   Either::B(future::ok(from_serial_path(point).unwrap()))
}
Sylwester Rąpała
@xoac
Nov 22 2018 09:30
@1tgr
   --> examples/rest_api.rs:183:10                                                                                                                                                                                                           
    |                                                                                                                                                                                                                                        
183 |         .and_then(|item| {                                                                                                                                                                                                             
    |          ^^^^^^^^                                                                                                                                                                                                                      
    |                                                                                                                                                                                                                                        
    = note: the method `and_then` exists but the following trait bounds were not satisfied:                                                                                                                                                  
            `futures::future::Either<impl futures::Future, futures::FutureResult<mtcp_client::MTCPStream<mtcp_client::tokio_serial::Serial>, std::io::Error>> : futures::Future`                                                             
            `&mut futures::future::Either<impl futures::Future, futures::FutureResult<mtcp_client::MTCPStream<mtcp_client::tokio_serial::Serial>, std::io::Error>> : futures::Stream`                                                        
            `&mut futures::future::Either<impl futures::Future, futures::FutureResult<mtcp_client::MTCPStream<mtcp_client::tokio_serial::Serial>, std::io::Error>> : futures::Future`
I don;t know why Either::B::Error is std::io::Error .. it should be tokio::io::Error
      let connect_point = std::env::var("CONNECT_POINT").unwrap_or("/dev/ttyS2".to_string());
      let will_be_item = if let Ok(addr) = connect_point.parse() {
          future::Either::A(from_tcp(&addr))
      } else {
          future::Either::B(future::ok::<_, tokio::io::Error>(from_path(connect_point).unwrap()))
      };
Tim Robinson
@1tgr
Nov 22 2018 09:39
Do they have the same Item type?
Does from_tcp return impl Future<Item = mtcp_client::MTCPStream<mtcp_client::tokio_serial::Serial>, Error = std::io::Error>?
Sylwester Rąpała
@xoac
Nov 22 2018 09:40
no it return MTCPStream<A> where A:AsyncWrite+AsyncRead
exactly it return impl Future< Item = MTCPStream<tokio::net::TcpStream>, Error = tokio::io::Error>
Tim Robinson
@1tgr
Nov 22 2018 09:44
So you need an equivalent of Either that can hold an MTCPStream<TcpStream> or an MTCPStream<Serial>
Easy way to do this is Box
trait AsyncReadWrite: AsyncRead + AsyncWrite {}
impl AsyncReadWrite for T where T: AsyncRead + AsyncWrite {}
...
let will_be_item = if let Ok(addr) = connect_point.parse() {
    future::Either::A(from_tcp(&addr).map(|stream| {
        let b: Box<AsyncReadWrite> = Box::new(stream);
        b
    })
} else {
    let stream = from_path(connect_point).unwrap();
    let b: Box<AsyncReadWrite> = Box::new(stream);
    future::Either::B(future::ok::<_, tokio::io::Error>(b))
};
Sylwester Rąpała
@xoac
Nov 22 2018 09:47
Ok Thanks. Last question why compiler says Either::B::Error is std::io::Error? I "told" him it's tokio::io::Error
Tim Robinson
@1tgr
Nov 22 2018 09:48
I recommend not using the Error type directly because there are so many and it's easy to pick the right one
I normally write io::Error instead of std::io::Error, but even here it can be confused with tokio::io::Error
So either spell it out in full, or use std::io::Error as StdIoError; use tokio::io::Error as TokioIoError;
Sylwester Rąpała
@xoac
Nov 22 2018 09:50

I think you don't understood my question:

    = note: the method `and_then` exists but the following trait bounds were not satisfied:                                                                                                                                                  
            `futures::future::Either<impl futures::Future, futures::FutureResult<mtcp_client::MTCPStream<mtcp_client::tokio_serial::Serial>, std::io::Error>> : futures::Future`

in response to this:

          future::Either::B(future::ok::<_, tokio::io::Error>(from_path(connect_point).unwrap()))

I expected this error (I constructed it by hand):

    = note: the method `and_then` exists but the following trait bounds were not satisfied:                                                                                                                                                  
            `futures::future::Either<impl futures::Future, futures::FutureResult<mtcp_client::MTCPStream<mtcp_client::tokio_serial::Serial>, tokio::io::Error>> : futures::Future`
the diffrence is std::io::Error (I don't mentioned it at all) / tokio::io::Error
Tim Robinson
@1tgr
Nov 22 2018 09:51
Either takes its item and error types from the Either::A case
So if they don't match, then it type checks, but none of the future methods work
Sylwester Rąpała
@xoac
Nov 22 2018 09:52
implementation from_tcp:
pub fn from_tcp(addr: &SocketAddr) -> impl Future< Item = MTCPStream<TcpStream>, Error = tokio::io::Error> {
    tokio::net::TcpStream::connect(addr).and_then(|tcp_stream| {
        Ok(from_async_point(tcp_stream))
    })
}
still should be tokio::io::Error
Tim Robinson
@1tgr
Nov 22 2018 09:53
Ok, I'm sure std::io::Error will have crept in somewhere
If you can, try commenting out any use std::io; and see what lines fail to compile
Sylwester Rąpała
@xoac
Nov 22 2018 09:55
Yes you are right. It exist but not in place I wish to be used. from_path(connect_point) return std::io::Error. But Either take it after unwrap()..
trsh
@trsh
Nov 22 2018 12:01
Of course the "best ever" thing happens when running the binary on production server - nothing
Just hangs
Nooo its working... jahuu.. tnx guys
trsh
@trsh
Nov 22 2018 12:56
dotenv::var vars are compiled or read on run?
toxicafunk
@toxicafunk
Nov 22 2018 12:57
hi, so stupid question but....
Iḿ trying to use r2d2-mongodb
once i get to let conn = pool.get().unwrap();
conn is a Pool<MongodbConnectionManager> but Iḿ not sure what to do with that
any examples or post or something anyone can point me to?
I do get a client but nothing resembling what i see here: https://crates.io/crates/mongodb
there should be a connect method in the ConnectionManager, but not sure how to get to it
trsh
@trsh
Nov 22 2018 14:55
Hi anybody can advise how to deal with "certificate verify failed" on unix system with openSSL
Sylwester Rąpała
@xoac
Nov 22 2018 15:03
Hi, Is it possible to do some strange things when docs is generated? I would like to add # Serialization section where I will print example output in json. It would be similar to example section but instead of just check code it will execute it when building docs and put there standard output
Sth like derive but not for code but for docs :P
trsh
@trsh
Nov 22 2018 15:45
I checked the cert like: openssl s_client -connect x.myshopify.com:443 -CApath /etc/ssl/certs
And got Verify return code: 0 (ok)
Never less getting this:
Connector(SslError(Error { code: ErrorCode(1), cause: Some(Ssl(ErrorStack([Error { code: 337047686, library: "SSL routines", function: "tls_process_server_certificate", reason: "certificate verify failed", file: "ssl/statem/statem_clnt.c", line: 1230 }]))) }))
trsh
@trsh
Nov 22 2018 16:20
Solved with openssl-probe :/
laurent bernabé
@loloof64
Nov 22 2018 17:13
@ltgr Unfortunately, even with pub methods for Board class inside backend Rust, I got an error 'Board.piece_at is not a function' (board.piece_at is undefined)
laurent bernabé
@loloof64
Nov 22 2018 18:25
Finally I've managed to do something useful : it seems that I must not pass the Board directly with Rpc class, but rather call its method through rpc via a "proxy" : another function.
updated
Thanks again for your helps :smile:
Tim Robinson
@1tgr
Nov 22 2018 19:21
I get it, you're just ending JSON messages from the Javascript to the Rust?
In your JS you have:
        load_piece_at: function(cell_x, cell_y) {
            rpc.invoke({cmd: 'GetPieceAt', cell_x, cell_y})
        },
And you're getting the error:
board.piece_at is undefined
So change load_piece_at to piece_at?