Skip to content

Lesson 1.7: Rectangle.to_sdl() method needs to return Option #33

@FFX01

Description

@FFX01

Currently, the code in the lesson looks like this:

//  ~/src/phi/data.rs
// ...
pub fn to_sdl(self) -> Option<SdlRect> {
    assert!(self.w >= 0.0 && self.h >= 0.0);
    SdlRect::new(
        self.x as i32, self.y as i32, self.w as u32, self.h as u32
    )
}

With newer version of sdl2(v0.24.0 specifically) this will fail at the compile step as Renderer.copy() expects 2 Option<Rect>'s as arguments. I'm fairly new to Rust, but I fixed the issue by changing the code to the following:

pub fn to_sdl(self) -> Option<SdlRect> {
    assert!(self.w >= 0.0 && self.h >= 0.0);
    Some(SdlRect::new(
        self.x as i32, self.y as i32, self.w as u32, self.h as u32
    ))
}

Let me know if you think this a good solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions