Skip to content

Examples don't build in a separate repo #174

@ianh

Description

@ianh

The examples build and run from the softbuffer repository, but fail to build in a separate test repo. I tested with the following Cargo.toml and src/main.rs (copied from softbuffer examples/winit.rs):

Cargo.toml:

[package]
name = "softbuffer-test"
version = "0.1.0"
edition = "2021"

[dependencies]
softbuffer = "0.3.3"
winit = "0.29.3"

src/main.rs:

use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
use winit::window::WindowBuilder;

fn main() {
    let event_loop = EventLoop::new().unwrap();
    let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());

    #[cfg(target_arch = "wasm32")]
    {
        use winit::platform::web::WindowExtWebSys;

        web_sys::window()
            .unwrap()
            .document()
            .unwrap()
            .body()
            .unwrap()
            .append_child(&window.canvas().unwrap())
            .unwrap();
    }

    let context = softbuffer::Context::new(window.clone()).unwrap();
    let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

    event_loop
        .run(move |event, elwt| {
            elwt.set_control_flow(ControlFlow::Wait);

            match event {
                Event::WindowEvent {
                    window_id,
                    event: WindowEvent::RedrawRequested,
                } if window_id == window.id() => {
                    if let (Some(width), Some(height)) = {
                        let size = window.inner_size();
                        (NonZeroU32::new(size.width), NonZeroU32::new(size.height))
                    } {
                        surface.resize(width, height).unwrap();

                        let mut buffer = surface.buffer_mut().unwrap();
                        for y in 0..height.get() {
                            for x in 0..width.get() {
                                let red = x % 255;
                                let green = y % 255;
                                let blue = (x * y) % 255;
                                let index = y as usize * width.get() as usize + x as usize;
                                buffer[index] = blue | (green << 8) | (red << 16);
                            }
                        }

                        buffer.present().unwrap();
                    }
                }
                Event::WindowEvent {
                    event:
                        WindowEvent::CloseRequested
                        | WindowEvent::KeyboardInput {
                            event:
                                KeyEvent {
                                    logical_key: Key::Named(NamedKey::Escape),
                                    ..
                                },
                            ..
                        },
                    window_id,
                } if window_id == window.id() => {
                    elwt.exit();
                }
                _ => {}
            }
        })
        .unwrap();
}

cargo run output:

   Compiling softbuffer-test v0.1.0 (/Users/ianh/Desktop/softbuffer-test)
error[E0308]: mismatched types
   --> src/main.rs:26:44
    |
26  |     let context = softbuffer::Context::new(window.clone()).unwrap();
    |                   ------------------------ ^^^^^^^^^^^^^^ expected `&_`, found `Rc<Window>`
    |                   |
    |                   arguments to this function are incorrect
    |
    = note: expected reference `&_`
                  found struct `Rc<Window>`
note: associated function defined here
   --> /Users/ianh/.cargo/registry/src/index.crates.io-6f17d22bba15001f/softbuffer-0.3.3/src/lib.rs:198:19
    |
198 |     pub unsafe fn new<D: HasRawDisplayHandle>(display: &D) -> Result<Self, SoftBufferError> {
    |                   ^^^
help: consider borrowing here
    |
26  |     let context = softbuffer::Context::new(&window.clone()).unwrap();
    |                                            +

error[E0308]: mismatched types
   --> src/main.rs:27:58
    |
27  |     let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
    |                       ------------------------           ^^^^^^^^^^^^^^ expected `&_`, found `Rc<Window>`
    |                       |
    |                       arguments to this function are incorrect
    |
    = note: expected reference `&_`
                  found struct `Rc<Window>`
note: associated function defined here
   --> /Users/ianh/.cargo/registry/src/index.crates.io-6f17d22bba15001f/softbuffer-0.3.3/src/lib.rs:277:19
    |
277 |     pub unsafe fn new<W: HasRawWindowHandle>(
    |                   ^^^
help: consider borrowing here
    |
27  |     let mut surface = softbuffer::Surface::new(&context, &window.clone()).unwrap();
    |                                                          +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `softbuffer-test` (bin "softbuffer-test") due to 2 previous errors

rustc --version: rustc 1.73.0 (cc66ad468 2023-10-03)

cargo --version: cargo 1.73.0 (9c4383fb5 2023-08-26)

uname -a: Darwin imhmba.local 21.6.0 Darwin Kernel Version 21.6.0: Wed Oct 4 23:54:48 PDT 2023; root:xnu-8020.240.18.704.15~1/RELEASE_ARM64_T8101 arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions