Incompatible type for trait


I am a rust beginner and i fail to use my own type with a trait. Explanations:

I am using coffee crate. This crate offer a UserInterface trait with the associated Renderer type.

I want to use my own Rendered. Doc say:

If you want to write your own renderer, you will need to implement the core::Renderer trait.

So I write my own (in fact, i copied coffee Renderer):

pub struct Renderer {
    pub(crate) sprites: Batch,
    pub(crate) images: Vec<Batch>,
    pub(crate) font: Rc<RefCell<Font>>,
    explain_mesh: Mesh,

// [...]

impl core::Renderer for Renderer {
    type Configuration = Configuration;
    // [...]

And declared it as my Renderer type:

use crate::ui::renderer::Renderer;  // my own
// ...
impl UserInterface for MyGame {
    type Message = Message;
    type Renderer = Renderer;
    // ...
    fn layout(&mut self, window: &Window) -> Element<Message> {

But, compiler say the following:

error[E0053]: method `layout` has an incompatible type for trait
   --> src/
388 |     fn layout(&mut self, window: &Window) -> Element<Message> {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `ui::renderer::Renderer`, found struct `coffee::ui::renderer::Renderer`
    = note: expected fn pointer `fn(&mut game::MyGame, &coffee::graphics::window::Window) -> coffee::ui::core::element::Element<'_, _, ui::renderer::Renderer>`
               found fn pointer `fn(&mut game::MyGame, &coffee::graphics::window::Window) -> coffee::ui::core::element::Element<'_, _, coffee::ui::renderer::Renderer>`

Compiler expect `fn(&mut game::MyGame, &coffee::graphics::window::Window) -> coffee::ui::core::element::Element<'_, _, ui::renderer::Renderer>`. This is what i want. But it found: fn(&mut game::MyGame, &coffee::graphics::window::Window) -> coffee::ui::core::element::Element<'_, _, coffee::ui::renderer::Renderer>. Coffee matching source code is this (i removed comments):

pub trait UserInterface: Game {
    type Message;
    type Renderer: self::core::Renderer;
    fn react(&mut self, message: Self::Message, window: &mut Window);
    fn layout(
        &mut self,
        window: &Window,
    ) -> self::core::Element<'_, Self::Message, Self::Renderer>;

I assumed that Self::Renderer is overrided by my own Rendered, when i impl UserInterface for MyGame. But it look like not ...

I don't understand what i have to do. Help very appreciated !

Your type signature for layout() says it returns Element<Message>, which has one fewer type parameters than the matching self::core::Element<'_, Self::Message, Self::Renderer> in the coffee source code. I suspect Element in your code is instead referring to coffee::ui::Element, which uses the default Renderer. If you want to use your own Renderer, you shouldn't be using that type. You may have imported it by accident, so the easiest fix would be to just remove the import. In the unlikely event that you need to have both in scope for different parts of the code, you would need to disambiguate it to refer to self::core::Element.

You got it. I had to define my own pub type Element<'a, Message> = self::core::Element<'a, Message, Renderer>; where Renderer is mine.

Thanks !

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.