These are chat archives for rust-lang/rust

1st
Nov 2018
Sackery
@sackery
Nov 01 2018 11:09
i added rocket-cors to workspace
laurent bernabé
@loloof64
Nov 01 2018 13:47
Hi everyone, I am facing an issue with a custom Struct implementation and a closure
pub struct ChessBoard
{
    drawing_area: DrawingArea,
    cells_size: u32,
}

impl ChessBoard
{
    pub fn new(cells_size: u32) -> ChessBoard
    {
        let piece_images = PieceImages::new(cells_size as i32);
        let drawing_area = DrawingArea::new();

        let chessBoard = ChessBoard {
            drawing_area: drawing_area,
            cells_size: cells_size,
        };

       /* HERE */ chessBoard.drawing_area.connect_draw(move |_, cr|{
            draw_background(cr);
            draw_cells(cr, cells_size);
            draw_pieces(cr, cells_size, &piece_images);
            draw_coordinates(cr, cells_size);

            Inhibit(false)
        });

        chessBoard
    }

    pub fn get_drawing_area(&self) -> &DrawingArea
    {
        &self.drawing_area
    }

    fn draw_background(cr: &Context)
    {
        let pink_color = [255.0/255.0, 204.0/255.0, 204.0/255.0];
        cr.set_source_rgb(
            pink_color[0],
            pink_color[1],
            pink_color[2],
        );
        cr.paint();
    }

    fn draw_cells(cr: &Context, cells_size: u32)
    {
        (0..8).for_each(|row| {
            (0..8).for_each(|col| {
                let white_cell_color = [255.0/255.0, 255.0/255.0, 179.0/255.0];
                let black_cell_color = [153.0/255.0, 102.0/255.0, 51.0/255.0];

                let is_white_cell = (row+col) % 2 == 0;
                let cell_color = if is_white_cell {white_cell_color} else {black_cell_color};

                let rect_x = (cells_size as f64) * (0.5 + (col as f64));
                let rect_y = (cells_size as f64) * (0.5 + (row as f64));
                let rect_size = cells_size as f64;

                cr.rectangle(
                    rect_x,
                    rect_y,
                    rect_size,
                    rect_size,
                );
                cr.set_source_rgb(
                    cell_color[0],
                    cell_color[1],
                    cell_color[2],
                );
                cr.fill();
            });
        });
    }

    fn draw_pieces(cr: &Context, cells_size: u32, piece_images: &PieceImages)
    {
        let image = piece_images.get_white_queen();
        let location_x = (cells_size as f64) * 0.5;
        let location_y = (cells_size as f64) * 0.5;
        cr.set_source_pixbuf(
            image,
            location_x,
            location_y
        );
        cr.paint();
    }

    fn draw_coordinates(cr: &Context, cells_size: u32)
    {
        let files = ["A", "B", "C", "D", "E", "F", "G", "H"];
        let ranks = ["8", "7", "6", "5", "4", "3", "2", "1"];

        cr.set_source_rgb(0.2, 0.4, 1.0);
        cr.select_font_face(
            "Sans Serif",
            FontSlant::Normal,
            FontWeight::Bold
        );
        cr.set_font_size((cells_size as f64) * 0.38);

        (0..8).for_each(|file_index| {
            let letter = files[file_index];
            let letter_x = (cells_size as f64) * (0.9 + (file_index as f64));
            let letter_y_top = (cells_size as f64) * 0.4;
            let letter_y_bottom = (cells_size as f64) * 8.9;

            cr.move_to(letter_x, letter_y_top);
            cr.show_text(letter);

            cr.move_to(letter_x, letter_y_bottom);
            cr.show_text(letter);
        });

        (0..8).for_each(|rank_index| {
            let letter = ranks[rank_index];
            let letter_y = (cells_size as f64) * (1.2 + (rank_index as f64));
            let letter_x_left = (cells_size as f64) * 0.1;
            let letter_x_right = (cells_size as f64) * 8.6;

            cr.move_to(letter_x_left, letter_y);
            cr.show_text(letter);

            cr.move_to(letter_x_right, letter_y);
            cr.show_text(letter);
        });
    }
}
In the new method of the chessBoard, I call the GTK function connect_draw(), which takes a closure (with DrawingArea and Context), but I don't know how to reference to the chessboard object for which it is called : a kind of self
I just which I could call
chessBoard.drawing_area.connect_draw(move |_, cr|{
            self.draw_background(cr);
            self.draw_cells(cr, cells_size);
            self.draw_pieces(cr, cells_size, &piece_images);
            self.draw_coordinates(cr, cells_size);

            Inhibit(false)
        });
