Strange dependency problem?!

I have the following code in file a.rs that is without a problem:

    pub fn on_draw(&mut self, ren: &RenderArgs, window: &mut PistonWindow, event: &Event) {
        let window_dimensions = ren.draw_size;
        let width = window_dimensions[0];
        let height = window_dimensions[1];

        self.scx = (width / 2) as f64;
        self.scy = (height / 2) as f64;

        window.draw_2d(event, |context, graphics, _| {
            clear([0.0, 0.0, 0.0, 1.0], graphics);
            let center = context.transform.trans(self.scx, self.scy);

            // THIS IS A FUNCTION CALL TO AN INSTANCE (board_display) OF A STRUCT
            self.board_display.render(graphics, center);  

        });
    }

but when I try to call "self.board_display.render(graphics, center);" above,
the following code gives me this error: from a different file b.rs

    pub fn render(&self, g: &mut GfxGraphics<gfx_device_gl::Resources, gfx_device_gl::command::CommandBuffer>, view: math::Matrix2d) {
    }


error[E0433]: failed to resolve: use of undeclared type or module `gfx_device_gl`
   --> src/chessboard.rs:121:46
    |
121 |     pub fn render(&self, g: &mut GfxGraphics<gfx_device_gl::Resources, gfx_device_gl::command::CommandBuffer>, view: math::Matrix2d) {
    |                                              ^^^^^^^^^^^^^ use of undeclared type or module `gfx_device_gl`

error[E0433]: failed to resolve: use of undeclared type or module `gfx_device_gl`
   --> src/chessboard.rs:121:72
    |
121 |     pub fn render(&self, g: &mut GfxGraphics<gfx_device_gl::Resources, gfx_device_gl::command::CommandBuffer>, view: math::Matrix2d) {
    |                                                                        ^^^^^^^^^^^^^ use of undeclared type or module `gfx_device_gl`

error[E0412]: cannot find type `GfxGraphics` in this scope
   --> src/chessboard.rs:121:34
    |
121 |     pub fn render(&self, g: &mut GfxGraphics<Resources, CommandBuffer>, view: math::Matrix2d) {
    |                                  ^^^^^^^^^^^ help: a trait with a similar name exists: `Graphics`
    | 
   ::: /home/nexus/.cargo/registry/src/github.com-1ecc6299db9ec823/piston2d-graphics-0.36.0/./src/graphics.rs:35:1
    |
35  | pub trait Graphics: Sized {
    | ------------------------- similarly named trait `Graphics` defined here

error[E0412]: cannot find type `Resources` in this scope
   --> src/chessboard.rs:121:46
    |
43  | impl Chessboard {
    |     - help: you might be missing a type parameter: `<Resources>`
...
121 |     pub fn render(&self, g: &mut GfxGraphics<Resources, CommandBuffer>, view: math::Matrix2d) {
    |                                              ^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `CommandBuffer` in this scope
   --> src/chessboard.rs:121:57
    |
43  | impl Chessboard {
    |     - help: you might be missing a type parameter: `<CommandBuffer>`
...
121 |     pub fn render(&self, g: &mut GfxGraphics<Resources, CommandBuffer>, view: math::Matrix2d) {
    |                                                         ^^^^^^^^^^^^^ not found in this scope

But I look at the other file's "use" statements and they are identical.
In file a.rs the "graphics" parameter for

self.board_display.render(graphics, center);

is:

&mut GfxGraphics<Resources, CommandBuffer>

which breaks down to:

piston2d-gfx_graphics::back_end::GfxGraphics<gfx_device_gl::Resources,
gfx_device_gl::command::CommandBuffer

but when I add these specifics I get the above errors.

I'm stumped! Any suggestions?

Ok, I "thought"solved the problem by adding two "use" statements:

use gfx_device_gl::{CommandBuffer, Resources};
use gfx_graphics::GfxGraphics;

but they gave me the following error:

error[E0432]: unresolved import `gfx_device_gl`
 --> src/chessboard.rs:2:5
  |
2 | use gfx_device_gl::{CommandBuffer, Resources};
  |     ^^^^^^^^^^^^^ help: a similar path exists: `piston_window::gfx_device_gl`

error[E0432]: unresolved import `gfx_graphics`
 --> src/chessboard.rs:3:5
  |
3 | use gfx_graphics::GfxGraphics;
  |     ^^^^^^^^^^^^ help: a similar path exists: `piston_window::gfx_graphics`

It seems like I'm missing something in the toml; but how can that be if file a.rs has no problems?

Does a.rs actually contain the text gfx_device_gl, GfxGraphics and so on? If not, they don't need to be imported.

The error messages in your second post seem to suggest the correct import, did you try
use piston_window::gfx_device_gl and use piston_window::gfx_graphics?

@Heliozoa a.rs must contain gfx_device_gl , GfxGraphics etc sonce it compiles without error.
There is no piston_window::gfx_device_gl and use piston_window::gfx_graphics.

If I've understood you correctly, this isn't true. Try the following playground code. If you remove the import, it fails to compile because it can't find Pin. But if you then also remove the type annotation, it works again, because Pin doesn't have to be imported for you to use values of type Pin. It's only if you actually directly refer to Pin (like in the type annotation) that you need it.

@Heliozoa I placed all the code in the same file. The following function works to display a red rectangle in a window:

    pub fn on_draw(&mut self, ren: &RenderArgs, window: &mut PistonWindow, event: &Event) {
        let window_dimensions = ren.draw_size;
        let width = window_dimensions[0];
        let height = window_dimensions[1];

        self.scx = (width / 2) as f64;
        self.scy = (height / 2) as f64;

        window.draw_2d(event, |context, graphics, _| {
            clear([0.0, 0.0, 0.0, 1.0], graphics);
            let center = context.transform.trans(self.scx, self.scy);
            let square = rectangle::square(0.0, 0.0, 100.0);
            let red = [1.0, 0.0, 0.0, 1.0];
            rectangle(
                red,
                square,
                center.trans(0.0, 0.0).trans(-50.0, -50.0),
                graphics,
            );
        });
    }

....but doing the following function signature causes an error where the "graphics" type is not recognized. Why? and how does one fix this??

pub fn render(&self, graphics: &mut GfxGraphics<Resources, CommandBuffer>, view: math::Matrix2d) {
}

Errors are:

error[E0412]: cannot find type `GfxGraphics` in this scope
   --> src/cot_app.rs:131:41
    |
131 |     pub fn render(&self, graphics: &mut GfxGraphics<Resources, CommandBuffer>, view: math::Matrix2d) {
    |                                         ^^^^^^^^^^^ help: a trait with a similar name exists: `Graphics`
    | 
   ::: /home/nexus/.cargo/registry/src/github.com-1ecc6299db9ec823/piston2d-graphics-0.36.0/./src/graphics.rs:35:1
    |
35  | pub trait Graphics: Sized {
    | ------------------------- similarly named trait `Graphics` defined here

error[E0412]: cannot find type `Resources` in this scope
   --> src/cot_app.rs:131:53
    |
16  | impl CotApp {
    |     - help: you might be missing a type parameter: `<Resources>`
...
131 |     pub fn render(&self, graphics: &mut GfxGraphics<Resources, CommandBuffer>, view: math::Matrix2d) {
    |                                                     ^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `CommandBuffer` in this scope
   --> src/cot_app.rs:131:64
    |
16  | impl CotApp {
    |     - help: you might be missing a type parameter: `<CommandBuffer>`
...
131 |     pub fn render(&self, graphics: &mut GfxGraphics<Resources, CommandBuffer>, view: math::Matrix2d) {
    |                                                                ^^^^^^^^^^^^^ not found in this scope

Ok! I finally solved it. Apparently, "window.draw_2d" closure vars types are not recognized "outside the closure"!; which was not that obvious to me.

The solution was adding to the .toml file:

gfx_device_gl = "0.16.2"
piston2d-gfx_graphics = "0.68.0"

....then in the function signature not recognizing types, change it to:

pub fn render(&self, graphics: &mut gfx_graphics::GfxGraphics<gfx_device_gl::Resources, gfx_device_gl::CommandBuffer>, view: math::Matrix2d) {
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.