These are chat archives for rust-lang/rust

23rd
Jan 2018
Alexandre Bury
@gyscos
Jan 23 2018 00:17
fn foo<T: Into<f64>>(value: T) {
    let value: f64 = value.into();
}

foo(3.1f64);
foo(43u8);
Maciej Gorywoda
@makingthematrix
Jan 23 2018 08:56
Does into take a copy of the original value and only then create the new one based on it? Or does it work on the original value? Yesterday I was doing an example where I implemented into on a vector. But I wouldn't like to copy the entire vector simply to ensure ownership. A reference to the original vector should be enough.
Michal 'vorner' Vaner
@vorner
Jan 23 2018 09:01
@makingthematrix Rust doesn't copy complex things unless you ask it to explicitly. Into usually consumes the original.
red75prime
@red75prime
Jan 23 2018 09:03
Into::into() always consumes original, unless it's Copy.
Michal 'vorner' Vaner
@vorner
Jan 23 2018 09:05
@red75prime Strictly speaking, that's true. But the original may be &T, not T (eg. it depends on what exact type implements the Into). So it might look like it didn't consume the original.
Maciej Gorywoda
@makingthematrix
Jan 23 2018 09:06
yes, at some point I started to use &Vec instead of Vec :)
Daniel Bischof
@dbischof90
Jan 23 2018 13:10
Is there any other nice, 'rustancean' way to drop a reference besides embedding it into a scope?
I have the feeling rust is telling me that I design my program in a bad way if I think about doing things like that
Daniel Bischof
@dbischof90
Jan 23 2018 13:27
I have something 'almost-working' now for a problem which is essentially a conditional recursive function call with a peek_mut() to the top element. I somehow solved that (almost, at least on paper) now by some what I think ugly scoping. which I could in theory solve by just handle the one or two cases where the peek still lives by something like 'drop()'. I'm not sure if that's a rusty way to do it.
Denis Lisov
@tanriol
Jan 23 2018 14:17
@dbischof90 Almost working?
blankhart
@blankhart
Jan 23 2018 14:23
@dbischof90 is another way to say this that you want to terminate a mutable borrow from peek_mut (and restore ownership to a collection name, rather than freeing memory) without leaving a current scope? not sure i understand the use case - it's because you want to make a recursive function call with a mutable borrow of the entire collection, which you have partially mutably borrowed in the peek_mut?
Michal 'vorner' Vaner
@vorner
Jan 23 2018 14:26
@blankhart It can't be done with stable Rust, but the NLL feature on nightly might solve the problem for you. On stable, you usually have to do some inelegant trick with introducing another scope yourself. Like this (guessing your situation):
let recurse = {
  let x = collection.peek_mut();
  do_i_want_to_recurse_more(x)
};
if recurse {
  recursive_call(collection);
}
blankhart
@blankhart
Jan 23 2018 14:30
@vorner thank you but i think your answer was meant for @dbischof90 (if the guess is right)
Michal 'vorner' Vaner
@vorner
Jan 23 2018 14:31
Oh, sorry, I thought the posts were from a single person… Not paying attention :-|
laurent bernabé
@loloof64
Jan 23 2018 15:38
Hi everyone :)
extern crate sdl2;

use sdl2::Sdl;
use sdl2::video::Window;
use sdl2::event::{Event};
use sdl2::render::WindowCanvas;
use sdl2::pixels::Color;

struct Sdl2Objects<'a> {
    ctx: Sdl,
    windowPtr: &'a mut Window,
    canvas: WindowCanvas,
}

fn main() {
    let mut sdl2_ctx = build_window_and_get_sdl2_objects();
    sdl2_ctx.windowPtr.show();
    manage_render_loop(sdl2_ctx);
}

fn build_window_and_get_sdl2_objects<'a>() -> Sdl2Objects<'a> {
    let ctx = sdl2::init().expect("Failed to init SDL2 context.");
    let video_ctx = ctx.video().expect("Failed to init SDL2 video context.");

    let window = match video_ctx.window("Morpion game", 400, 400)
        .position_centered()
        .opengl()
        .build()
    {
        Ok(window) => window,
        Err(error) => panic!("Failed to create window : {}", error)
    };

    let mut canvas = window.into_canvas().build().expect("Failed to get canvas from window.");
    canvas.set_draw_color(Color::RGB(255,0,0));
    canvas.clear();

    Sdl2Objects{ ctx, windowPtr: &mut window, canvas }
}

fn manage_render_loop(sdl2_objects: Sdl2Objects) {
    let mut events = sdl2_objects.ctx.event_pump().expect("Failed to get event pump");
    'running: loop {
        for event in events.poll_iter() {
            match event {
                Event::Quit{..} => break 'running,
                _ => continue
            }
        }
    }
}
In the build_window_and_get_sdl2_objects function, how can make the & mut window value live longer than the function ?
I've tried using lifetime, but it does not work either.
Michal 'vorner' Vaner
@vorner
Jan 23 2018 15:44
Lifetimes don't change how long something live. It only explains the relations between different lifetimes to the compiler if it can't figure them itself. You basically need to make sure it lives long enough by returning it. Why don't you return the window directly, instead a reference to it?
laurent bernabé
@loloof64
Jan 23 2018 15:45
Thanks : because this time I am facing a moved window value.
Because of the canvas creation, which moves the window value
Michal 'vorner' Vaner
@vorner
Jan 23 2018 15:46
That seems wrong. If it consumed the window, then you shouldn't be able to take &mut reference to it anyway.
laurent bernabé
@loloof64
Jan 23 2018 15:46
Ok thanks. So I'd better create the window outside and pass it to the function.
Michal 'vorner' Vaner
@vorner
Jan 23 2018 15:47
That should probably work
laurent bernabé
@loloof64
Jan 23 2018 15:47
Thank you :)
I am just trying to exit from the classical Sdl tutorials method, which makes all in the main method.
(Quite an ugly solution I find than putting all in the main place)
Daniel Bischof
@dbischof90
Jan 23 2018 15:56