laurent bernabé
@loloof64
Nov 01 2018 13:52
Is there a way to get a reference to the chessboard inside the connect_draw call ?
How should I call connect_draw another way ?
laurent bernabé
@loloof64
Nov 01 2018 14:04
Oh sorry ! I know how to solve the code, and it is straightforward.
(Adding &self to each method, then add chessboard. for each call)
Denis Lisov
@tanriol
Nov 01 2018 14:05
I guess you'll need an Rc/Arc somewhere due to some 'static requirements
laurent bernabé
@loloof64
Nov 01 2018 14:09
Thank you, Indeed my solution brings some problems (borrowing) so I'll look toward Rc or Arc right now :smiley:
laurent bernabé
@loloof64
Nov 01 2018 14:23
My current attempt, as I may have misunderstood many things
impl ChessBoard
{
    pub fn new(cells_size: u32) -> ChessBoard
    {
        let piece_images = PieceImages::new(cells_size as i32);
        let drawing_area = DrawingArea::new();

        let chessBoard = ChessBoard {
            drawing_area: drawing_area,
            cells_size: cells_size,
        };

        let chessBoardRef = Rc::new(chessBoard);

        chessBoard.drawing_area.connect_draw(move |_, cr|{
            chessBoardRef.draw_background(cr);
            chessBoardRef.draw_cells(cr, cells_size);
            chessBoardRef.draw_pieces(cr, cells_size, &piece_images);
            chessBoardRef.draw_coordinates(cr, cells_size);

            Inhibit(false)
        });

        chessBoard
    }

