Need help accessing the registry in Wayland-Client crate

Hi,
I was hoping someone could help me translate the C code from this tutorial to Rust.

My code looks like this:

use wayland_client as client;
use client::protocol::wl_display::WlDisplay;
use client::protocol::wl_registry::WlRegistry;
use client::Proxy;

fn main() {
    let display = client::Display::connect_to_env().unwrap();

    let event_queue = display.create_event_queue();

    let wl_display: &Proxy<WlDisplay> = &*display;
    let reg = wl_display.into().get_registry();
}

The docs for wayland-client say that you can use the .into() method to convert a Proxy<WlDisplay> into a WlDisplay, but the compiler gives me this error:

error[E0282]: type annotations needed
  --> src/main.rs:12:26
   |
12 |     let reg = wl_display.into().get_registry();
   |               -----------^^^^--
   |               |          |
   |               |          cannot infer type for type parameter `T` declared on the trait `Into`
   |               this method call resolves to `T`
   |
   = note: type must be known at this point

So I change the line to:
let reg = wl_display.into::<WlDisplay>().get_registry();

and rustc tells me that the .into() method expected 0 generic arguments.

Any help here would be seriously appreciated.

Here's a link to the docs: wayland_client - Rust

I only looked at the Into situation, not the rest of the library.

Try this:

let display: WlDisplay = wl_display.into();
let reg = display.get_registry();

get_registry borrows the WlDisplay, so you'll need to keep it around in more than a temporary. If that wasn't the case, you could do it in one line with something like

let reg = Into::<WlDisplay>::into(wl_display).get_registry();

(The type parameter is on the trait, not the function.)

1 Like

Thank you for such a quick reply! The first line gave me an error, but I kept trying what the compiler recommended until I got this error: 'Attemping to create an object from a non-attached proxy.'

I didn't realize before that you have to attach things to an event_queue before you can do anything with them, so I was able to come up with this code (for anyone who has this same problem six years from now lol). It not an exact 1-to-1 representation of the wayland-book code, but it does approximately the same:

use wayland_client as client;
use client::protocol::wl_display::WlDisplay;
use client::protocol::wl_registry::WlRegistry;
use client::Proxy;

fn main() {
    let display = client::Display::connect_to_env().unwrap();

    let mut event_queue = display.create_event_queue();
    let eq_token = event_queue.token();

    let wl_display = display.attach(eq_token);

    let reg = wl_display.get_registry();
    
    event_queue.dispatch(&mut (), |ev, _, _| {
        println!("{:?}", ev);
    });
}

Thanks for the suggestion!