@tanriol yes, "almost working" (TM) ;) I fiddled around and came to the conclusion that I can come up with a move to not having to fight with the constant &mut self everywhere and eventually "drag out" the sensible parts (where the recursions appear) of the match statements with two options which I return by scoping the match statements. A little ugly, as mentioned but I only need the references in that block, so.. fine.
@vorner Interesting, NLLs seem to solve the exact problem I was fighting with - but the fact that it is in nightly possibly speaks for itself. And I think I should first find a solution based on something that is stable for now ;) As assumed, some scoping is thus the way to go for now.

Thanks a lot!

laurent bernabé
@loloof64
Jan 23 2018 16:23
I've refactored the class: could someone help me find why the windows shows up and diseappear right after ?
extern crate sdl2;

use sdl2::Sdl;
use sdl2::VideoSubsystem;
use sdl2::video::Window;
use sdl2::event::{Event};
use sdl2::render::WindowCanvas;
use sdl2::pixels::Color;

fn main() {
    let (sdl2_ctx, video_ctx) = build_context();
    let window = build_window_from_video_context(video_ctx);
    let _ = build_canvas_from_window(window);
    manage_render_loop(sdl2_ctx);
}

fn build_context() -> (Sdl, VideoSubsystem) {
    let ctx = sdl2::init().expect("Failed to init SDL2 context.");
    let video_ctx = ctx.video().expect("Failed to init SDL2 video context.");

    (ctx, video_ctx)
}

fn build_window_from_video_context(video_ctx: VideoSubsystem) -> Window {
    let mut window = match video_ctx.window("Morpion game", 400, 400)
        .position_centered()
        .opengl()
        .build()
    {
        Ok(window) => window,
        Err(error) => panic!("Failed to create window : {}", error)
    };

    window.show();

    window
}

fn build_canvas_from_window(window: Window) -> WindowCanvas {
    let mut canvas = window.into_canvas().present_vsync().build().expect("Failed to get canvas from window.");
    canvas.set_draw_color(Color::RGB(255,0,0));
    canvas.clear();

    canvas
}

fn manage_render_loop(ctx: Sdl) {
    let mut events = ctx.event_pump().expect("Failed to get event pump");
    'running: loop {
        for curr_event in events.poll_iter() {
            match curr_event {
                Event::Quit{..} => break 'running,
                _ => {}
            }
        }
    }
}
As you can see, I've set up the event polling.
(sdl2 = "0.31.0")
~Maybe this is because nothing is ever drawn on my screen (https://stackoverflow.com/a/34581579/662618), trying to draw into the canvas to see if this can solve the issue.~
laurent bernabé
@loloof64
Jan 23 2018 16:30
That's not the cause
laurent bernabé
@loloof64
Jan 23 2018 17:35
Ok found the problem : I must update the rendering in the running loop
extern crate sdl2;

use sdl2::Sdl;
use sdl2::VideoSubsystem;
use sdl2::video::Window;
use sdl2::event::{Event};
use sdl2::render::WindowCanvas;
use sdl2::pixels::Color;

fn main() {
    let (sdl2_ctx, video_ctx) = build_context();
    let window = build_window_from_video_context(video_ctx);
    let mut canvas = build_canvas_from_window(window);
    manage_render_loop(sdl2_ctx, &mut canvas);
}

fn build_context() -> (Sdl, VideoSubsystem) {
    let ctx = sdl2::init().expect("Failed to init SDL2 context.");
    let video_ctx = ctx.video().expect("Failed to init SDL2 video context.");

    (ctx, video_ctx)
}

fn build_window_from_video_context(video_ctx: VideoSubsystem) -> Window {
    let mut window = match video_ctx.window("Morpion game", 400, 400)
        .position_centered()
        .opengl()
        .build()
    {
        Ok(window) => window,
        Err(error) => panic!("Failed to create window : {}", error)
    };

    window.show();

    window
}

fn build_canvas_from_window(window: Window) -> WindowCanvas {
    let mut canvas = window.into_canvas().present_vsync().build().expect("Failed to get canvas from window.");
    canvas.set_draw_color(Color::RGB(255,0,0));
    canvas.clear();
    canvas.present();

    canvas
}

fn manage_render_loop(ctx: Sdl, canvas: &mut WindowCanvas) {
    let mut events = ctx.event_pump().expect("Failed to get event pump");
    'running: loop {
        for curr_event in events.poll_iter() {
            match curr_event {
                Event::Quit{..} => break 'running,
                _ => {}
            }
        }
        canvas.set_draw_color(Color::RGB(255,0,0));
        canvas.clear();
        canvas.present();
    }
}
laurent bernabé
@loloof64
Jan 23 2018 20:13
Stupid question maybe : but is it possible to convert from i16 to u32 ?
laurent bernabé
@loloof64
Jan 23 2018 20:16
Thank you very much :)
Forgot about casting
Arthur
@Biacode
Jan 23 2018 20:16
Welcome :)