    pub fn get_drawing_area(&self) -> &DrawingArea
    {
        &self.drawing_area
    }

/* and the rest */
$ cargo run
   Compiling chess_position_trainer v0.1.0 (/home/laurent-bernabe/Documents/Programmation/ProjetsPersos/Rust/chess_position_trainer)
error[E0382]: use of moved value: `chessBoard.drawing_area`                     
  --> src/chess_position_trainer/graphic/chessboard.rs:31:9                     
   |                                                                            
29 |         let chessBoardRef = Rc::new(chessBoard);                           
   |                                     ---------- value moved here            
30 |                                                                            
31 |         chessBoard.drawing_area.connect_draw(move |_, cr|{                 
   |         ^^^^^^^^^^^^^^^^^^^^^^^ value used here after move                 
   |                                                                            
   = note: move occurs because `chessBoard` has type `chess_position_trainer::graphic::chessboard::ChessBoard`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `chessBoard`                                  
  --> src/chess_position_trainer/graphic/chessboard.rs:40:9                     
   |                                                                            
29 |         let chessBoardRef = Rc::new(chessBoard);                           
   |                                     ---------- value moved here            
...                                                                             
40 |         chessBoard                                                         
   |         ^^^^^^^^^^ value used here after move                              
   |                                                                            
   = note: move occurs because `chessBoard` has type `chess_position_trainer::graphic::chessboard::ChessBoard`, which does not implement the `Copy` trait

error: aborting due to 2 previous errors                                        

For more information about this error, try `rustc --explain E0382`.             
error: Could not compile `chess_position_trainer`.                              

To learn more, run the command again with --verbose.
Maybe that's not the right purpose of std::Arc, but I am trying to get a new reference to the chessBoard so that it could be "moved or borrowed" safely
Denis Lisov
@tanriol
Nov 01 2018 14:26
In cases like this I usually have fn new() -> Rc<Self>
laurent bernabé
@loloof64
Nov 01 2018 14:30
Thank you :smiley:
Sorry for my naivete again, by how to "bypass" the move problem in this new attempt ?
    pub fn new(cells_size: u32) -> Rc<Self>
    {
        let piece_images = PieceImages::new(cells_size as i32);
        let drawing_area = DrawingArea::new();

        let chessBoard = ChessBoard {
            drawing_area: drawing_area,
            cells_size: cells_size,
        };

        let chessBoardRef = Rc::new(chessBoard);

        chessBoard.drawing_area.connect_draw(move |_, cr|{
            chessBoardRef.draw_background(cr);
            chessBoardRef.draw_cells(cr, cells_size);
            chessBoardRef.draw_pieces(cr, cells_size, &piece_images);
            chessBoardRef.draw_coordinates(cr, cells_size);

            Inhibit(false)
        });

        Rc::new(chessBoard)
    }
$ cargo run
   Compiling chess_position_trainer v0.1.0 (/home/laurent-bernabe/Documents/Programmation/ProjetsPersos/Rust/chess_position_trainer)
error[E0382]: use of moved value: `chessBoard.drawing_area`                     
  --> src/chess_position_trainer/graphic/chessboard.rs:31:9                     
   |                                                                            
29 |         let chessBoardRef = Rc::new(chessBoard);                           
   |                                     ---------- value moved here            
30 |                                                                            
31 |         chessBoard.drawing_area.connect_draw(move |_, cr|{                 
   |         ^^^^^^^^^^^^^^^^^^^^^^^ value used here after move                 
   |                                                                            
   = note: move occurs because `chessBoard` has type `chess_position_trainer::graphic::chessboard::ChessBoard`, which does not implement the `Copy` trait

error[E0382]: use of moved value: `chessBoard`                                  
  --> src/chess_position_trainer/graphic/chessboard.rs:40:17                    
   |                                                                            
29 |         let chessBoardRef = Rc::new(chessBoard);                           
   |                                     ---------- value moved here            
...                                                                             
40 |         Rc::new(chessBoard)                                                
   |                 ^^^^^^^^^^ value used here after move                      
   |                                                                            
   = note: move occurs because `chessBoard` has type `chess_position_trainer::graphic::chessboard::ChessBoard`, which does not implement the `Copy` trait

error: aborting due to 2 previous errors                                        

For more information about this error, try `rustc --explain E0382`.             
error: Could not compile `chess_position_trainer`.                              

To learn more, run the command again with --verbose.
laurent bernabé
@loloof64
Nov 01 2018 14:47
Finally solved ! :smiley:
trsh
@trsh
Nov 01 2018 14:48
I would try to RF a bit, and live without Rc
laurent bernabé
@loloof64
Nov 01 2018 14:48
From the original code, what have been changed are the calls to the ChessBoard::func()
trsh
@trsh
Nov 01 2018 14:49
Yeah
Denis Lisov
@tanriol
Nov 01 2018 14:49
@loloof64 Please note that for big code listings gists and/or playground links work much better than inline.
laurent bernabé
@loloof64
Nov 01 2018 14:49
Thanks @trsh , but in fact, no reference problem, I can just call the functions in the ChessBoard namespace
@tanriol Ok
trsh
@trsh
Nov 01 2018 14:51
Looks, interesting. What u working on?
laurent bernabé
@loloof64
Nov 01 2018 14:52
Thanks, just a chess program where you set up a position, and try to play it against stockfish at its highest level
trsh
@trsh
Nov 01 2018 14:52
Btw in pastebin Rust language is also available
laurent bernabé
@loloof64
Nov 01 2018 14:52
So it will be more suited for endings training. Ok for Pastbin, will look again
Done for PasteBin. Thanks again :smiley:
trsh
@trsh
Nov 01 2018 14:54
Good luck with your work
laurent bernabé
@loloof64
Nov 01 2018 14:54
Thank you :smiley: With Gtk-Rs, it should not be that difficult, I hope
trsh
@trsh
Nov 01 2018 14:55
@loloof64 is it fast?
For a game like project I would use something that uses GPU horse power
laurent bernabé
@loloof64
Nov 01 2018 14:57
Running yes, compilation might not
But I don't use any animation yet
trsh
@trsh
Nov 01 2018 14:58
"yet" :D
laurent bernabé
@loloof64
Nov 01 2018 14:58
Yes
Maybe I'll put animations, but mostly not :D
Furthermore I've just manage to compile the project on Windows 10 : that's was not so easy to set up
If you some day thing other than now :)
laurent bernabé
@loloof64
Nov 01 2018 15:01
You're right, an engine can be a solution : but for this project I'll need file chooser
Thank you anyway :smile:
trsh
@trsh
Nov 01 2018 15:02
No problem
Jack O'Connor
@oconnor663
Nov 01 2018 16:23
is anyone else seeing Travis builds fail for nightly rust on macOS?
laurent bernabé
@loloof64
Nov 01 2018 20:12
What can be the best practice to use std::Path ?
Because when I use the following, it pass in debug mode, but crashes in release mode.
self.window.set_icon_from_file(Path::new("resources/Chess_ql.png"))
            .expect("Could not find application icon !");
I have a folder resources , next to the src folder in my project (so outside of it)
laurent bernabé
@loloof64
Nov 01 2018 20:24
(Maybe the bug comes from a bad usage of the GTK::Window method set_icon_from_file)