2

Repository
https://github.com/hunterlester/rusty_arcade

Versions
Rust: 1.7.0
sdl2: 0.16.1
sdl2_image: 0.16.0

Error

src/views/mod.rs:88:13: 93:23 error: mismatched types:
 expected `core::option::Option<sdl2::rect::Rect>`,
    found `sdl2::rect::Rect`
(expected enum `core::option::Option`,
    found struct `sdl2::rect::Rect`) [E0308]
src/views/mod.rs:88             Rectangle {
src/views/mod.rs:89                 x: 0.0,
src/views/mod.rs:90                 y: 0.0,
src/views/mod.rs:91                 w: self.player.rect.w,
src/views/mod.rs:92                 h: self.player.rect.h,
src/views/mod.rs:93             }.to_sdl(),
src/views/mod.rs:88:13: 93:23 help: run `rustc --explain E0308` to see  a detailed explanation
src/views/mod.rs:94:13: 94:38 error: mismatched types:
 expected `core::option::Option<sdl2::rect::Rect>`,
    found `sdl2::rect::Rect`
(expected enum `core::option::Option`,
    found struct `sdl2::rect::Rect`) [E0308]
src/views/mod.rs:94             self.player.rect.to_sdl()

Tracking it down
Lines 93 and 94 of the specified file.

phi.renderer.copy(&mut self.player.tex,
            Rectangle {
                x: 0.0,
                y: 0.0,
                w: self.player.rect.w,
                h: self.player.rect.h,
            }.to_sdl(), // Line 93
            self.player.rect.to_sdl() // Line 94
        );

I'm assuming this has something to do with what the .to_sdl() method is returning.

to_sdl method

impl Rectangle {
    pub fn to_sdl(self) -> 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)
    }

to_sdl returns an SdlRect which is in use at top of file:

use sdl2::rect::Rect as SdlRect;

sdl2 source
https://github.com/AngryLawyer/rust-sdl2/blob/master/src/sdl2/rect.rs

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Rect {
    raw: ll::SDL_Rect,
}

impl Rect {

pub fn new(x: i32, y: i32, width: u32, height: u32) -> Rect {
    let raw = ll::SDL_Rect {
        x: clamp_position(x),
        y: clamp_position(y),
        w: clamp_size(width) as i32,
        h: clamp_size(height) as i32,
    };
    Rect { raw: raw }
}

I don't see where sdl2::rect::Rect is expected to be wrapped in a core::option::Option enum type.

If you happen to be familiar with the tutorial that I'm following, you'll notice that I'm using a different version of sdl2_image than specified in the tutorial because the specified has been yanked from Crates.

sdl2_image depends on a different version of sdl2 which I had to match in order to take care of other errors.

Hunter Lester
  • 2,972
  • 3
  • 15
  • 19

1 Answers1

6

The error means that the expression self.player.rect.to_sdl() yields a SdlRect, but that whatever uses that value, expects to be given an Option<SdlRect>. You are calling the copy of sdl2::render::Renderer, which has the following arguments:

&mut self, texture: &Texture, src: Option<Rect>, dst: Option<Rect>

As you can see, you need to pass two Option<Rect>s. The sdl-crate's documentation even says why:

Copies a portion of the texture to the current rendering target.

  • If src is None, the entire texture is copied.
  • If dst is None, the texture will be stretched to fill the given rectangle.
oli_obk
  • 28,729
  • 6
  • 82
  • 98