Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix raw window handle pub #1359

Closed

Conversation

serpilliere
Copy link
Contributor

If an end user wants to use raw handles, the trait must be defined as public to be included in external projects.

@Cobrand
Copy link
Member

Cobrand commented Jan 3, 2024

I don't understand how this is necessary? If you add raw-window-handle in your external project's Cargo.toml, it will use the same version if it's compatible, and you should be able to use the traits normally then, should you not?

@serpilliere
Copy link
Contributor Author

Hi @Cobrand
Maybe I am missing something, but your trait is not exported as public (like, for example, a structure declaration in a crate which is not tagged as pub). So code that uses this crate are not allowed to use it.
In this case, if I create a simple rust projet with this cargo:

[package]
name = "test_sdl2_raw"
version = "0.1.0"
edition = "2021"

[dependencies]
sdl2 = {version = "0.35.0", features = ["bundled", "raw-window-handle"]}

And with this code:

use sdl2::raw_window_handle::RawWindowHandle;

fn main() {
    println!("Hello, world!");
}

I have this result:

$ cargo build --release
   Compiling test_sdl2_raw v0.1.0 (/home/serpilliere/projet/test_rust/test_sdl2_raw)
error[E0603]: enum `RawWindowHandle` is private
  --> src/main.rs:1:30
   |
1  | use sdl2::raw_window_handle::RawWindowHandle;
   |                              ^^^^^^^^^^^^^^^ private enum
   |
note: the enum `RawWindowHandle` is defined here
  --> /home/serpilliere/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sdl2-0.35.2/src/sdl2/raw_window_handle.rs:3:51
   |
3  | use self::raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
   |                                                   ^^^^^^^^^^^^^^^

So I won't be able to use functions associated to this trait. (so no raw handles)

@Cobrand
Copy link
Member

Cobrand commented Jan 5, 2024

[dependencies]
sdl2 = {version = "0.35.0", features = ["bundled", "raw-window-handle"]}
raw-window-handle = "0.5"

then

use raw_window_handle::RawWindowHandle;

doesn't this work?

You are rarely supposed to use raw-window-handle by yourself anyway, it almost always interops with something else (OpenGL, vulkan, DirectX, ...), which almost imports raw-window-handle, so I don't see why it would be our job to re-export this trait.

@serpilliere
Copy link
Contributor Author

Nop: it doesn't work and I don't really understand why.

Cargo.toml:

[package]
name = "test_sdl2_raw"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sdl2 = {version = "0.35.0", features = ["bundled", "raw-window-handle"]}
raw-window-handle = "0.5"

And the source:

use raw_window_handle::RawWindowHandle;
use raw_window_handle::HasRawWindowHandle;

fn main() {
    let sdl = sdl2::init().expect("xx");

    let video_subsystem = sdl
        .video().expect("xx");

    let clipboard = video_subsystem.clipboard();

    let gl_attr = video_subsystem.gl_attr();

    let mut builder =
        sdl2::video::WindowBuilder::new(&video_subsystem, "test", 800, 600);
    let builder = builder.opengl().allow_highdpi();
    let window = builder.build().expect("Cannot build window");
    if let RawWindowHandle::Xlib(handle) = window.raw_window_handle() {
        println!("Raw handle {:?}", handle);
    }
}

I have the following error:

$ cargo run --release
   Compiling test_sdl2_raw v0.1.0 (/home/serpilliere/projet/test_rust/test_sdl2_raw)
error[E0599]: no method named `raw_window_handle` found for struct `sdl2::video::Window` in the current scope
  --> src/main.rs:18:51
   |
18 |     if let RawWindowHandle::Xlib(handle) = window.raw_window_handle() {
   |                                                   ^^^^^^^^^^^^^^^^^ method not found in `Window`
   |
  ::: /home/serpilliere/.cargo/registry/src/index.crates.io-6f17d22bba15001f/raw-window-handle-0.4.3/src/lib.rs:58:8
   |
58 |     fn raw_window_handle(&self) -> RawWindowHandle;
   |        ----------------- the method is available for `sdl2::video::Window` here
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  + use raw_window_handle::HasRawWindowHandle;
   |

warning: unused import: `raw_window_handle::HasRawWindowHandle`
 --> src/main.rs:2:5
  |
2 | use raw_window_handle::HasRawWindowHandle;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0599`.
warning: `test_sdl2_raw` (bin "test_sdl2_raw") generated 1 warning
error: could not compile `test_sdl2_raw` (bin "test_sdl2_raw") due to previous error; 1 warning emitted

What is interesting is that the compiler:

  • first says that the import is useless
  • that we need to import the trait

It make me thinks that it's a bit like the same error when you have a version of crate used in a sub module and you use the same crate but with a different version. The compiler doesn't mix those object.

Have you got an example of a code which uses raw handles from a program outside of the crate sdl2 itself?

@Cobrand
Copy link
Member

Cobrand commented Jan 8, 2024

You are using rust-sdl2 0.35.0, which uses a different version of raw-window-handle. Try updating to rust-sdl2 0.36, or downgrading raw-window-handle.

@serpilliere
Copy link
Contributor Author

Oh!
You are right. I missed the update.
Sorry for this @Cobrand

@serpilliere serpilliere closed this Jan 9, 2024
@serpilliere serpilliere deleted the fix_pub_raw_win_handle branch January 9, 2024 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants